Skip to content

Commit ea7dabe

Browse files
committed
chore: prevent adding missing entries to wrong locale files
1 parent 9867a47 commit ea7dabe

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lunaria/prepare-json-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async function mergeLocale(locale: LocaleObject): Promise<void> {
8888

8989
await fs.writeFile(
9090
path.resolve(`${destFolder}/${locale.code}.json`),
91-
JSON.stringify(source, null, 2),
91+
`${JSON.stringify(source, null, 2)}\n`,
9292
'utf-8',
9393
)
9494
}

scripts/compare-translations.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ const processLocale = async (
215215
): Promise<SyncStats> => {
216216
const filePath = join(LOCALES_DIRECTORY, localeFile)
217217
const localeInfo = checkJsonName(filePath)
218+
219+
if (fix) {
220+
// prevent update
221+
// country locale file: e.g. es or es-ES
222+
if (localeInfo.forCountry) {
223+
// prevent adding missing keys to target locale: e.g. es-ES
224+
// in this case i18n will do the work for us, the target locale should be empty
225+
if (localeInfo.mergeLocale) {
226+
console.error(
227+
`${COLORS.red}Error: Locale "${localeInfo.locale}" cannot be fixed, fix the ${localeInfo.lang} locale instead!${COLORS.reset}`,
228+
)
229+
process.exit(1)
230+
}
231+
}
232+
// prevent fix country locale, i18n will merge lang file and the target locale:
233+
// e.g. es-419: i18n will merge es and es-419 for us
234+
else if (localeInfo.mergeLocale) {
235+
console.error(
236+
`${COLORS.red}Error: Locale "${localeInfo.locale}" cannot be fixed.!${COLORS.reset}`,
237+
)
238+
process.exit(1)
239+
}
240+
}
241+
218242
const targetContent = await loadJson(localeInfo)
219243

220244
const stats: SyncStats = {

0 commit comments

Comments
 (0)