mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 10:52:35 +02:00
Fix: conflicting strings issue in translations (#917)
* Fix conflicting strings issue in translations * Preserve structure of `customTranslations` * Use `deepmerge` to merge whole of `localized-strings` * Simplify and make deep property access on an object safe * Fix deep property accessor and rename it to idx
This commit is contained in:
parent
d18b09954b
commit
cfabaedc99
12 changed files with 94 additions and 66 deletions
|
@ -26,6 +26,7 @@ const fs = require('fs-extra');
|
|||
const glob = require('glob');
|
||||
const mkdirp = require('mkdirp');
|
||||
const nodePath = require('path');
|
||||
const deepmerge = require('deepmerge');
|
||||
|
||||
const readMetadata = require('./server/readMetadata.js');
|
||||
|
||||
|
@ -34,12 +35,19 @@ const siteConfig = require(`${CWD}/siteConfig.js`);
|
|||
const sidebars = require(`${CWD}/sidebars.json`);
|
||||
|
||||
let customTranslations = {
|
||||
'localized-strings': {},
|
||||
'localized-strings': {
|
||||
docs: {},
|
||||
links: {},
|
||||
categories: {},
|
||||
},
|
||||
'pages-strings': {},
|
||||
};
|
||||
if (fs.existsSync(`${CWD}/data/custom-translation-strings.json`)) {
|
||||
customTranslations = JSON.parse(
|
||||
fs.readFileSync(`${CWD}/data/custom-translation-strings.json`, 'utf8')
|
||||
customTranslations = deepmerge(
|
||||
JSON.parse(
|
||||
fs.readFileSync(`${CWD}/data/custom-translation-strings.json`, 'utf8')
|
||||
),
|
||||
customTranslations
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -54,13 +62,20 @@ function execute() {
|
|||
next: 'Next',
|
||||
previous: 'Previous',
|
||||
tagline: siteConfig.tagline,
|
||||
docs: {},
|
||||
links: {},
|
||||
categories: {},
|
||||
},
|
||||
'pages-strings': {},
|
||||
};
|
||||
|
||||
// look through markdown headers of docs for titles and categories to translate
|
||||
const docsDir = nodePath.join(CWD, '../', readMetadata.getDocsPath());
|
||||
let files = glob.sync(`${CWD}/../${readMetadata.getDocsPath()}/**`);
|
||||
const versionedDocsDir = nodePath.join(CWD, 'versioned_docs');
|
||||
let files = [
|
||||
...glob.sync(`${docsDir}/**`),
|
||||
...glob.sync(`${versionedDocsDir}/**`),
|
||||
];
|
||||
files.forEach(file => {
|
||||
const extension = nodePath.extname(file);
|
||||
if (extension === '.md' || extension === '.markdown') {
|
||||
|
@ -75,11 +90,13 @@ function execute() {
|
|||
return;
|
||||
}
|
||||
const metadata = res.metadata;
|
||||
const id = metadata.localized_id;
|
||||
|
||||
translations['localized-strings'][metadata.localized_id] = metadata.title;
|
||||
translations['localized-strings'].docs[id] = {};
|
||||
translations['localized-strings'].docs[id].title = metadata.title;
|
||||
|
||||
if (metadata.sidebar_label) {
|
||||
translations['localized-strings'][metadata.sidebar_label] =
|
||||
translations['localized-strings'].docs[id].sidebar_label =
|
||||
metadata.sidebar_label;
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +104,7 @@ function execute() {
|
|||
// look through header links for text to translate
|
||||
siteConfig.headerLinks.forEach(link => {
|
||||
if (link.label) {
|
||||
translations['localized-strings'][link.label] = link.label;
|
||||
translations['localized-strings'].links[link.label] = link.label;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -95,7 +112,7 @@ function execute() {
|
|||
Object.keys(sidebars).forEach(sb => {
|
||||
const categories = sidebars[sb];
|
||||
Object.keys(categories).forEach(category => {
|
||||
translations['localized-strings'][category] = category;
|
||||
translations['localized-strings'].categories[category] = category;
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -120,7 +137,7 @@ function execute() {
|
|||
Object.keys(sidebarContent).forEach(sb => {
|
||||
const categories = sidebarContent[sb];
|
||||
Object.keys(categories).forEach(category => {
|
||||
translations['localized-strings'][category] = category;
|
||||
translations['localized-strings'].categories[category] = category;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -170,7 +187,7 @@ function execute() {
|
|||
translations['pages-strings'],
|
||||
customTranslations['pages-strings']
|
||||
);
|
||||
translations['localized-strings'] = Object.assign(
|
||||
translations['localized-strings'] = deepmerge(
|
||||
translations['localized-strings'],
|
||||
customTranslations['localized-strings']
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue