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

+9 -1
View File
@@ -3,8 +3,9 @@ import { makeAutomationAPI } from "./automations";
import { makeRemoteAPI } from "./base"; import { makeRemoteAPI } from "./base";
import { useAPIService } from "./core"; import { useAPIService } from "./core";
import { useAutoMockAPI, useProfileMockAPI } from "./mock"; import { useAutoMockAPI, useProfileMockAPI } from "./mock";
import { useRef } from "react"; import { createContext, useContext, useRef } from "react";
import { makeProfileAPI } from "./profile"; import { makeProfileAPI } from "./profile";
import { ApiState } from "./types";
const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i); const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i);
const baseURL = new URL( const baseURL = new URL(
@@ -25,3 +26,10 @@ export const useMockApiService = (
makeAutomationAPI(useAutoMockAPI(initialAutos, useRef, returnErrors)), makeAutomationAPI(useAutoMockAPI(initialAutos, useRef, returnErrors)),
makeProfileAPI(useProfileMockAPI()) makeProfileAPI(useProfileMockAPI())
); );
const ApiStateContext = createContext<ApiState>({
automations: { ready: false },
userProfile: { ready: false },
});
export const ApiStateProvider = ApiStateContext.Provider;
export const useApiState = () => useContext(ApiStateContext);
+1 -1
View File
@@ -16,7 +16,7 @@ export const BottomBar: FC<BottomBarProps> = ({ api }) => {
return ( return (
<div className="bottom-bar center row"> <div className="bottom-bar center row">
<Modal open={isOpen}> <Modal open={isOpen} className="center column">
<UserProfileEditor api={api} /> <UserProfileEditor api={api} />
<Button onClick={() => setOpen(false)}>{lang.get("CLOSE")}</Button> <Button onClick={() => setOpen(false)}>{lang.get("CLOSE")}</Button>
</Modal> </Modal>
+3 -2
View File
@@ -9,14 +9,15 @@ export interface ModalStyleProps {
} }
export interface Props extends ModalStyleProps { export interface Props extends ModalStyleProps {
children: ReactNode; children: ReactNode;
className?: string;
} }
export const Modal: FC<Props> = ({ children, open }) => { export const Modal: FC<Props> = ({ children, open, className }) => {
const { isMobile } = useWindowSize(); const { isMobile } = useWindowSize();
return ( return (
<MuiModal open={open}> <MuiModal open={open}>
<div className={["modal--root", isMobile ? "mobile" : ""].join(" ")}> <div className={["modal--root", isMobile ? "mobile" : ""].join(" ")}>
<Card variant="outlined"> <Card variant="outlined">
<CardContent>{children}</CardContent> <CardContent className={className}>{children}</CardContent>
</Card> </Card>
</div> </div>
</MuiModal> </MuiModal>
+4 -2
View File
@@ -15,7 +15,7 @@ import { ConfirmProvider } from "material-ui-confirm";
import { Color } from "@mui/material"; import { Color } from "@mui/material";
import useWindowSize from "utils/useWindowSize"; import useWindowSize from "utils/useWindowSize";
import { ApiService } from "apiService/core"; import { ApiService } from "apiService/core";
import { useMockApiService } from "apiService"; import { ApiStateProvider, useMockApiService } from "apiService";
export type PageProps = PropsWithChildren<{ export type PageProps = PropsWithChildren<{
api: ApiService; api: ApiService;
@@ -100,7 +100,9 @@ export const Page: FC<PageProps> = (props) => {
dense dense
preventDuplicate preventDuplicate
> >
<InternalPage {...props} /> <ApiStateProvider value={props.api.state}>
<InternalPage {...props} />
</ApiStateProvider>
</SnackbarProvider> </SnackbarProvider>
</ConfirmProvider> </ConfirmProvider>
</ThemeProvider> </ThemeProvider>
+24 -14
View File
@@ -1,3 +1,4 @@
import { useApiState } from "apiService";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
const MISSING_TEXT = "<<MISSING TEXT -- {{langId}}>>"; const MISSING_TEXT = "<<MISSING TEXT -- {{langId}}>>";
@@ -5,6 +6,7 @@ const LOADING_TEXT = "...";
export type LangStore = ReturnType<typeof useLang>; export type LangStore = ReturnType<typeof useLang>;
export const useLang = () => { export const useLang = () => {
const apiState = useApiState();
const [langStore, setLangStore] = const [langStore, setLangStore] =
useState<Record<string, string> | null>(null); useState<Record<string, string> | null>(null);
const [langEngStore, setLangEngStore] = const [langEngStore, setLangEngStore] =
@@ -12,13 +14,16 @@ export const useLang = () => {
useEffect(() => { useEffect(() => {
import("./eng.json").then((data) => { import("./eng.json").then((data) => {
// setLangStore(data.default);
setLangEngStore(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 = ( const updateParams = (
textTemplate: string, textTemplate: string,
@@ -33,15 +38,20 @@ export const useLang = () => {
}; };
return { return {
get: (langId: string, replacements: Record<string, string> = {}) => get: (langId: string, replacements: Record<string, string> = {}) => {
updateParams( let out = LOADING_TEXT;
langStore === null || langEngStore === null if (langStore !== null) {
? LOADING_TEXT out =
: langStore[langId.toLocaleUpperCase()] ?? langStore[langId.toLocaleUpperCase()] ??
langEngStore[langId.toLocaleUpperCase()] ?? `<<${langStore.MISSING_TEXT}>>` ??
`<<${langStore.MISSING_TEXT}>>` ?? MISSING_TEXT;
MISSING_TEXT, } else if (langEngStore !== null) {
{ ...replacements, langId } out =
), langEngStore[langId.toLocaleUpperCase()] ??
`<<${langEngStore.MISSING_TEXT}>>` ??
MISSING_TEXT;
}
return updateParams(out, { ...replacements, langId });
},
}; };
}; };