Files
blitzkit/packages/scripts/src/pruneStrings.ts
T

50 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-09-27 18:01:20 -05:00
import { type TranslationTree } from "@blitzkit/i18n";
import locales from "@blitzkit/i18n/locales.json";
import { writeFile } from "fs/promises";
2025-01-17 19:56:46 -06:00
2025-09-27 18:01:20 -05:00
const ROOT = "@blitzkit/i18n/strings";
const WORKING_ROOT = "../../packages/i18n/strings";
2025-01-17 19:56:46 -06:00
2025-09-27 18:01:20 -05:00
const defaultStrings = require(`${ROOT}/${locales.default}.json`).default;
const nonDefaultLocales = locales.supported.filter(
({ locale }) => locale !== locales.default
2025-01-17 19:56:46 -06:00
);
function prune(defaultStrings: TranslationTree, strings: TranslationTree) {
for (const key in strings) {
if (key in defaultStrings) {
if (typeof defaultStrings[key] === typeof strings[key]) {
2025-09-27 18:01:20 -05:00
if (typeof strings[key] === "string") {
2025-01-17 19:56:46 -06:00
if (
2025-01-19 12:46:57 -06:00
// are equivalent regardless of %s flavor
2025-09-27 18:01:20 -05:00
(defaultStrings[key] as string).replaceAll(/%s\d/g, "%s") ===
(strings[key] as string).replaceAll(/%s\d/g, "%s") ||
2025-01-19 12:46:57 -06:00
// string is empty
2025-09-27 18:01:20 -05:00
strings[key] === "" ||
2025-01-19 12:46:57 -06:00
// string has old flavor of %s
strings[key].match(/%s([^\d]|$)/g) !== null
2025-01-17 19:56:46 -06:00
) {
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}...`);
2025-01-19 12:51:26 -06:00
const strings = require(`${ROOT}/${locale}.json`).default;
2025-01-17 19:56:46 -06:00
const pruned = prune(defaultStrings, strings);
writeFile(`${WORKING_ROOT}/${locale}.json`, JSON.stringify(pruned, null, 2));
}