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:
.
โโโ docs
โโโ i18n.json
โโโ package.json
โโโ tsconfig.json
โโโ rspress.config.tsIn this JSON file, you can define the text needed for internationalization, the type definition is as follows:
export interface I18n {
// key: text id
[key: string]: {
// key: language
[key: string]: string;
};
}For example:
{
"gettingStarted": {
"en": "Getting Started",
"zh": "ๅผๅง"
},
"features": {
"en": "Features",
"zh": "็นๆง"
},
"guide": {
"en": "Guide",
"zh": "ๆๅ"
}
}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, you can configure locales data in two places:
locales, used to configure thelang,title,descriptionand other information of the site, mainly around the information of the site itself.themeConfig.locales, used to configure the theme'slang,outline title,previous page/next page textand other information, mainly for theme-related config.
import { defineConfig } from '@rspress/core';
export default defineConfig({
locales: [
{
lang: 'en',
// The label in nav bar to switch language
label: 'English',
title: 'Rspress',
description: 'Static Site Generator',
},
{
lang: 'zh',
label: '็ฎไฝไธญๆ',
title: 'Rspress',
description: '้ๆ็ฝ็ซ็ๆๅจ',
},
],
themeConfig: {
locales: [
{
lang: 'en',
outlineTitle: 'ON THIS Page',
},
{
lang: 'zh',
outlineTitle: 'ๅคง็บฒ',
},
],
},
});
In the default theme, themeConfig.locales also contains all the fields in locales, the former takes precedence.
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:
import { defineConfig } from '@rspress/core';
export default defineConfig({
lang: 'en',
});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:
docs
โโโ en
โ โโโ api
โ โ โโโ index.md
โ โโโ guide
โ โโโ getting-started.md
โ โโโ features.md
โโโ zh
โโโ api
โ โโโ index.md
โโโ guide
โโโ getting-started.md
โโโ features.mdAs 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:
[
{
"text": "guide",
"link": "/guide/start/getting-started",
"activeMatch": "/guide/"
}
]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:
[
{
"type": "dir",
"name": "start",
"label": "gettingStarted"
}
]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:
import { useI18n } from '@rspress/core/runtime';
const MyComponent = () => {
const t = useI18n();
return <div>{t('gettingStarted')}</div>;
};For better type hinting, you can configure paths in tsconfig.json:
{
"compilerOptions": {
"paths": {
"i18n": ["./i18n.json"]
}
}
}Then use it like this in the component:
import { useI18n } from '@rspress/core/runtime';
const MyComponent = () => {
const t = useI18n<typeof import('i18n')>();
return <div>{t('gettingStarted')}</div>;
};This way you get type hints for all literal keys defined in i18n.json.
