Skip to content

Commit 9f9c75f

Browse files
authored
chore: prevent adding missing entries to wrong locale files (#1157)
1 parent 16b012a commit 9f9c75f

2 files changed

Lines changed: 22 additions & 4 deletions

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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,25 @@ const logSection = (
209209
}
210210

211211
const processLocale = async (
212+
singleLocale: boolean,
212213
localeFile: string,
213214
referenceContent: NestedObject,
214215
fix = false,
215216
): Promise<SyncStats> => {
216217
const filePath = join(LOCALES_DIRECTORY, localeFile)
217218
const localeInfo = checkJsonName(filePath)
219+
220+
// prevent updating wrong locale file:
221+
// - language locale files at countries allowed: e.g. es.json
222+
// - country locale file forbidden: e.g. es-ES.json
223+
// - target locale file forbidden: e.g. es-419.json
224+
if (fix && localeInfo.mergeLocale && singleLocale) {
225+
console.error(
226+
`${COLORS.red}Error: Locale "${localeInfo.locale}" cannot be fixed, fix the ${localeInfo.lang} locale instead!${COLORS.reset}`,
227+
)
228+
process.exit(1)
229+
}
230+
218231
const targetContent = await loadJson(localeInfo)
219232

220233
const stats: SyncStats = {
@@ -226,7 +239,7 @@ const processLocale = async (
226239
const newContent = syncLocaleData(referenceContent, targetContent, stats, fix)
227240

228241
// Write if there are removals (always) or we are in fix mode
229-
if (stats.extra.length > 0 || fix) {
242+
if (!localeInfo.mergeLocale && (stats.extra.length > 0 || fix)) {
230243
writeFileSync(filePath, JSON.stringify(newContent, null, 2) + '\n', 'utf-8')
231244
}
232245

@@ -246,7 +259,12 @@ const runSingleLocale = async (
246259
process.exit(1)
247260
}
248261

249-
const { missing, extra, referenceKeys } = await processLocale(localeFile, referenceContent, fix)
262+
const { missing, extra, referenceKeys } = await processLocale(
263+
true,
264+
localeFile,
265+
referenceContent,
266+
fix,
267+
)
250268

251269
console.log(
252270
`${COLORS.cyan}=== Missing keys for ${localeFile}${fix ? ' (with --fix)' : ''} ===${COLORS.reset}`,
@@ -286,7 +304,7 @@ const runAllLocales = async (referenceContent: NestedObject, fix = false): Promi
286304
let totalAdded = 0
287305

288306
for (const localeFile of localeFiles) {
289-
const stats = await processLocale(localeFile, referenceContent, fix)
307+
const stats = await processLocale(false, localeFile, referenceContent, fix)
290308
results.push({
291309
file: localeFile,
292310
...stats,

0 commit comments

Comments
 (0)