Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move custom translation strings to its own file #775

Merged
merged 3 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/guides-translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ The script will include text from the following places:
* header link `label` strings in `siteConfig.js`
* strings wrapped in the `<translate>` tag in any `.js` files inside `pages`

### Custom Translation Strings

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:

```json
{
"localized-strings": {
"id": "string",
"id2": "string2"
},
"pages-strings" : {
"id3": "string3",
"id4": "string4"
}
}
```

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:

```json
{
"_comment": "This file is used to provide custom strings for translations, including overriding defaults",
"localized-strings": {
"translation": "Translations and Localization"
},
"pages-strings" : {
"Help Translate|recruit community translators for your project": "Help Us Translate"
}
}
```

See the generated `website/i18n/en.json` for an example.

## How Strings Get Translated

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.
Expand Down
12 changes: 6 additions & 6 deletions lib/write-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const babylon = require('babylon');
const traverse = require('babel-traverse').default;
const sidebars = require(CWD + '/sidebars.json');

let currentTranslations = {
let customTranslations = {
'localized-strings': {},
'pages-strings': {},
};
if (fs.existsSync(CWD + '/i18n/en.json')) {
currentTranslations = JSON.parse(
fs.readFileSync(CWD + '/i18n/en.json', 'utf8')
if (fs.existsSync(CWD + '/data/custom-translation-strings.json')) {
customTranslations = JSON.parse(
fs.readFileSync(CWD + '/data/custom-translation-strings.json', 'utf8')
);
}

Expand Down Expand Up @@ -158,11 +158,11 @@ function execute() {
'Translate';
translations['pages-strings'] = Object.assign(
translations['pages-strings'],
currentTranslations['pages-strings']
customTranslations['pages-strings']
);
translations['localized-strings'] = Object.assign(
translations['localized-strings'],
currentTranslations['localized-strings']
customTranslations['localized-strings']
);
writeFileAndCreateFolder(
CWD + '/i18n/en.json',
Expand Down
9 changes: 9 additions & 0 deletions website/data/custom-translation-strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"_comment": "This file is used to provide custom strings for translations, including overriding defaults",
"localized-strings": {
"translation": "Translations and Localization"
},
"pages-strings" : {
"Help Translate|recruit community translators for your project": "Help Us Translate"
}
}