-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
Fix conflicting strings issue in translations #917
Changes from 1 commit
9cf0cc8
dae9e44
c608bbc
077f9c8
cf1405c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,7 +35,11 @@ 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`)) { | ||
|
@@ -54,13 +59,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,27 +87,29 @@ 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; | ||
} | ||
} | ||
}); | ||
// 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; | ||
} | ||
}); | ||
|
||
// find sidebar category titles to translate | ||
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 +134,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,9 +184,17 @@ function execute() { | |
translations['pages-strings'], | ||
customTranslations['pages-strings'] | ||
); | ||
translations['localized-strings'] = Object.assign( | ||
translations['localized-strings'], | ||
customTranslations['localized-strings'] | ||
translations['localized-strings'].links = Object.assign( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to simply deepmerge ? IMHO using Object assign for one level deep and deepmerge for > one level deep is not as declarative as simply deepmerging translations['localized-strings'] = deepmerge(
translations['localized-strings'],
customTranslations['localized-strings']
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed that one! |
||
translations['localized-strings'].links, | ||
customTranslations['localized-strings'].links | ||
); | ||
translations['localized-strings'].categories = Object.assign( | ||
translations['localized-strings'].categories, | ||
customTranslations['localized-strings'].categories | ||
); | ||
translations['localized-strings'].docs = deepmerge( | ||
translations['localized-strings'].docs, | ||
customTranslations['localized-strings'].docs | ||
); | ||
writeFileAndCreateFolder( | ||
`${CWD}/i18n/en.json`, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this made me realize that we should create a safe getter to access deeply nested object.
We don't want to keep doing this. Example:
We can create a small helper function (maybe in
core/utils.js
)So we can write
instead of
WDYT ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used almost the same method you used to access a deep property, but modified it so that the function accepts the
path
as a string rather than an array, because most of the libraries out there follow the same pattern.If we need to be more safe then we can move to any library like just-safe-get.