Internationalization
To internationalize a Rspress documentation site, complete the following steps:
- Define I18n text data.
- Configure the language list,
localesinrspress.config.ts. - Configure the default language,
langinrspress.config.ts. - Create docs for each language.
- Configure sidebar and navbar.
- Use
useI18nin custom components.
Define I18n text data
Create an i18n.json file in the current workspace. The directory structure is:
Define the text required for internationalization in this JSON file. Its type is:
For example:
This text data is used in both the config file and custom components, as explained below.
Configure locales
In rspress.config.ts, locales configures site-level information such as lang, title, and description for each language.
themeConfig.locales also contains all fields from locales, but it will be removed in the future. Use locales instead.
For other internationalized theme options, see API type.
Configure lang default language
After configuring locales, set the default site language with lang:
This is important because Rspress removes the language prefix from routes in the default language. For example, /en/guide/getting-started becomes /guide/getting-started.
Create docs for each language
After the configuration above, create the following structure in the docs root:
Here, docs for different languages live in the en and zh directories under docs, making each language version easy to distinguish.
Configure _nav.json and _meta.json
Use _nav.json and _meta.json to configure the navbar and sidebar. For details, see Autogenerated navigation.
Navigation bar _nav.json
In the navbar-level _nav.json, text can be an i18n key. For example:
Here, text is guide. Rspress translates this value to 指南 or Guide based on i18n.json and the current language.
Sidebar _meta.json
In the sidebar-level _meta.json, label can be an i18n key. For example:
Here, label is gettingStarted. Rspress translates this value to 开始 or Getting Started based on the current language.
Use useI18n in custom components
When writing MDX or developing a custom theme, custom components may also need localized text. Use useI18n to read it:
Rspress provides the useI18n hook to get the internationalized text, the usage is as follows:
For better type hinting, you can configure paths in tsconfig.json:
Then use it like this in the component:
This way you get type hints for all text keys defined in i18n.json.