Skip to content

Commit

Permalink
Add English strings if strings are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Sep 4, 2019
1 parent 2baf61d commit 11b96fe
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 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 Down Expand Up @@ -46,12 +46,19 @@ const syncLangStrings = (destDir, sourceLangStringsMap, destLangStringsMap) => {
}
Object.keys(langMap).forEach((key) => {
if (sourceLangStringsMap[lang][key] || newFile) {
// Make sure that the source language contains the string to be translated
// This avoids keeping translations for removed strings
if (destLangStringsMap.en[key]) {
langMap[key].message =
formattingFixer(brandingFixer(sourceLangStringsMap[lang][key]))
}
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]
}
})
// Make sure we have English strings for any string that's missing
Object.keys(destLangStringsMap.en).forEach((key) => {
if (!langMap[key]) {
langMap[key] = destLangStringsMap.en[key]
}
})
const data = JSON.stringify(langMap, null, 2)
Expand Down Expand Up @@ -87,7 +94,7 @@ const syncStrings = (sourceDir, destDir) => {
assertDirExists(destDir)

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

syncDirs(destDir, sourceLangs)
Expand Down

0 comments on commit 11b96fe

Please sign in to comment.