Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lunaria/prepare-json-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function mergeLocale(locale: LocaleObject): Promise<void> {

await fs.writeFile(
path.resolve(`${destFolder}/${locale.code}.json`),
JSON.stringify(source, null, 2),
`${JSON.stringify(source, null, 2)}\n`,
'utf-8',
)
}
24 changes: 21 additions & 3 deletions scripts/compare-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,25 @@ const logSection = (
}

const processLocale = async (
singleLocale: boolean,
localeFile: string,
referenceContent: NestedObject,
fix = false,
): Promise<SyncStats> => {
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)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const targetContent = await loadJson(localeInfo)

const stats: SyncStats = {
Expand All @@ -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')
Comment thread
userquin marked this conversation as resolved.
}

Expand All @@ -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}`,
Expand Down Expand Up @@ -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,
Expand Down
Loading