diff --git a/front/src/i18n/locales.ts b/front/src/i18n/locales.ts index c18a1e5b..9e29161a 100644 --- a/front/src/i18n/locales.ts +++ b/front/src/i18n/locales.ts @@ -1,9 +1,10 @@ import { detectLocale, navigatorDetector, initLocalStorageDetector } from "typesafe-i18n/detectors"; import { FALLBACK_LOCALE } from "../Enum/EnvironmentVariable"; -import { setLocale } from "./i18n-svelte"; +import { setLocale, locale } from "./i18n-svelte"; import type { Locales } from "./i18n-types"; import { baseLocale, locales } from "./i18n-util"; import { loadLocaleAsync } from "./i18n-util.async"; +import { get } from "svelte/store"; const fallbackLocale = (FALLBACK_LOCALE || baseLocale) as Locales; const localStorageProperty = "language"; @@ -42,3 +43,20 @@ export const displayableLocales: { id: Locales; language: string; region: string region: new Intl.DisplayNames(locale, { type: "region" }).of(region), }; }); + +export const i18nJson = (text: string): string => { + if (text.trim().startsWith("{")) { + try { + const textObject = JSON.parse(text); + if (textObject[get(locale)]) { + return textObject[get(locale)]; + } else if (Object.keys(textObject).length > 0) { + // fallback to first value + return textObject[Object.keys(textObject)[0]]; + } + } catch (err) { + // + } + } + return text; +};