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,
}

4
translations/cs-CZ.json Normal file
View File

@@ -0,0 +1,4 @@
{
"settings.localization": "Localizace"
}

3
translations/en-US.json Normal file
View File

@@ -0,0 +1,3 @@
{
"settings.localization": "Localization"
}

View File

@@ -1827,6 +1827,13 @@
magic-string "^0.25.7"
resolve "^1.17.0"
"@rollup/plugin-json@^6.1.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-6.1.0.tgz#fbe784e29682e9bb6dee28ea75a1a83702e7b805"
integrity sha512-EGI2te5ENk1coGeADSIwZ7G2Q8CJS2sF120T7jLw4xFw9n7wIOXHo+kIYRAoVpJAN+kmqZSoO3Fp4JtoNF4ReA==
dependencies:
"@rollup/pluginutils" "^5.1.0"
"@rollup/plugin-node-resolve@^13.0.5":
version "13.3.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.3.0.tgz#da1c5c5ce8316cef96a2f823d111c1e4e498801c"
@@ -1872,6 +1879,15 @@
estree-walker "^1.0.1"
picomatch "^2.2.2"
"@rollup/pluginutils@^5.1.0":
version "5.1.4"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.1.4.tgz#bb94f1f9eaaac944da237767cdfee6c5b2262d4a"
integrity sha512-USm05zrsFxYLPdWWq+K3STlWiT/3ELn3RcV5hJMghpeAIhxfsUIg6mt12CBJBInWMV4VneoV7SfGv8xIwo2qNQ==
dependencies:
"@types/estree" "^1.0.0"
estree-walker "^2.0.2"
picomatch "^4.0.2"
"@sinclair/typebox@^0.24.1":
version "0.24.51"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f"
@@ -2365,6 +2381,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
"@types/estree@^1.0.0":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.6.tgz#628effeeae2064a1b4e79f78e81d87b7e5fc7b50"
integrity sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==
"@types/fs-extra@^8.0.1":
version "8.1.5"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.1.5.tgz#33aae2962d3b3ec9219b5aca2555ee00274f5927"
@@ -4979,7 +5000,7 @@ estree-walker@^1.0.1:
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==
estree-walker@^2.0.1:
estree-walker@^2.0.1, estree-walker@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==
@@ -9602,6 +9623,11 @@ picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2, picomatch@^2.2.3, picomatc
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
picomatch@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-4.0.2.tgz#77c742931e8f3b8820946c76cd0c1f13730d1dab"
integrity sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==
pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"