Files
myeasycms-v2/apps/dev-tool/app/translations/lib/translations-loader.ts
Giancarlo Buomprisco c185bcfa11 2.3.0 Dev Tools (#180)
* 2.3.0 - Added new Dev Tools app
2025-02-21 14:29:42 +08:00

47 lines
1.1 KiB
TypeScript

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;
};
export type Translations = {
[locale: string]: {
[namespace: string]: TranslationData;
};
};
export async function loadTranslations() {
const localesPath = join(process.cwd(), '../web/public/locales');
const locales = readdirSync(localesPath);
const translations: Translations = {};
for (const locale of locales) {
translations[locale] = {};
for (const namespace of defaultI18nNamespaces) {
try {
const filePath = join(localesPath, locale, `${namespace}.json`);
const content = readFileSync(filePath, 'utf8');
translations[locale][namespace] = JSON.parse(content);
} catch (error) {
console.warn(
`Warning: Translation file not found for locale "${locale}" and namespace "${namespace}"`,
);
translations[locale][namespace] = {};
}
}
}
return translations;
}