added language support
This commit is contained in:
1 parent
03a735b281
commit
8985312dd2
5 files changed
+41
-20
No files matched your search
@@ -3,8 +3,9 @@ import { makeAutomationAPI } from "./automations";
|
||||
import { makeRemoteAPI } from "./base";
|
||||
import { useAPIService } from "./core";
|
||||
import { useAutoMockAPI, useProfileMockAPI } from "./mock";
|
||||
import { useRef } from "react";
|
||||
import { createContext, useContext, useRef } from "react";
|
||||
import { makeProfileAPI } from "./profile";
|
||||
import { ApiState } from "./types";
|
||||
|
||||
const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i);
|
||||
const baseURL = new URL(
|
||||
@@ -25,3 +26,10 @@ export const useMockApiService = (
|
||||
makeAutomationAPI(useAutoMockAPI(initialAutos, useRef, returnErrors)),
|
||||
makeProfileAPI(useProfileMockAPI())
|
||||
);
|
||||
|
||||
const ApiStateContext = createContext<ApiState>({
|
||||
automations: { ready: false },
|
||||
userProfile: { ready: false },
|
||||
});
|
||||
export const ApiStateProvider = ApiStateContext.Provider;
|
||||
export const useApiState = () => useContext(ApiStateContext);
|
||||
@@ -16,7 +16,7 @@ export const BottomBar: FC<BottomBarProps> = ({ api }) => {
|
||||
|
||||
return (
|
||||
<div className="bottom-bar center row">
|
||||
<Modal open={isOpen}>
|
||||
<Modal open={isOpen} className="center column">
|
||||
<UserProfileEditor api={api} />
|
||||
<Button onClick={() => setOpen(false)}>{lang.get("CLOSE")}</Button>
|
||||
</Modal>
|
||||
|
||||
@@ -9,14 +9,15 @@ export interface ModalStyleProps {
|
||||
}
|
||||
export interface Props extends ModalStyleProps {
|
||||
children: ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
export const Modal: FC<Props> = ({ children, open }) => {
|
||||
export const Modal: FC<Props> = ({ children, open, className }) => {
|
||||
const { isMobile } = useWindowSize();
|
||||
return (
|
||||
<MuiModal open={open}>
|
||||
<div className={["modal--root", isMobile ? "mobile" : ""].join(" ")}>
|
||||
<Card variant="outlined">
|
||||
<CardContent>{children}</CardContent>
|
||||
<CardContent className={className}>{children}</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</MuiModal>
|
||||
|
||||
@@ -15,7 +15,7 @@ import { ConfirmProvider } from "material-ui-confirm";
|
||||
import { Color } from "@mui/material";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
import { ApiService } from "apiService/core";
|
||||
import { useMockApiService } from "apiService";
|
||||
import { ApiStateProvider, useMockApiService } from "apiService";
|
||||
|
||||
export type PageProps = PropsWithChildren<{
|
||||
api: ApiService;
|
||||
@@ -100,7 +100,9 @@ export const Page: FC<PageProps> = (props) => {
|
||||
dense
|
||||
preventDuplicate
|
||||
>
|
||||
<InternalPage {...props} />
|
||||
<ApiStateProvider value={props.api.state}>
|
||||
<InternalPage {...props} />
|
||||
</ApiStateProvider>
|
||||
</SnackbarProvider>
|
||||
</ConfirmProvider>
|
||||
</ThemeProvider>
|
||||
|
||||
+24
-14
@@ -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 });
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user