Fix i18n not working if locale is set to a non-existen language

This commit is contained in:
Nikita Karamov 2024-01-20 16:42:23 +01:00
parent 3819048880
commit 447c877f3c
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD

View file

@ -9,9 +9,6 @@
import { strings, defaultLanguage, languages } from "./translations"; import { strings, defaultLanguage, languages } from "./translations";
export function useTranslations(language: string) { export function useTranslations(language: string) {
if (!(language in strings)) {
language = defaultLanguage;
}
return function t( return function t(
key: keyof (typeof strings)[typeof defaultLanguage], key: keyof (typeof strings)[typeof defaultLanguage],
): string { ): string {
@ -46,6 +43,9 @@ export function findBestLanguage(): string {
} }
export function applyTranslations(language: string) { export function applyTranslations(language: string) {
if (!(language in strings)) {
language = defaultLanguage;
}
const t = useTranslations(language); const t = useTranslations(language);
for (const node of document.querySelectorAll("[data-translate]")) { for (const node of document.querySelectorAll("[data-translate]")) {