refactor(translations): replace default namespaces with dynamic loading from filesystem

This commit is contained in:
gbuomprisco
2025-06-14 00:00:10 +08:00
parent ab0e1c9948
commit f95da27b8f
2 changed files with 19 additions and 20 deletions

View File

@@ -1,15 +1,6 @@
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
const defaultI18nNamespaces = [
'common',
'auth',
'account',
'teams',
'billing',
'marketing',
];
export type TranslationData = {
[key: string]: string | TranslationData;
};
@@ -28,16 +19,24 @@ export async function loadTranslations() {
for (const locale of locales) {
translations[locale] = {};
for (const namespace of defaultI18nNamespaces) {
const namespaces = readdirSync(join(localesPath, locale)).filter(
(file) => file.endsWith('.json'),
);
for (const namespace of namespaces) {
const namespaceName = namespace.replace('.json', '');
try {
const filePath = join(localesPath, locale, `${namespace}.json`);
const filePath = join(localesPath, locale, namespace);
const content = readFileSync(filePath, 'utf8');
translations[locale][namespace] = JSON.parse(content);
translations[locale][namespaceName] = JSON.parse(content);
} catch (error) {
console.warn(
`Warning: Translation file not found for locale "${locale}" and namespace "${namespace}"`,
`Warning: Translation file not found for locale "${locale}" and namespace "${namespaceName}"`,
);
translations[locale][namespace] = {};
translations[locale][namespaceName] = {};
}
}
}