diff --git a/packages/locales/src/lib.ts b/packages/locales/src/lib.ts index b9f0726..bb877e8 100644 --- a/packages/locales/src/lib.ts +++ b/packages/locales/src/lib.ts @@ -1,39 +1,40 @@ -import { createSignal } from "solid-js"; +import logger from "@luckydye/log"; +import i18next from "i18next"; + import de from "../locales/de.json"; import en from "../locales/en.json"; import kr from "../locales/kr.json"; -export const langs = { - en: en as Record, - de: de as Record, - kr: kr as Record, -} as const; - -export type LocaleKey = keyof typeof en; -export type LocaleLang = keyof typeof langs; - -export const [language, setLanguage] = createSignal("en"); - -export function t(id: LocaleKey, args: Array = []) { - const lang = language(); - const str = langs[lang][id] || langs.en[id]; - - if (str) { - const parts = str.split("{}"); - if (parts.length > 0) { - const merged: Array = []; - - parts.forEach((part, i) => { - merged.push(part); - const arg = args[i]; - if (arg) { - merged.push(arg); - } - }); - - return merged.join("") || id; - } - } - - return str; +export const AVAILABLE_LANGS = ["en"]; +export const DEFAULT_LANGUAGE = "en"; + +const log = logger().prefix("i18next").trace(); + +// https://www.i18next.com/overview/api + +i18next.init( + { + // partialBundledLanguages: true, /* partialy from backend */ + fallbackLng: "en", + defaultNS: "global", + resources: {}, + }, + (err) => { + if (err) return log.error("something went wrong loading", err); + log.info("i18next loaded"); + }, +); + +i18next.addResourceBundle("de", "global", de); +i18next.addResourceBundle("en", "global", en); +i18next.addResourceBundle("kr", "global", kr); + +export type LocaleKey = keyof typeof de; + +export function translation( + id: LocaleKey | LocaleKey[], + lang: string, + args: Array = [], +): string | undefined { + return i18next.t(id, { lng: lang, ...args }); }