Skip to content

Commit 608e2c8

Browse files
authored
Move custom translation strings to its own file (facebook#775)
1 parent fd9a3ff commit 608e2c8

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

docs/guides-translation.md

+35
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ The script will include text from the following places:
9393
* header link `label` strings in `siteConfig.js`
9494
* strings wrapped in the `<translate>` tag in any `.js` files inside `pages`
9595

96+
### Custom Translation Strings
97+
98+
If 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:
99+
100+
```json
101+
{
102+
"localized-strings": {
103+
"id": "string",
104+
"id2": "string2"
105+
},
106+
"pages-strings" : {
107+
"id3": "string3",
108+
"id4": "string4"
109+
}
110+
}
111+
```
112+
113+
where `localized-strings` represent strings in your documentation content and `pages-strings` represents metadata in your documentation (e.g., title, links, etc).
114+
115+
Here is an example:
116+
117+
```json
118+
{
119+
"_comment": "This file is used to provide custom strings for translations, including overriding defaults",
120+
"localized-strings": {
121+
"translation": "Translations and Localization"
122+
},
123+
"pages-strings" : {
124+
"Help Translate|recruit community translators for your project": "Help Us Translate"
125+
}
126+
}
127+
```
128+
129+
See the generated `website/i18n/en.json` for an example.
130+
96131
## How Strings Get Translated
97132

98133
Docusaurus itself does not do any translation from one language to another. Instead, it integrates [Crowdin](https://crowdin.com/) to upload translations and then downloads the appropriately translated files from Crowdin.

lib/write-translations.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const babylon = require('babylon');
2020
const traverse = require('babel-traverse').default;
2121
const sidebars = require(CWD + '/sidebars.json');
2222

23-
let currentTranslations = {
23+
let customTranslations = {
2424
'localized-strings': {},
2525
'pages-strings': {},
2626
};
27-
if (fs.existsSync(CWD + '/i18n/en.json')) {
28-
currentTranslations = JSON.parse(
29-
fs.readFileSync(CWD + '/i18n/en.json', 'utf8')
27+
if (fs.existsSync(CWD + '/data/custom-translation-strings.json')) {
28+
customTranslations = JSON.parse(
29+
fs.readFileSync(CWD + '/data/custom-translation-strings.json', 'utf8')
3030
);
3131
}
3232

@@ -158,11 +158,11 @@ function execute() {
158158
'Translate';
159159
translations['pages-strings'] = Object.assign(
160160
translations['pages-strings'],
161-
currentTranslations['pages-strings']
161+
customTranslations['pages-strings']
162162
);
163163
translations['localized-strings'] = Object.assign(
164164
translations['localized-strings'],
165-
currentTranslations['localized-strings']
165+
customTranslations['localized-strings']
166166
);
167167
writeFileAndCreateFolder(
168168
CWD + '/i18n/en.json',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"_comment": "This file is used to provide custom strings for translations, including overriding defaults",
3+
"localized-strings": {
4+
"translation": "Translations and Localization"
5+
},
6+
"pages-strings" : {
7+
"Help Translate|recruit community translators for your project": "Help Us Translate"
8+
}
9+
}

0 commit comments

Comments
 (0)