added language support

This commit is contained in:
ari committed 2022-08-30 22:03:40 -04:00
1 parent 03a735b281
commit 8985312dd2
5 files changed
+41 -20

No files matched your search

+24 -14
View File
@@ -1,3 +1,4 @@
import { useApiState } from "apiService";
import { useEffect, useState } from "react";
const MISSING_TEXT = "<<MISSING TEXT -- {{langId}}>>";
@@ -5,6 +6,7 @@ const LOADING_TEXT = "...";
export type LangStore = ReturnType<typeof useLang>;
export const useLang = () => {
const apiState = useApiState();
const [langStore, setLangStore] =
useState<Record<string, string> | null>(null);
const [langEngStore, setLangEngStore] =
@@ -12,13 +14,16 @@ export const useLang = () => {
useEffect(() => {
import("./eng.json").then((data) => {
// setLangStore(data.default);
setLangEngStore(data.default);
});
import("./eng.json").then((data) => {
setLangStore(data.default);
});
}, []);
useEffect(() => {
if (apiState.userProfile.ready && apiState.userProfile.ok) {
import(`./${apiState.userProfile.data.lang}.json`).then((data) => {
setLangStore(data.default);
});
}
}, [apiState.userProfile]);
const updateParams = (
textTemplate: string,
@@ -33,15 +38,20 @@ export const useLang = () => {
};
return {
get: (langId: string, replacements: Record<string, string> = {}) =>
updateParams(
langStore === null || langEngStore === null
? LOADING_TEXT
: langStore[langId.toLocaleUpperCase()] ??
langEngStore[langId.toLocaleUpperCase()] ??
`<<${langStore.MISSING_TEXT}>>` ??
MISSING_TEXT,
{ ...replacements, langId }
),
get: (langId: string, replacements: Record<string, string> = {}) => {
let out = LOADING_TEXT;
if (langStore !== null) {
out =
langStore[langId.toLocaleUpperCase()] ??
`<<${langStore.MISSING_TEXT}>>` ??
MISSING_TEXT;
} else if (langEngStore !== null) {
out =
langEngStore[langId.toLocaleUpperCase()] ??
`<<${langEngStore.MISSING_TEXT}>>` ??
MISSING_TEXT;
}
return updateParams(out, { ...replacements, langId });
},
};
};