-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string>, | ||
de: de as Record<string, string>, | ||
kr: kr as Record<string, string>, | ||
} as const; | ||
|
||
export type LocaleKey = keyof typeof en; | ||
export type LocaleLang = keyof typeof langs; | ||
|
||
export const [language, setLanguage] = createSignal<LocaleLang>("en"); | ||
|
||
export function t(id: LocaleKey, args: Array<string | number> = []) { | ||
const lang = language(); | ||
const str = langs[lang][id] || langs.en[id]; | ||
|
||
if (str) { | ||
const parts = str.split("{}"); | ||
if (parts.length > 0) { | ||
const merged: Array<string | number> = []; | ||
|
||
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<number | string | undefined> = [], | ||
): string | undefined { | ||
return i18next.t(id, { lng: lang, ...args }); | ||
} |