committed by
GitHub
parent
59dfc0ad91
commit
c185bcfa11
46
apps/dev-tool/app/translations/lib/translations-loader.ts
Normal file
46
apps/dev-tool/app/translations/lib/translations-loader.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user