mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 03:29:51 +02:00
feat: prune
This commit is contained in:
1 parent
6725e79021
commit
7027f63236
12 files changed
+5866
-7415
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
DEFAULT_LOCALE,
|
||||
SUPPORTED_LOCALES,
|
||||
TranslationTree,
|
||||
} from '@blitzkit/i18n';
|
||||
import { writeFile } from 'fs/promises';
|
||||
|
||||
const ROOT = '@blitzkit/i18n/strings';
|
||||
const WORKING_ROOT = '../../packages/i18n/strings';
|
||||
|
||||
const defaultStrings = await import(`${ROOT}/${DEFAULT_LOCALE}.json`);
|
||||
const nonDefaultLocales = SUPPORTED_LOCALES.filter(
|
||||
(locale) => locale !== DEFAULT_LOCALE,
|
||||
);
|
||||
|
||||
function prune(defaultStrings: TranslationTree, strings: TranslationTree) {
|
||||
for (const key in strings) {
|
||||
if (key in defaultStrings) {
|
||||
if (typeof defaultStrings[key] === typeof strings[key]) {
|
||||
if (typeof strings[key] === 'string') {
|
||||
if (
|
||||
(defaultStrings[key] as string).replaceAll(/%s\d/g, '%s') ===
|
||||
(strings[key] as string).replaceAll(/%s\d/g, '%s') ||
|
||||
strings[key] === ''
|
||||
) {
|
||||
delete strings[key];
|
||||
}
|
||||
} else {
|
||||
prune(defaultStrings[key] as TranslationTree, strings[key]);
|
||||
}
|
||||
} else {
|
||||
delete strings[key];
|
||||
}
|
||||
} else {
|
||||
delete strings[key];
|
||||
}
|
||||
}
|
||||
|
||||
return strings;
|
||||
}
|
||||
|
||||
for (const locale of nonDefaultLocales) {
|
||||
console.log(`Pruning ${locale}...`);
|
||||
|
||||
const strings = await import(`${ROOT}/${locale}.json`);
|
||||
const pruned = prune(defaultStrings, strings);
|
||||
|
||||
writeFile(`${WORKING_ROOT}/${locale}.json`, JSON.stringify(pruned, null, 2));
|
||||
}
|
||||
Reference in new issue
Block a user