添加国际化功能
在本秘诀中,你将学习如何使用内容集合和动态路由来构建你自己的国际化 (i18n) 解决方案并以不同的语言提供你的内容。
¥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.
此示例在其自己的子路径上为每种语言提供服务,例如 example.com/en/blog
代表英语,example.com/fr/blog
代表法语。
¥This example serves each language at its own subpath, e.g. example.com/en/blog
for English and example.com/fr/blog
for French.
如果你希望默认语言不像其他语言那样在 URL 中可见,则可以使用下面的 隐藏默认语言的说明。
¥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 为每种语言设置页面¥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:Directorysrc/
Directorypages/
Directoryen/
- about.astro
- index.astro
Directoryfr/
- 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.
使用翻译内容的集合
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:Directorysrc/
Directorycontent/
Directoryblog/
Directoryen/ Blog posts in English
- post-1.md
- post-2.md
Directoryfr/ 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.
翻译 UI 字符串
Section titled 翻译 UI 字符串¥Translate UI strings
创建术语词典来翻译站点周围 UI 元素的标签。这使你的访问者可以用他们的语言充分体验你的网站。
¥Create 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
: -
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.
让用户在语言之间切换
Section titled 让用户在语言之间切换¥Let users switch between languages
创建指向你支持的不同语言的链接,以便用户可以选择他们想要使用的语言来阅读你的网站。
¥Create 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:
隐藏 URL 中的默认语言
Section titled 隐藏 URL 中的默认语言¥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/
:Directorysrc/
Directorypages/
- about.astro
- index.astro
Directoryfr/
- 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
翻译每种语言的页面路径。
¥Translate 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
¥Community libraries
-
astro-i18next — 一个 Astro 集成 i18next,包括一些实用程序组件。
-
astro-i18n — Astro 的 TypeScript 优先国际化库。
-
astro-i18n-aut — 一个 Astro 集成 i18n,支持
defaultLocale
而无需生成页面。该集成与适配器和 UI 框架无关。 -
paraglide — 一个完全类型安全的 i18n 库,专为部分水合模式(如 Astro 岛)而设计。