feat: add translations api for fe

This commit is contained in:
Nybkox
2025-02-20 15:10:22 +01:00
parent ea5e2f660b
commit b7044248cb
7 changed files with 82 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
"@ant-design/colors": "^5.0.0",
"@mdi/font": "^7.1.96",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^13.0.5",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.2.5",

View File

@@ -8,6 +8,7 @@ import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
import css from 'rollup-plugin-css-only';
import json from '@rollup/plugin-json';
const production = !process.env.ROLLUP_WATCH;
@@ -121,6 +122,7 @@ export default [
sourceMap: !production,
inlineSources: !production,
}),
json(),
// In dev mode, call `npm run start` once
// the bundle has been generated

View File

@@ -0,0 +1,43 @@
import enUS from '../../../translations/en-US.json';
import csCZ from '../../../translations/cs-CZ.json';
import { getStringSettingsValue } from './settings/settingsTools';
const translations = {
'en-US': enUS,
'cs-CZ': csCZ,
};
export function getSelectedLanguage(): string {
const borwserLanguage = getBrowserLanguage();
const selectedLanguage = getStringSettingsValue('localization.language', borwserLanguage);
return selectedLanguage;
}
export function getBrowserLanguage(): string {
if (typeof window !== 'undefined') {
return (navigator.languages && navigator.languages[0]) || navigator.language || 'en-US';
}
return 'en-US';
}
type TranslateOptions = {
defaultMessage: string;
values?: Record<string, unknown>;
};
export function _t(key: string, options: TranslateOptions): string {
const { defaultMessage } = options;
const selectedLanguage = getSelectedLanguage();
const selectedTranslations = translations[selectedLanguage] ?? enUS;
const translation = selectedTranslations[key];
if (!translation) {
console.warn(`Translation not found for key: ${key}. For language: ${selectedLanguage}`);
return defaultMessage;
}
return translation;
}

View File

@@ -5,6 +5,7 @@
"exclude": ["node_modules/*", "public/*"],
"compilerOptions": {
"resolveJsonModule": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
@@ -12,7 +13,7 @@
"noImplicitAny": false,
"strictNullChecks": false,
"strict": false,
"target": "es6",
"target": "es6"
// "allowJs": true,
// "checkJs": true,
}