Repository navigation
Description
Docusaurus v2 i18n
Here is a brain dump of many things to consider for i18n support in v2.
I'll keep this issue updated over time, but feel free to comment if you have anything to say, particularly if you used v1 i18n support and can provide valuable feedback.
Superseed this older issue (that still have interesting content): #2651
Existing translation systems
Links to get inspiration from.
Git fork based translations
Have an upstream repo (often in English), and one fork per language
A translation strategy first seen on Vue translation: each language creates a git fork.
We can build tooling on top of that, so that a translation change made in the upstream repo can trigger new PRs on forked repos, to automate the process and ensure translations stay in sync.
Pros:
- work fine, as seen on ReactJS and VueJs docs
- stay in sync with upstream repo
- one repo per lang permit per-language git permissions
- developers don't like to contribute through another new saas tool that they have to learn
- developers like their contributions to appear on github graph etc...
Cons:
- developer-centric workflow
- does it work for small communities?
- maintaining many forks can be overwhelming, can we find an owner for each fork?
- need infrastructure to run the sync bots and open the PRs
- no translation automation tools
ReactJS case
Links related to the work of Nat Alison.
Contains some interesting notes on why a SaaS like Crowdin was not a good fit, despite an attempt to use it.
https://reactjs.org/blog/2019/02/23/is-react-translated-yet.html
reactjs/react.dev#1605
https://github.com/reactjs/reactjs.org-translation
https://github.com/reactjs/reactjs.org-translation/blob/master/PROGRESS.template.md
react/react#8063
reactjs/react.dev#82
reactjs/react.dev#873
GatsbyJS case
Another translation RFC from Nat Alison, quite close to her work on ReactJS:
https://github.com/gatsbyjs/rfcs/blob/master/text/0010-gatsby-docs-localization.md
I don't think this work is in production.
Also some interesting bits on this thread where she explains her unfortunate situation working at Gatsby.
Git, single repo
You have a repo and you just have a folder per language.
Pros:
- simple approach, easy to contribute
- still git-based, developers will like
- single repo
Cons:
- developer-centric
- difficulty to have per language git permissions
- how to stay in sync with "upstream language"
- scalability, can be overwhelming to have many PRs large sites
Nuxt case
The nuxt doc is a simple repo with language folders.
It works fine, but the author told me it was hard to keep all languages in sync. Looks like a manual process.
TypeScript case
Quite similar, TS website has one languages folder per package: <packageName>/copy/<lang> and the translations are handled on the same github monorepo, but split by package
microsoft/TypeScript-Website#100
microsoft/TypeScript-Website#181
Note: Orta found a way to solve the per-language permission problem, as he created a bot so that code owners can self merge through a github PR comment despite not having git permissions:
microsoft/TypeScript-Website#130 (comment)
https://github.com/orta/code-owner-self-merge
Additional notes
I think it's possible to handle the "sync with upstream" problem inside a mono repo by using git patch.
- generate a patch on upstream docs:
git diff origin/master HEAD~100 -- ./website/docs - apply that patch to each translated language (maybe open one PR per language?)
It's a way to emulate the upstream repo -> language forks pattern
SaaS
Using a SaaS like Crowdin / Transifex or others has benefits, like the ability to have advanced translation features (UI, editors supporting various formats (PO, Markdown, ICU key/values), translation memory, automatically pay for platform translators, track translation progress, sync with upstream language, version management...)
Pros:
- Advanced translation features
- Non-developers can use it
- Docusaurus v1 use Crowdin already
Cons:
- Proprietary
- Often paid services
- Need custom scripts to interface with Docusaurus system
- UI not always easy to use
- Developers don't like it much
- Developers contributions are not "visible", lack of incentives
Crowdin
Solution suggested by Docusaurus 1, free plan for open-source, used by Docusaurus site v1, Jest, Yarn, Electron...
We should rather try to make it easy to migrate from v1.
Not everybody like this solution however.
Some drawbacks mentioned here:
https://github.com/gatsbyjs/rfcs/blob/master/text/0010-gatsby-docs-localization.md#saas-platform-crowdin
Note: some questions I have asked to Crowdin here: https://gist.github.com/slorber/30643299196c7efa77084eec10c1c609
Other SaaS
???
Docusaurus 2 translation system
Unlike presented use-cases, we are a framework, not a site, and we don't serve a single community.
I think we want to be able to support both the developers and non-developers.
We can't expect all Docusaurus translators to be developers, nor git users, yet we know that developers don't necessarily always like the lock-in to a SaaS like Crowdin.
Translation management
I think the translation system should be file-system based, as it's probably the common abstraction between git-based workflows and saas-based workflows
Basically, if you build your site for the fr language, and if you have i18n/fr/docs/myDoc.md, then it should be used for the french page instead of the file at docs/myDoc.md.
I think ./i18n is a good default path to put the translated content, but the paths of such system should be flexible enough so that you can adopt the workflow of your choice, but I thin
- git single repo: you can directly version control the content of
./i18n - git multiple forks: maybe you'll just fork the main website, but you could as well fork only the content and use git submodules or whatever...
- saas: each saas will require integration scripts to upload/download the translations from/to the correct paths
So, the first step is to support the first case where you just put the translations in a folder of your site. I'm going to experiment with this on Docusaurus 2 website and try to see if I can provide a french translation.
It's unlikely we'll be able to provide integrations with all the existing translation SaaS, but a 2nd step would be to write integration scripts with Crowdin, so that v1 users can keep using it.
Translation runtime lib
It's likely we'll try to use FBT, a translation tool from Facebook.
I have personally a good experience with React-intl as well and prefer it over many react alternatives.
Translated URLs
Supposing en is the "main" language.
Does https://myDomain.com/en/myDoc exist?
What should be the behavior of the site if the URL does not contain a language, like https://myDomain.com/myDoc ? Is it the English language? Or do we add code to redirect to the most suitable language?
Is it ok for SEO to have a homepage that just redirects? Or is the homepage english? Then which page is the canonical one?
Note: v1 redirects docs, but not the homepage: https://docusaurus.io/ & https://docusaurus.io/docs/installation
Interesting comment (point 5): #2651 (comment)
Let's not forget to add the proper page meta tags such as:
<html lang="en">
<link rel="alternate" href="https://myDomain.com/fr/myDoc" hrefLang="fr-FR"/>
See also #2471
(I think if we have this header in pages, it's not needed to add it in sitemaps)
Translated URL schemes
There are multiple ways to handle the URLs of translated pages
https://fr.myDomain.com/myDoc
Using a custom subdomain seems not a very good fit, as it would require one separate deployment per lang (or you'd need to have some custom reverse proxy logic to handle that?).
I don't think this is the workflow we'll encourage, but we could still support this if people really want it. Maybe with an option like docusaurus build --fr, so that it builds a single language site.
Note: this can't be done on simple hosting solutions like Github Pages
https://myDomain.com/fr/myDoc
I think having a path language prefix is a simpler option, and can be easily done with a single deployment.
There's still a choice to be made here:
- 1: Should we build all languages as a single SPA website?
- 2: Should we build one SPA per language, and append the language prefix as
baseUrl?
Both solutions has cons:
- 1 means the PWA plugin will download all the content of all the languages for offline support (quite overkill)
- 1 means the SPA routes file and other site globals will grow very large and can be a performance problem
- 2 means we'll have to be careful about how the "main sitemap" will have to link to language specific sitemaps
For now I think 2 is a better solution
Details and problems to consider
1 SPA per language, dev experience?
As we have seen above, it may be a good idea for performance to split the site into multiple smaller SPAs.
But this also means that we'll build the SPAs independently, but what would be the dev experience if you run docusaurus start?
Do we code something completely different in dev so that the routes of all languages are accessible as a single SPA? Do we instead provide a docusaurus start --lang fr to only run the "french SPA"? I think it's an acceptable tradeoff and have some advantages, but can also be annoying for some users.
Anchor links
Auto-generated ids are a problem for anchor links.
As a translator change a heading of some translated markdown file, the id changes, and links from other files do change as well. We should provide an easy way to make the anchors stable across translations
reactjs/react.dev#1605 (comment)
reactjs/react.dev#1605 (comment)
ethereum/ethereum-org-website#272
https://github.com/reactjs/reactjs.org/pull/1636/files
mdx-js/mdx#810
Right-to-Left support
Support RTL in themes?
Plugin integration
TODO
Doc edit button
If the user is browsing a french doc, and press "edit", he should rather open the correct URL (git or crowdin), so we should make this configurable.
Related:
#648
Default language
We should not assume english will be the default language, like in v1.
Scalability
The build time mostly depends on 3 factors:
- number of docs
- number of versions
- number of languages
To decrease build time and make it sustainable, you can remove older versions from the SPA part, and make them available as a standalone, single version deployment.
We'll work on a cli feature to "archive" older versions more easily: #3286
Fallback
A missing page/translation should be allowed, in such case we'd fallback to the default language and could show a warning
See 6: #2651 (comment)
Creating a language
We need a cli to init a language folder based on current language/versions
Creating a version
See proposal here: #2651 (comment)
We'll have to snapshot each localized folder too
Asset colocation
It's possible to colocate assets close to the docs. Somehow it permits to use a different image per version. What's the story for i18n? This colocated image would likely end up being copied in the language folders too, so it might be duplicated on multiple axis (version/lang). Is it a good thing? At the same time, if an image contains text, that text could be translated differently so it still makes sense...
Slugs
Should we allow to create custom slugs per language?
If we do that, to be able to switch from one lang to the other without loosing context (the doc you are currently reading), one version would have to be aware of the slugs of all the other language versions, which might be quite a lot of data. How do we access such data in a performant way?
To me, it does not look so critical to be able to switch language and preserving context. If the user wants to browse docs in french, he can go through the french home and browse from there, and it's likely google gives him the docs in the correct language in the first place.
We should try to find a solution though, but this can probably be done later, with some code that would, on language switch request, read some json file emitted by the other language, and then obtain a mapping from document id to slug of the other language.
Note: Yarn 1/classic (Jekyll based?) can switch language and preserve context when doing so, but the slugs are not localized: https://classic.yarnpkg.com/es-ES/docs/usage
Translation mode
If you add the ?translate=true querystring, it could enhance the UI so that we add in-place translation features.
It could be possible to integrate with the translation API of a SaaS like crowdin.
This is mostly for key/value translations, as markdown docs will be translated as a whole and there's already the editUrl on the docs plugin.
TODO ...
Ongoing PR: #3325
Activity
slorber commentedon Oct 14, 2020
Worth studying the NextJS i18n routing RFC: vercel/next.js#17078
clairefro commentedon Nov 3, 2020
Hi there, eavesdropping as I've also been grappling translation approaches for another project
Regarding translation management in a single repo scenario, are you aware of git localize? A system that incorporates or mimics this could make for happy devs
https://gitlocalize.com/
slorber commentedon Nov 3, 2020
thanks @clairefro , didn't know about this one, will take a look :)
slorber commentedon Dec 10, 2020
Here are some news about i18n support.
You'll find the i18n RFC here: #3317
The i18n core PR has already been merged but it is not officially released yet.
#3325
However, can test it using the
@canarynpm dist tag (yarn add @docusaurus/core@canaryetc) and reading some instructions in that PR.We are in the dogfooding phase to see if the i18n API and system works fine, and if we need some breaking changes.
We dogfood this on 2 sites:
frsubpath with some basic translation: https://v2.docusaurus.io/fr)When tests are ok for these 2 sites, we'll release i18n with proper documentation, hopefully before the end of the year.
The Jest v2 + i18n migration is in progress and can be tracked here: jest-website-migration/jest#2
tomchen commentedon Jan 5, 2021
Hello, good job, I'm currently using it. And here are my suggestions:
I can add
classNameto any othernavbaritemsbut{type: 'localeDropdown'}. Please supportclassNameso user can more easily write CSS for it.{type: 'localeDropdown'}should has an icon by default, just like v1 where it hasbtw, in case anyone wants, currently I use something like this in Sass to insert the aforementioned lang icon (click to show)
<head>for better SEO:For
langglobal attribute (<html lang="">), you currently trim or simplifyzh-Hant/zh-Hanstozh, orpt-BRtopt, etc. You should not. They are valid lang tags per iana assignment (this one is the real official list according to w3c).Changing
zh-Hant/zh-Hanstozhactually causes font rendering problems (depending on your OS and your OS configurations,<html lang="zh-Hant">pages could use fonts designed for zh-Hant, but<html lang="zh">pages could default to zh-Hans fonts)You said in feat(v2): core v2 i18n support + Docusaurus site Crowdin integration #3325 the command is
docusaurus write-translations --locales all, but:First, it's actually
--locale, without 's'. It's not only the text in PR #3325 that's wrong, I saw warnings in terminal likeAvailable locales=, so you might need to check everything in the code, replacinglocalesbylocale.Second, I run
docusaurus write-translations --locale all, it shows:Well, I don't quite understand. I have the locale list in my docusaurus.config.js.
docusaurus.config.js (click to show)
I had to
docusaurus write-translations --locale=enand manually copy them into other lang folders. And it works fine.This one is just a question, not really an issue or a suggesetion: how to check / get the language of the current page, or, how to modify (e.g. insert tags into the
<head>) all pages from a specific language?slorber commentedon Jan 5, 2021
Thanks for the feedback @tomchen
Agree
Agree
About these meta tags, I didn't think they were 100% required for the initial i18n release (as v1 does not have them).
I'm still trying to figure some things out.
What I understand is that we can't simply put the "root" of the localized site here but need to link to the exact same page in the localized site.
And then we also want at the same time to localize the slugs (probably for SEO reasons too), but this complicates things as now
/helloneed to know about the french URL/bonjour, which makes things more complicated.My idea was to create a
/hellopage (the original slug) on the localized sites, and redirects to/bonjourwith JS, but not sure how happy google will be with that client-side redirectThanks, didn't know, will see what I can do.
The PR might have been changed a bit and the PR doc is not up to date. The up to date doc is the cli --help flag.
Currently writing the i18n doc, will make sure the published doc is correct regarding this.
I removed the
--locales alloption as I thought it was a bit messy and overkill (as it's possible to run manually the cli command for each locale one after the other).Do you have a usecase for it? Is your site repo public?
You can access the i18n config and current locale using the
useDocusaurusContext()hook.You can wrap your site by using a custom
Rootcomponent:https://v2.docusaurus.io/docs/next/using-themes/#wrapper-your-site-with-root
Or by using a custom
Layoutcomponent:https://v2.docusaurus.io/docs/next/using-themes/#for-site-owners
This gives you the opportunity to use a
<Head>(react-helmet) to add additional metadatas on a per-locale basis:https://v2.docusaurus.io/docs/next/docusaurus-core#head
This is not the most convenient API to do that.
I'd like to make the current locale available in the configuration file, just not sure how to do this properly yet.
Also we currently build one SPA per locale instead of one SPA for all locales.
I'd like to avoid creating an API surface that would prevent us in the future to build all localized sites as a single SPA.
In general, the initial i18n release will not be exhaustive.
It is more the core system we will build on top, and I'd like to avoid rushing creating a hasty API surface, but rather gather some initial feedback and properly design the issues users have
thibaudcolas commentedon Jan 5, 2021
It’s great to see the progress on this – just chiming in with a few things I think I can help with.
Yes, each page should have bidirectional links to its equivalent in other languages there are translations for. In their official guidance, Google emphasizes it’s important for the links to be bidirectional at least between the "main" language of each page, and its translations (but links between different translations – not as much).
For the validity of
hreflang– there is no need to localize the slugs. From my experience I don’t see this done very often if at all, as it leads to the complexities you mention when querying pages by slug. So/fr/hellois perfectly fine. Looking around, I couldn’t find examples of localized slugs on the multilingual sites I’ve been involved with recently.slorber commentedon Jan 5, 2021
Thanks @thibaudcolas
Also something to consider is that a Docusaurus site may decide to exclude some parts (like the blog) in localized sites. This is also the reason nextjs does not include hreflang headers by default (vercel/next.js#17078)
In any case, it's possible for you to insert the hreflang by creating your own
<Root>component, including the conditional logic of your choice.So I guess we could simply assume that we could generate hreflang for all languages and all pages of the site, assuming slugs won't be translated, and site translations will be "complete".
But will leave an option to disable this hreflang generation in case user wants to translate slugs or create partially translated sites, in such case he'll have to figure out how to generate the appropriate headers himself.
tomchen commentedon Jan 5, 2021
@slorber No I don't have a usecase for
--locale all. Not having--locale allis fine for me. I use docusaurus for some small personal py/js library documentation and hobby websites, my usecases are perhaps the simplest. I didn't even look at Crowdin and other i18n tools because I don't think I need them - I just generatei18n/<LANG>/and edit these current.json files.What makes my usecase a little special is I have zh-Hans (Simplified Chinese) and zh-Hant (Traditional Chinese), I even wrote a simple script to convert everthing in i18n/zh-Hans/ to i18n/zh-Hant/ before building). It's nice to write in a variant and automatically generate another variant. But a small SEO concern is that Google might think zh-Hans and zh-Hant pages are duplicate content, so, declaring
<link rel="alternate" hreflang="" />in<head>, correct lang code in<html lang="zh-Hant">, and optionally inserting a<link rel="canonical" href="<zh-Hans_URL>" />on zh-Hant pages, these might be important here.(Ideally the zh-Hans->zh-Hant conversion, or even any page creation from user data, should be done programmatically when building, but it seems currently users can't do this very easily in Docusaurus like we do it in Gatsby. And for a small website, there's nothing wrong in not doing it programmatically when building, but auto-generating lots of .md files from the data)
Oh, there's another i18n issue I forgot to mention: dropdown menu items in the top nav can be exported into current.json files using
docusaurus write-translations --locale <LANG>, but after you translate them in current.json files, you will find they are still in default language and not correctly localized on the website. Non-dropdown top nav items work fine.slorber commentedon Jan 15, 2021
Going to close this now but feel free to continue the discussions on i18n and providing feedback.
I'll handle those feedbacks very soon @tomchen.
Btw the translation of the theme is not yet 100% exhaustive, I mostly translated the low hanging fruits first but remains some small, less visible labels.
I've created an issue for any discussion related to Crowdin + Docusaurus here: #4052
1 remaining item
hyochan commentedon Feb 7, 2021
Really thanks for this! Is there any goal to use
FBToptionally?slorber commentedon Feb 8, 2021
Thanks @hyochan . We don't plan to have first-class support for FBT, but I clearly want to make the integration possible. That goal will be reached when we'd be able to integrate FBT demos directly in its documentation here: https://facebook.github.io/fbt/
We probably need new lifecycle hooks for that (that will also unlock some CSS-in-JS integrations)
renatodex commentedon Feb 16, 2021
Just heard that the Docusaurus team got some progress with the i18n feature!
That's awesome! I'm the owner of Fables & Goblins, an Open Source RPG System oriented in a Goblin world.
And we are using Docusaurus 2.0 to host the Online Book documentation:
htttps://www.fabulasegoblins.com.br
Currently, it's entirely written in Brazilian Portuguese, but I would like to try the latest non-stable version to see if I could start to add English as an option.
Do you guys think it's reasonably safe to try?
Or would you recommend waiting for a few weeks/months?
slorber commentedon Feb 16, 2021
@renatodex we'd appreciate if you try it and give us feedback, but in my opinion, it's already working fine and you should try it. Just use the
@canarynpm dist tagrenatodex commentedon Feb 16, 2021
Thank you @slorber, I really appreciate all of this awesome work!
mem212 commentedon Nov 22, 2021
But if you have only one local ( en ) , docusaurus will generate hreflang tags for both x-default and en with the same url AND the canonical tag with the same url also . This can be considered as a conflict on term of SEO and make search engines like Google ignore the doc.
slorber commentedon Dec 8, 2021
@MxhDx I'm open to suggestion and not an ultimate i18n/SEO expert, however it's important for me that you link to some authority website (like Google SEO documentation) to back your claims.
I think it's reasonable to remove hreflang headers for sites using a single locale, but where have you read that not doing so leads to search engines ignoring the docs?
thadguidry commentedon Dec 8, 2021
https://moz.com/learn/seo/hreflang-tag
https://developers.google.com/search/docs/advanced/crawling/localized-versions
slorber commentedon Dec 9, 2021
Thanks
@MxhDx @thadguidry I created a dedicated issue for these SEO problems: #6075
I read those pages when I implemented i18n but it's not so easy to interpret for me.
Please help me figure out the bad behaviors of Docusaurus with more concrete examples.
I don't remember reading anywhere that using
x-default+hreflang enon the same page can lead to Google not indexing a page 🤷♂️ But I do agree it may be un-necessarily pre-emptive for unlocalized sites.thadguidry commentedon Dec 9, 2021
@slorber oh I was only giving that context and not making a judgement one way or another. If I were to make a judgement then I would say you are right that it is probably unnecessary but not harmful. That's what Google's docs are alluding to.
jovezhong commentedon Nov 7, 2022
Is that possible to load different font for different locale?
For example, by default I'd like to use
Interfont, when the user switched to Chinese locale, I'd like to use a different font, saynoto-sans-scI cannot find a proper way to do so. I run
yarn add @fontsource/interand set--ifm-font-family-base: 'Inter';in custom.css. Alsoimport "@fontsource/inter";in pages/index.jsIt'll be great to have per locale font settings
slorber commentedon Nov 9, 2022
@jovezhong it's not possible, but indeed it makes sense to have per-locale stylesheets in general (allowing you to load fonts and define per-locale CSS variables).
I think I'll provide an API so that you can customize your site config on a per locale basis: this solves many use-cases we have like translating site titles etc... The bad news is that such an API is hard to design correctly, so it will likely be an experimental API that will be used as a temporary workaround
jovezhong commentedon Nov 9, 2022
Thanks @slorber
Look forwards to such experimental API. Yes, it'll be nice to customize the title/tagline per locale.
Sorry I am not familiar with how docusaurus works. Is there a javascript file each page will load? Maybe I can import the custom font in that JS per locale and set font for
--ifm-font-family-baseThis is not a blocking issue for me.
slorber commentedon Nov 10, 2022
The way I see it is to enable config to receive the locale being currently built so you can register a per-locale stylesheet:
This is a bit weird as an API unfortunately because we would have to run this function once initially with
currentLocale: undefinedjust to be able to retrieve the list of locales your site supports.slorber commentedon Jul 28, 2025
In v3.9, Docusauruus will let you configure
urlandbaseUrlon a per locale basis, giving you more flexibility to host your site in fancy ways.#11316
This makes it possible to keep the
/<locale>/prefix for the default locale, instead of hosting it at the root/baseUrl.This also permits us to fix issues with multi-domain deployments where the navbar locale dropdown would not link to the correct alternative page if hosted on a distinct domain. Also, the HTML
hreflangmetadata will now link to the right alternative pages. This requires providing aurlattribute for each locale.