fix: util translation helper

This commit is contained in:
Gabriellvl 2025-02-28 23:59:04 +01:00
parent 80fa6b9f94
commit a8f6f5f776
4 changed files with 27 additions and 23 deletions

View file

@ -0,0 +1,18 @@
import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
export function loadTranslations<T>(language: string): T {
try {
const filePath = path.join(process.cwd(), '_i18n', `${language}.yml`);
const yamlFile = fs.readFileSync(filePath, 'utf8');
return yaml.load(yamlFile) as T;
} catch (error) {
console.error(
`Cannot load translation for ${language}, fallen back to dutch`
);
console.error(error);
const fallbackPath = path.join(process.cwd(), '_i18n', 'nl.yml');
return yaml.load(fs.readFileSync(fallbackPath, 'utf8')) as T;
}
}