diff --git a/lunaria/prepare-json-files.ts b/lunaria/prepare-json-files.ts index 1011514cb3..127c8aa0fa 100644 --- a/lunaria/prepare-json-files.ts +++ b/lunaria/prepare-json-files.ts @@ -88,7 +88,7 @@ async function mergeLocale(locale: LocaleObject): Promise { await fs.writeFile( path.resolve(`${destFolder}/${locale.code}.json`), - JSON.stringify(source, null, 2), + `${JSON.stringify(source, null, 2)}\n`, 'utf-8', ) } diff --git a/scripts/compare-translations.ts b/scripts/compare-translations.ts index ad4a98acfd..5e95a042b5 100644 --- a/scripts/compare-translations.ts +++ b/scripts/compare-translations.ts @@ -209,12 +209,25 @@ const logSection = ( } const processLocale = async ( + singleLocale: boolean, localeFile: string, referenceContent: NestedObject, fix = false, ): Promise => { const filePath = join(LOCALES_DIRECTORY, localeFile) const localeInfo = checkJsonName(filePath) + + // prevent updating wrong locale file: + // - language locale files at countries allowed: e.g. es.json + // - country locale file forbidden: e.g. es-ES.json + // - target locale file forbidden: e.g. es-419.json + if (fix && localeInfo.mergeLocale && singleLocale) { + console.error( + `${COLORS.red}Error: Locale "${localeInfo.locale}" cannot be fixed, fix the ${localeInfo.lang} locale instead!${COLORS.reset}`, + ) + process.exit(1) + } + const targetContent = await loadJson(localeInfo) const stats: SyncStats = { @@ -226,7 +239,7 @@ const processLocale = async ( const newContent = syncLocaleData(referenceContent, targetContent, stats, fix) // Write if there are removals (always) or we are in fix mode - if (stats.extra.length > 0 || fix) { + if (!localeInfo.mergeLocale && (stats.extra.length > 0 || fix)) { writeFileSync(filePath, JSON.stringify(newContent, null, 2) + '\n', 'utf-8') } @@ -246,7 +259,12 @@ const runSingleLocale = async ( process.exit(1) } - const { missing, extra, referenceKeys } = await processLocale(localeFile, referenceContent, fix) + const { missing, extra, referenceKeys } = await processLocale( + true, + localeFile, + referenceContent, + fix, + ) console.log( `${COLORS.cyan}=== Missing keys for ${localeFile}${fix ? ' (with --fix)' : ''} ===${COLORS.reset}`, @@ -286,7 +304,7 @@ const runAllLocales = async (referenceContent: NestedObject, fix = false): Promi let totalAdded = 0 for (const localeFile of localeFiles) { - const stats = await processLocale(localeFile, referenceContent, fix) + const stats = await processLocale(false, localeFile, referenceContent, fix) results.push({ file: localeFile, ...stats,