diff --git a/sync-strings.js b/sync-strings.js index 47ca48b..74b5c66 100644 --- a/sync-strings.js +++ b/sync-strings.js @@ -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')) @@ -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) @@ -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)