Add i18n features
本頁內容尚未翻譯。
In this recipe, you will learn how to use content collections and dynamic routing to build your own internationalization (i18n) solution and serve your content in different languages.
In v4.0, Astro added built-in support for i18n routing that allows you to configure default and supported languages and includes valuable helper functions to assist you in serving an international audience. If you want to use this instead, see our internationalization guide to learn about these features.
This example serves each language at its own subpath, e.g. example.com/en/blog
for English and example.com/fr/blog
for French.
If you prefer the default language to not be visible in the URL unlike other languages, there are instructions to hide the default language below.
Recipe
Section titled RecipeSet up pages for each language
Section titled Set up pages for each language-
Create a directory for each language you want to support. For example,
en/
andfr/
if you are supporting English and French:目錄src/
目錄pages/
目錄en/
- about.astro
- index.astro
目錄fr/
- about.astro
- index.astro
- index.astro
-
Set up
src/pages/index.astro
to redirect to your default language.This approach uses a meta refresh and will work however you deploy your site. Some static hosts also let you configure server redirects with a custom configuration file. See your deploy platform’s documentation for more details.
If you are using an SSR adapter, you can use
Astro.redirect
to redirect to the default language on the server.
Use collections for translated content
Section titled Use collections for translated content-
Create a folder in
src/content/
for each type of content you want to include and add subdirectories for each supported language. For example, to support English and French blog posts:目錄src/
目錄content/
目錄blog/
目錄en/ Blog posts in English
- post-1.md
- post-2.md
目錄fr/ Blog posts in French
- post-1.md
- post-2.md
-
Create a
src/content/config.ts
file and export a collection for each type of content.Read more about Content Collections. -
Use dynamic routes to fetch and render content based on a
lang
and aslug
parameter.In static rendering mode, use
getStaticPaths
to map each content entry to a page:In SSR mode, fetch the requested entry directly:
Read more about dynamic routing.The example above uses the built-in
toLocaleString()
date-formatting method to create a human-readable string from the frontmatter date. This ensures the date and time are formatted to match the user’s language.
Translate UI strings
Section titled Translate UI stringsCreate dictionaries of terms to translate the labels for UI elements around your site. This allows your visitors to experience your site fully in their language.
-
Create a
src/i18n/ui.ts
file to store your translation strings: -
Create two helper functions: one to detect the page language based on the current URL, and one to get translations strings for different parts of the UI in
src/i18n/utils.ts
:In step 1, the
nav.twitter
string was not translated to French. You may not want every term translated, such as proper names or common industry terms. TheuseTranslations
helper will return the default language’s value if a key is not translated. In this example, French users will also see “Twitter” in the nav bar. -
Import the helpers where needed and use them to choose the UI string that corresponds to the current language. For example, a nav component might look like:
-
Each page must have a
lang
attribute on the<html>
element that matches the language on the page. In this example, a reusable layout extracts the language from the current route:You can then use this base layout to ensure that pages use the correct
lang
attribute automatically.
Let users switch between languages
Section titled Let users switch between languagesCreate links to the different languages you support so users can choose the language they want to read your site in.
-
Create a component to show a link for each language:
-
Add
<LanguagePicker />
to your site so it is shown on every page. The example below adds it to the site footer in a base layout:
Hide default language in the URL
Section titled Hide default language in the URL-
Create a directory for each language except the default language. For example, store your default language pages directly in
pages/
, and your translated pages infr/
:目錄src/
目錄pages/
- about.astro
- index.astro
目錄fr/
- about.astro
- index.astro
-
Add another line to the
src/i18n/ui.ts
file to toggle the feature: -
Add a helper function to
src/i18n/utils.ts
, to translate paths based on the current language: -
Import the helper where needed. For example, a
nav
component might look like: -
The helper function can also be used to translate paths for a specific language. For example, when users switch between languages:
Translate Routes
Section titled Translate RoutesTranslate the routes of your pages for each language.
-
Add route mappings to
src/i18n/ui.ts
: -
Update the
useTranslatedPath
helper function insrc/i18n/utils.ts
to add router translation logic. -
Create a helper function to get the route, if it exists based on the current URL, in
src/i18n/utils.ts
: -
The helper function can be used to get a translated route. For example, when no translated route is defined, the user will be redirected to the home page:
Resources
Section titled ResourcesCommunity libraries
Section titled Community libraries- astro-i18next — An Astro integration for i18next including some utility components.
- astro-i18n — A TypeScript-first internationalization library for Astro.
- astro-i18n-aut — An Astro integration for i18n that supports the
defaultLocale
without page generation. The integration is adapter agnostic and UI framework agnostic. - astro-react-i18next — An Astro integration that seamlessly enables the use of i18next and react-i18next in React components on Astro websites.
- paraglide — A fully type-safe i18n library specifically designed for partial hydration patterns like Astro islands.