Skip to content

Commit

Permalink
Only include translated items if they exist in the en destination file
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Sep 4, 2019
1 parent 4dfc702 commit 31e53c1
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions sync-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const assertDirExists = (dir) => {
}
}

const getLangStringsMap = (dir) => {
const getLangStringsMap = (dir, filterLangsFn = filterLangs) => {
const langStringsMap = {}
fs.readdirSync(dir)
.filter(filterLangs)
.filter(filterLangsFn)
.forEach((lang) => {
const langPath = path.join(dir, lang, 'messages.json')
const langMap = JSON.parse(fs.readFileSync(langPath, 'utf8'))
Expand All @@ -26,7 +26,7 @@ const getLangStringsMap = (dir) => {
const filterLangs = (lang) =>
!['index.json', 'en'].includes(lang)

const syncLangStrings = (destDir, sourceLangStringsMap) => {
const syncLangStrings = (destDir, sourceLangStringsMap, destLangStringsMap) => {
fs.readdirSync(destDir)
.filter(filterLangs)
.forEach((lang) => {
Expand All @@ -49,7 +49,21 @@ const syncLangStrings = (destDir, sourceLangStringsMap) => {
langMap[key].message =
formattingFixer(brandingFixer(sourceLangStringsMap[lang][key]))
}
// Make sure that the source language contains the string to be translated
// This avoids keeping translations for removed strings
if (!destLangStringsMap.en[key]) {
delete langMap[key]
}
})
// MetaMask prefers not to have this.
/*
// Make sure we have English strings for any string that's missing
Object.keys(destLangStringsMap.en).forEach((key) => {
if (!langMap[key]) {
langMap[key] = { message: destLangStringsMap.en[key] }
}
})
*/
const data = JSON.stringify(langMap, null, 2)
console.log('writing lang path is: ', langPath)
fs.writeFileSync(langPath, data + '\n', 'utf8')
Expand Down Expand Up @@ -83,10 +97,11 @@ const syncStrings = (sourceDir, destDir) => {
assertDirExists(destDir)

const sourceLangStringsMap = getLangStringsMap(sourceDir)
const destLangStringsMap = getLangStringsMap(destDir, (lang) => lang === 'en')
const sourceLangs = Object.keys(sourceLangStringsMap)

syncDirs(destDir, sourceLangs)
syncLangStrings(destDir, sourceLangStringsMap)
syncLangStrings(destDir, sourceLangStringsMap, destLangStringsMap)

console.log(`Syncing from sourceDir: ${sourceDir} to destDir: ${destDir}`)

Expand Down

0 comments on commit 31e53c1

Please sign in to comment.