You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Astro's experimental i18n routing API allows you to add your multilingual content with support for configuring a default language, computing relative page URLs, and accepting preferred languages provided by your visitor's browser. You can also specify fallback languages on a per-language basis so that your visitors can always be directed to existing content on your site.
8
+
9
+
Enable the experimental routing option by adding an `i18n` object to your Astro configuration with a default location and a list of all languages to support:
10
+
11
+
```js
12
+
// astro.config.mjs
13
+
import {defineConfig} from"astro/config";
14
+
15
+
exportdefaultdefineConfig({
16
+
experimental: {
17
+
i18n: {
18
+
defaultLocale:"en",
19
+
locales: ["en", "es", "pt-br"]
20
+
}
21
+
}
22
+
})
23
+
```
24
+
25
+
Organize your content folders by locale depending on your `i18n.routingStrategy`, and Astro will handle generating your routes and showing your preferred URLs to your visitors.
26
+
```
27
+
├── src
28
+
│ ├── pages
29
+
│ │ ├── about.astro
30
+
│ │ ├── index.astro
31
+
│ │ ├── es
32
+
│ │ │ ├── about.astro
33
+
│ │ │ ├── index.astro
34
+
│ │ ├── pt-br
35
+
│ │ │ ├── about.astro
36
+
│ │ │ ├── index.astro
37
+
```
38
+
39
+
Compute relative URLs for your links with `getLocaleRelativeURL` from the new `astro:i18n` module:
<p>Learn more <a href={aboutURL}>About</a> this site!</p>
47
+
```
48
+
49
+
Enabling i18n routing also provides two new properties for browser language detection: `Astro.preferredLocale` and `Astro.preferredLocaleList`. These combine the browser's `Accept-Langauge` header, and your site's list of supported languages and can be used to automatically respect your visitor's preferred languages.
50
+
51
+
Read more about Astro's [experimental i18n routing](https://docs.astro.build/en/guides/internationalization/) in our documentation.
// TODO review with docs team before merging to `main`
1445
+
/**
1446
+
* @docs
1447
+
* @name experimental.i18n
1448
+
* @type {object}
1449
+
* @version 3.5.0
1450
+
* @type {object}
1451
+
* @description
1452
+
*
1453
+
* Configures experimental i18n routing and allows you to specify some customization options.
1454
+
*/
1455
+
i18n?: {
1456
+
/**
1457
+
* @docs
1458
+
* @name experimental.i18n.defaultLocale
1459
+
* @type {string}
1460
+
* @version 3.5.0
1461
+
* @description
1462
+
*
1463
+
* The default locale of your website/application. This is a required field.
1464
+
*/
1465
+
defaultLocale: string;
1466
+
/**
1467
+
* @docs
1468
+
* @name experimental.i18n.locales
1469
+
* @type {string[]}
1470
+
* @version 3.5.0
1471
+
* @description
1472
+
*
1473
+
* A list of all locales supported by the website (e.g. `['en', 'es', 'pt_BR']`). This list should also include the `defaultLocale`. This is a required field.
1474
+
*
1475
+
* No particular language format or syntax is enforced, but your folder structure must match exactly the locales in the list.
1476
+
*/
1477
+
locales: string[];
1478
+
1479
+
/**
1480
+
* @docs
1481
+
* @name experimental.i18n.fallback
1482
+
* @type {Record<string, string>}
1483
+
* @version 3.5.0
1484
+
* @description
1485
+
*
1486
+
* The fallback strategy when navigating to pages that do not exist (e.g. a translated page has not been created).
1487
+
*
1488
+
* Use this object to declare a fallback `locale` route for each language you support. If no fallback is specified, then unavailable pages will return a 404.
1489
+
*
1490
+
* #### Example
1491
+
*
1492
+
* The following example configures your content fallback strategy to redirect unavailable pages in `/pt/` to their `es` version, and unavailable pages in `/fr/` to their `en` version. Unavailable `/es/` pages will return a 404.
* Available only when `experimental.i18n` enabled and in SSR.
2203
+
*
2204
+
* It represents the list of the preferred locales that are supported by the application. The list is sorted via [quality value].
2205
+
*
2206
+
* For example, given `i18n.locales` equals to `['fr', 'pt', 'de']`, and the `Accept-Language` value equals to `en, de;q=0.2, fr;q=0.6`, the
2207
+
* `Astro.preferredLocaleList` will be equal to `['fs', 'de']` because `en` isn't supported, and `pt` isn't part of the locales contained in the
2208
+
* header.
2209
+
*
2210
+
* When the `Accept-Header` is `*`, the original `i18n.locales` are returned. The value `*` means no preferences, so Astro returns all the supported locales.
0 commit comments