Skip to content

Commit

Permalink
Locales use i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
luckydye committed Apr 21, 2024
1 parent 6b0cd7f commit ad269d7
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions packages/locales/src/lib.ts
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 });
}

0 comments on commit ad269d7

Please sign in to comment.