Translations & Localization
Docusaurus allows for easy translation functionality using Crowdin. Documentation files written in English are uploaded to Crowdin for translation by users within a community. Top-level pages written with English strings can be translated by wrapping any strings you want to translate in a <translate>
tag. Other titles and labels will also be found and properly translated.
#
Docusaurus Translation ConfigurationsTo generate example files for translations with Docusaurus, run the examples
script with the command line argument translations
:
or
This will create the following files:
- The
pages/en/help-with-translations.js
file includes the same starter help page generated by theexamples
script, but now includes translation tags.
Generally, you will use
help-with-translations.js
as a guide to enable translations in your other pages, but not actually commit the file to your repo (i.e., you can delete it). However, if you want a Help page, and you currently do not have one, you can rename this file tohelp.js
and use it as a starting point.
The
languages.js
file tells Docusaurus what languages you want to enable for your site. By default, we expect English to be enabled.The
crowdin.yaml
file is used to configure Crowdin integration, and is copied up one level into your Docusaurus project repo. If your Docusaurus project resides in/project/website
, thencrowdin.yaml
will be copied to/project/crowdin.yaml
.
#
Translating Your Existing DocsYour documentation files (e.g., the .md
files that live in your docs
directory) do not need to be changed or moved to support translations. They will be uploaded to Crowdin to be translated directly.
#
Enabling Translations on PagesPages allow you to customize layout and specific content of pages like a custom index page or help page.
Pages with text that you want translated should be placed in website/pages/en
directory.
Wrap strings you want translated in a <translate>
tag, and add the following require
statement to the top of the file:
You can also include an optional description attribute to give more context to a translator about how to translate the string:
The
<translate>
tag generally works well on pure strings. If you have a string like "Docusaurus currently provides support to help your website use translations", wrapping the<translation>
tag around that entire string will cause issues because of the markdown linking, etc. Your options are to not translate those strings, or spread a bunch of<translate>
tags amongst the pure substrings of that string.
#
Gathering Strings to TranslateThe strings within localized Pages must be extracted and provided to Crowdin.
Add the following script to your website/package.json
file, if it does not exist already:
Running the script will generate a website/i18n/en.json
file containing all the strings that will be translated from English into other languages.
The script will include text from the following places:
title
andsidebar_label
strings in document markdown headers- category names in
sidebars.json
- tagline in
siteConfig.js
- header link
label
strings insiteConfig.js
- strings wrapped in the
<translate>
tag in any.js
files insidepages
#
Custom Translation StringsIf you want to add additional custom translation strings, or override any of the strings that get produced by the script that creates the website/i18n/en.json
file, you can add a website/data/custom-translation-strings.json
file. The file should have form of:
where localized-strings
represent strings in your documentation content and pages-strings
represents metadata in your documentation (e.g., title, links, etc).
Here is an example:
See the generated website/i18n/en.json
for an example.
#
How Strings Get TranslatedDocusaurus itself does not do any translation from one language to another. Instead, it integrates Crowdin to upload translations and then downloads the appropriately translated files from Crowdin.
#
How Docusaurus Uses String TranslationsThis section provides context about how translations in Docusaurus works.
#
StringsA Docusaurus site has many strings used throughout it that require localization. However, maintaining a list of strings used through out a site can be laborious. Docusaurus simplifies this by centralizing strings.
The header navigation, for example can have links to 'Home' or your 'Blog'. This and other strings found in the headers and sidebars of pages are extracted and placed into i18n/en.json
. When your files are translated, say into Spanish, a i18n/es-ES.json
file will be downloaded from Crowdin. Then, when the Spanish pages are generated, Docusaurus will replace the English version of corresponding strings with translated strings from the corresponding localized strings file (e.g. In a Spanish enabled site 'Help' will become 'Ayuda').
#
Markdown FilesFor documentation files themselves, translated versions of these files are downloaded and then rendered through the proper layout template.
#
Other PagesFor other pages, Docusaurus will automatically transform all <translate>
tags it finds into function calls that return the translated strings from the corresponding localized file locale.json
.
#
CrowdinCrowdin is a company that provides translation services. For Open Source projects, Crowdin provides free string translations.
Create your translation project on Crowdin. You can use Crowdin's guides to learn more about the translations work flow. We suggest that you deselect and do not include "English" as a translatable language to prevent the creation of en-US
localization files as this can lead to confusion.
Ensure in your Crowdin settings, in the Translations section, that "Duplicate Strings" are set to "Hide - all duplicates will share the same translation". This setting will ensure that identical strings between versions share a single translation.
Your project will need a crowdin.yaml
file generated. If you ran yarn examples translations
or npm run examples translations
, this file was created for you on the same level as your website
directory.
You will need to install the
crowdin
command line interface. Please follow the installation directions.
The example below can be automatically generated by the Docusaurus cli with the examples
script. It should be placed in the top level of your project directory to configure how and what files are uploaded/downloaded.
Below is an example Crowdin configuration for the respective languages: German, Spanish, French, Japanese, Korean, Bahasa Indonesia, Portuguese Brazilian, Chinese Simplified, and Chinese Traditional.
You can go here to learn more about customizing your crowdin.yaml
file.
#
Setup the Crowdin ScriptsYou will want to manually sync your files to and from Crowdin. The sync process will upload any markdown files in /docs
as well as translatable strings in website/i18n/en.json
. (These strings can be generated by running yarn write-translations
.)
You can add the following to your package.json
to manually trigger Crowdin.
#
Manual File SyncYou will always want to upload your markdown files and translatable strings first and the download the translations section. So run the commands in this order:
YOUR_CROWDIN_PROJECT_ID
is the name of your Crowdin project. e.g., for https://crowdin.com/project/docusaurus/, that variable would be set todocusaurus
.YOUR_CROWDIN_API_KEY
is a unique key that is like a password. You can find it in theAPI
tab of your Crowdin project'sSettings
.
These commands require having an environment variable set with your Crowdin project id and api key (
CROWDIN_PROJECT_ID
,CROWDIN_API_KEY
). You can preface them inline as done above or add them permanently to your.bashrc
or.bash_profile
.
If you run more than one localized Docusaurus project on your computer, you should change the name of the environment variables to something unique (
CROWDIN_PROJECTNAME_PROJECT_ID
,CROWDIN_PROJECTNAME_API_KEY
).
Since the files are generated, you do not need to have any files in your
website/i18n
orwebsite/translated_docs
directory as part of your repo. So you can can addwebsite/i18n/*
andwebsite/translated_docs
to your.gitignore
file.
#
Automated File Sync Using CircleCIYou can automate pulling down and uploading translations for your files using the CircleCI web continuous integration service.
First, update the .circleci/config.yml
file in your project directory to include steps to upload English files to be translated and download translated files using the Crowdin CLI. Here is an example .circleci/config.yml
file:
The crowdin
command uses the crowdin.yaml
file generated with the examples
script. It should be placed in your project directory to configure how and what files are uploaded/downloaded.
Note that in the crowdin.yaml
file, CROWDIN_PROJECT_ID
and CROWDIN_API_KEY
are environment variables set-up in Circle for your Crowdin project. They can be found in your Crowdin project settings.
Now, Circle will help you automatically get translations prior to building your website. The provided crowdin.yaml
file will copy translated documents into website/translated_docs/
, and translated versions of the i18n/en.json
strings file will into i18n/${language}.json
.
If you wish to use Crowdin on your machine locally, you can install the Crowdin CLI tool and run the same commands found in the circle.yaml
file. The only difference is that you must set project_identifier
and api_key
values in the crowdin.yaml
file since you will not have Circle environment variables set up.
#
Versioned TranslationsIf you wish to have translation and versioning for your documentation, add the following section to the end of your crowdin.yaml
file:
Translated, versioned documents will be copied into website/translated_docs/${language}/${version}/
.