Internationalization
To achieve document internationalization in Rspress, you need to do the following:
- Defines I18n text data.
- Configure
localesandthemeConfig.localesใ - Configure the default language.
- Create documents in different language version.
- Configure sidebar and navbar.
- Use
useI18nin custom components.
Defines I18n text data
Create a new i18n.json in the current workspace, the directory structure is as follows:
In this JSON file, you can define the text needed for internationalization, the type definition is as follows:
For example:
These text data are used in both config file and custom components, which will be introduced in detail later.
Configure locales
In rspress.config.ts, locales is used to configure the lang, title, description and other information of the site, mainly around the information of the site itself.
themeConfig.locales also contains all the fields in locales. However, it will be removed in the future, please use locales.
For other international theme parameters, please refer to API type.
Configure the default language
After configure locales data, you need configure the default language of the document via lang, as shown in the following example:
This is important because for routes in the default language, Rspress will remove the language prefix, such as /en/guide/getting-started will be converted to /guide/getting-started.
Create documents in different language
After the above configuration, we can start to create documents in different language versions. It is very simple. We only need to create the following structure in the document root directory:
As you can see, we put documents in different languages โโin the en and zh directories under the docs directory, so that we can easily distinguish documents in different languages.
Configuring _meta.json
Through the _meta.json file, we can configure the content of the nav bar and sidebar. For details, please refer to Auto Nav/Sidebar.
Navigation bar level
In the _meta.json configuration at the navigation bar level, you can specify text as an i18n key, for example:
Here, text is guide, this value will be automatically translated into ๆๅ or Guide, depending on the current language.
Sidebar level
In the _meta.json configuration at the sidebar level, you can specify label as an i18n key, for example:
Here, label is gettingStarted, this value will be automatically translated into ๅผๅง or Getting Started, depending on the current language.
Use useI18n in custom components
In the process of MDX development or custom theme development, you may write some custom components, which also need to use international text, so how to get it?
Rspress provides useI18n this 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 literal keys defined in i18n.json.