aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/i18n/text.ts
blob: f8f7e7e6bc9a9e0adcce88f558d9c5ed68847173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import i18n from "i18next";
import { useTranslation } from "react-i18next";

export type I18nText =
  | string
  | { type: "text" | "custom"; value: string }
  | { type: "i18n"; value: string };

type T = typeof i18n.t;

export function convertI18nText(text: I18nText, t: T): string {
  if (typeof text === "string") {
    return t(text);
  } else if (text.type === "i18n") {
    return t(text.value);
  } else {
    return text.value;
  }
}

export interface C {
  (text: I18nText): string;
}

export function createC(t: T): C {
  return ((text) => convertI18nText(text, t)) as C;
}

export const c = createC(i18n.t);

export function useC(ns?: string): C {
  const { t } = useTranslation(ns);
  return createC(t);
}