From 8647199625f0a064e15ecad7a18b869d1c30128c Mon Sep 17 00:00:00 2001 From: _Bastler <_Bastler@bstly.de> Date: Mon, 9 May 2022 08:40:29 +0200 Subject: [PATCH] i18n --- front/src/i18n/locales.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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; +};