feat: compile messages

This commit is contained in:
Nybkox
2025-02-27 13:35:40 +01:00
parent 82eabc41fe
commit 138fadf672
3 changed files with 83 additions and 8 deletions

View File

@@ -58,6 +58,7 @@
"uuid": "^3.4.0" "uuid": "^3.4.0"
}, },
"dependencies": { "dependencies": {
"@messageformat/core": "^3.4.0",
"chartjs-plugin-zoom": "^1.2.0", "chartjs-plugin-zoom": "^1.2.0",
"date-fns": "^4.1.0", "date-fns": "^4.1.0",
"debug": "^4.3.4", "debug": "^4.3.4",

View File

@@ -1,16 +1,23 @@
import enUS from '../../../translations/en-US.json'; import enUS from '../../../translations/en-US.json';
import csCZ from '../../../translations/cs-CZ.json'; import csCZ from '../../../translations/cs-CZ.json';
import MessageFormat, { MessageFunction } from '@messageformat/core';
import { getStringSettingsValue } from './settings/settingsTools'; import { getStringSettingsValue } from './settings/settingsTools';
const translations = { const translations = {
'en-US': enUS, 'en-US': enUS,
'cs-CZ': csCZ, 'cs-CZ': csCZ,
}; };
const supportedLanguages = Object.keys(translations);
const compiledMessages: Partial<Record<string, Record<string, MessageFunction<'string'>>>> = {};
export function getSelectedLanguage(): string { export function getSelectedLanguage(): string {
const borwserLanguage = getBrowserLanguage(); const borwserLanguage = getBrowserLanguage();
const selectedLanguage = getStringSettingsValue('localization.language', borwserLanguage); const selectedLanguage = getStringSettingsValue('localization.language', borwserLanguage);
if (!supportedLanguages.includes(selectedLanguage)) return 'en-US';
return selectedLanguage; return selectedLanguage;
} }
@@ -26,18 +33,34 @@ type TranslateOptions = {
values?: Record<string, unknown>; values?: Record<string, unknown>;
}; };
export function _t(key: string, options: TranslateOptions): string { function getTranslation(key: string, defaultMessage: string, language: string) {
const { defaultMessage } = options; const selectedTranslations = translations[language] ?? enUS;
const selectedLanguage = getSelectedLanguage();
const selectedTranslations = translations[selectedLanguage] ?? enUS;
const translation = selectedTranslations[key]; const translation = selectedTranslations[key];
if (!translation) { if (!translation) {
console.warn(`Translation not found for key: ${key}. For language: ${selectedLanguage}`); console.warn(`Translation not found for key: ${key}. For language: ${language}`);
return defaultMessage; return defaultMessage;
} }
return translation; return translation;
} }
export function _t(key: string, options: TranslateOptions): string {
const { defaultMessage, values } = options;
const selectedLanguage = getSelectedLanguage();
if (!compiledMessages[selectedLanguage]) {
compiledMessages[selectedLanguage] = {};
}
if (!compiledMessages[selectedLanguage][key]) {
const translation = getTranslation(key, defaultMessage, selectedLanguage);
const complied = new MessageFormat(selectedLanguage).compile(translation);
compiledMessages[selectedLanguage][key] = complied;
}
const compliledTranslation = compiledMessages[selectedLanguage][key];
return compliledTranslation(values ?? {});
}

View File

@@ -1708,6 +1708,42 @@
resolved "https://registry.yarnpkg.com/@mdi/font/-/font-7.4.47.tgz#2ae522867da3a5c88b738d54b403eb91471903af" resolved "https://registry.yarnpkg.com/@mdi/font/-/font-7.4.47.tgz#2ae522867da3a5c88b738d54b403eb91471903af"
integrity sha512-43MtGpd585SNzHZPcYowu/84Vz2a2g31TvPMTm9uTiCSWzaheQySUcSyUH/46fPnuPQWof2yd0pGBtzee/IQWw== integrity sha512-43MtGpd585SNzHZPcYowu/84Vz2a2g31TvPMTm9uTiCSWzaheQySUcSyUH/46fPnuPQWof2yd0pGBtzee/IQWw==
"@messageformat/core@^3.4.0":
version "3.4.0"
resolved "https://registry.yarnpkg.com/@messageformat/core/-/core-3.4.0.tgz#2814c23383dec7bddf535d54f2a03e410165ca9f"
integrity sha512-NgCFubFFIdMWJGN5WuQhHCNmzk7QgiVfrViFxcS99j7F5dDS5EP6raR54I+2ydhe4+5/XTn/YIEppFaqqVWHsw==
dependencies:
"@messageformat/date-skeleton" "^1.0.0"
"@messageformat/number-skeleton" "^1.0.0"
"@messageformat/parser" "^5.1.0"
"@messageformat/runtime" "^3.0.1"
make-plural "^7.0.0"
safe-identifier "^0.4.1"
"@messageformat/date-skeleton@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@messageformat/date-skeleton/-/date-skeleton-1.1.0.tgz#3bad068cbf5873d14592cfc7a73dd4d8615e2739"
integrity sha512-rmGAfB1tIPER+gh3p/RgA+PVeRE/gxuQ2w4snFWPF5xtb5mbWR7Cbw7wCOftcUypbD6HVoxrVdyyghPm3WzP5A==
"@messageformat/number-skeleton@^1.0.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@messageformat/number-skeleton/-/number-skeleton-1.2.0.tgz#e7c245c41a1b2722bc59dad68f4d454f761bc9b4"
integrity sha512-xsgwcL7J7WhlHJ3RNbaVgssaIwcEyFkBqxHdcdaiJzwTZAWEOD8BuUFxnxV9k5S0qHN3v/KzUpq0IUpjH1seRg==
"@messageformat/parser@^5.1.0":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@messageformat/parser/-/parser-5.1.1.tgz#ca7d6c18e9f3f6b6bc984a465dac16da00106055"
integrity sha512-3p0YRGCcTUCYvBKLIxtDDyrJ0YijGIwrTRu1DT8gIviIDZru8H23+FkY6MJBzM1n9n20CiM4VeDYuBsrrwnLjg==
dependencies:
moo "^0.5.1"
"@messageformat/runtime@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@messageformat/runtime/-/runtime-3.0.1.tgz#94d1f6c43265c28ef7aed98ecfcc0968c6c849ac"
integrity sha512-6RU5ol2lDtO8bD9Yxe6CZkl0DArdv0qkuoZC+ZwowU+cdRlVE1157wjCmlA5Rsf1Xc/brACnsZa5PZpEDfTFFg==
dependencies:
make-plural "^7.0.0"
"@mongodb-js/saslprep@^1.1.5": "@mongodb-js/saslprep@^1.1.5":
version "1.1.6" version "1.1.6"
resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.6.tgz#54da3f794c71a17445740fe2b74882e0c76a3058" resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.6.tgz#54da3f794c71a17445740fe2b74882e0c76a3058"
@@ -8490,6 +8526,11 @@ make-fetch-happen@^9.0.1:
socks-proxy-agent "^6.0.0" socks-proxy-agent "^6.0.0"
ssri "^8.0.0" ssri "^8.0.0"
make-plural@^7.0.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-7.4.0.tgz#fa6990dd550dea4de6b20163f74e5ed83d8a8d6d"
integrity sha512-4/gC9KVNTV6pvYg2gFeQYTW3mWaoJt7WZE5vrp1KnQDgW92JtYZnzmZT81oj/dUTqAIu0ufI2x3dkgu3bB1tYg==
makeerror@1.0.12: makeerror@1.0.12:
version "1.0.12" version "1.0.12"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.12.tgz#3e5dd2079a82e812e983cc6610c4a2cb0eaa801a" resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.12.tgz#3e5dd2079a82e812e983cc6610c4a2cb0eaa801a"
@@ -8819,6 +8860,11 @@ mongodb@^6.3.0:
bson "^6.7.0" bson "^6.7.0"
mongodb-connection-string-url "^3.0.0" mongodb-connection-string-url "^3.0.0"
moo@^0.5.1:
version "0.5.2"
resolved "https://registry.yarnpkg.com/moo/-/moo-0.5.2.tgz#f9fe82473bc7c184b0d32e2215d3f6e67278733c"
integrity sha512-iSAJLHYKnX41mKcJKjqvnAN9sf0LMDTXDEvFv+ffuRR9a1MIuXLjMNL6EsnDHSkKLTWNqQQ5uo61P4EbU4NU+Q==
mri@^1.1.0: mri@^1.1.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b" resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
@@ -10468,6 +10514,11 @@ safe-buffer@5.2.1, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.2, s
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ== integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
safe-identifier@^0.4.1:
version "0.4.2"
resolved "https://registry.yarnpkg.com/safe-identifier/-/safe-identifier-0.4.2.tgz#cf6bfca31c2897c588092d1750d30ef501d59fcb"
integrity sha512-6pNbSMW6OhAi9j+N8V+U715yBQsaWJ7eyEUaOrawX+isg5ZxhUlV1NipNtgaKHmFGiABwt+ZF04Ii+3Xjkg+8w==
safe-regex-test@^1.0.3: safe-regex-test@^1.0.3:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/safe-regex-test/-/safe-regex-test-1.0.3.tgz#a5b4c0f06e0ab50ea2c395c14d8371232924c377" resolved "https://registry.yarnpkg.com/safe-regex-test/-/safe-regex-test-1.0.3.tgz#a5b4c0f06e0ab50ea2c395c14d8371232924c377"