Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n): add documentation for custom paths #5526

Merged
merged 18 commits into from
Dec 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/content/docs/en/guides/internationalization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,55 @@ Use this like [`getRelativeLocaleUrl`](#getrelativelocaleurl) to return a list o

`getAbsoluteLocaleUrlList(locale: string, options?: GetLocaleOptions): string[]`


Use this like [`getAbsoluteLocaleUrl`](#getabsolutelocaleurl) to return a list of absolute paths for all the locales.

### `getPathByLocale(locale: string): string`

A function that return the `path` associated to a locale (defined as code). It's particularly useful in case you use [refined locales](#refined-locales).


### Refined locales

Astro allows to map an arbitrary number of locales to a particular URL path. The `locales` array can accept an object, where the `path` key is what you'll see in the URL, and `codes` represents the languages mapped to this URL.

This is useful if you support multiple variations of language, e.g. `"fr"`, `"fr-BR"` and `"fr-CA"` and you want to have all these locales mapped under the same URL, `/french`:

```js title="astro.config.mjs" del={6} ins={7-10}
import { defineConfig } from "astro/config"
export default defineConfig({
experimental: {
i18n: {
defaultLocale: "en",
locales: ["es", "en", "fr"],
locales: ["es", "en", {
path: "french", // not slash needed
codes: ["fr", "fr-BR", "fr-CA"]
}],
routingStrategy: "prefix-always"
}
}
})
```

If you rely on the [`astro:i18n` virtual module](#virtual-module-astroi18n), you'll have to use the `path` to compose the correct URL, for example:

```astro title="src/pages/index.astro"
---
import { getPathByLocale, getRelativeLocaleUrl, getLocaleByPath } from "astro:i18n";

getRelativeLocaleUrl("french", "blog"); // returns /french/blog

// These functions are not particularly useful
getPathByLocale("fr"); // returns "french"
getPathByLocale("fr-CA"); // returns "french"
getPathByLocale("es"); // returns "es"
getLocaleByPath("french"); // returns "fr" because that's the first code configured
getLocaleByPath("es"); // returns "es"
---
```

If you rely on [the browser language detection](#browser-language-detection), Astro will use the `codes` to determine the `Astro.preferredLocale` and `Astro.preferredLocaleList`. If so, it's advised to write the codes using the same pattern [used by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#syntax).


[`site`]: /en/reference/configuration-reference/#site
[`i18n.locales`]: /en/reference/configuration-reference/#experimentali18nlocales