Internationalization
Support multiple languages in your documentation
Next Docs has built-in support for internationalized routing in Contentlayer. You can define a list of supported languages and pass it to i18n utilities. Read the Next.js Docs to learn more about implementing I18n in Next.js.
Setup
- Put all supported languages in a file.
- Change your current configurations.
- Create i18n middleware.
- Create a dynamic route, ensure all special files are nested under
/app/[lang]
.
Get Pages
To get the pages with specific languages, use the utilities exported from
source.ts
.
Docs Search
You will need some extra configurations in order to implement international document searching.
The default createSearchAPI
doesn't provide functionality for i18n. Instead,
you can use createI18nSearchAPI
.
- Update the route handler.
- Add
locale
to search dialog, this will only allow pages with specified locale to being searched by the user.
Pages Structure
You can create a page by adding .{locale}
to your MDX file name. Pages can't
be language-specific unless you have created the same page for default locale.
For meta files, you can add -{locale}
to the file name instead
(meta-cn.json
).
If it's the default language, just leave it empty like get-started.mdx
. Do
not use add locale code to file name.
Example
Assume your default language is en
.
Name | |
---|---|
meta.json | Allowed |
meta-cn.json | Allowed |
meta-en.json | Not Allowed |
get-started.mdx | Allowed |
get-started.cn.mdx | Allowed |
get-started.en.mdx | Not Allowed |
components.cn.mdx | Not Allowed |
Last updated on