aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/i18n/text.ts
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-21 23:49:12 +0800
committercrupest <crupest@outlook.com>2023-09-21 23:49:12 +0800
commitd477c7270c90b190ed82b13f48f39a05d83503d2 (patch)
tree572f74b2adc75247b9d1c9a8965e3de2559d4160 /FrontEnd/src/i18n/text.ts
parenta8a8385cd959e4d9d57b8a35381d2851049c07ff (diff)
downloadtimeline-d477c7270c90b190ed82b13f48f39a05d83503d2.tar.gz
timeline-d477c7270c90b190ed82b13f48f39a05d83503d2.tar.bz2
timeline-d477c7270c90b190ed82b13f48f39a05d83503d2.zip
Fix #1394.
Diffstat (limited to 'FrontEnd/src/i18n/text.ts')
-rw-r--r--FrontEnd/src/i18n/text.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/FrontEnd/src/i18n/text.ts b/FrontEnd/src/i18n/text.ts
new file mode 100644
index 00000000..f8f7e7e6
--- /dev/null
+++ b/FrontEnd/src/i18n/text.ts
@@ -0,0 +1,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);
+}
+