mirror of https://github.com/usememos/memos
chore: add `i18n` based with `useContext`
parent
735938395b
commit
646a41e931
@ -1,27 +0,0 @@
|
|||||||
import { useCallback, useRef } from "react";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* useDebounce: useRef + useCallback
|
|
||||||
* @param func function
|
|
||||||
* @param delay delay duration
|
|
||||||
* @param deps depends
|
|
||||||
* @returns debounced function
|
|
||||||
*/
|
|
||||||
export default function useDebounce<T extends (...args: any[]) => any>(func: T, delay: number, deps: any[] = []): T {
|
|
||||||
const timer = useRef<number>();
|
|
||||||
|
|
||||||
const cancel = useCallback(() => {
|
|
||||||
if (timer.current) {
|
|
||||||
clearTimeout(timer.current);
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const run = useCallback((...args: any) => {
|
|
||||||
cancel();
|
|
||||||
timer.current = window.setTimeout(() => {
|
|
||||||
func(...args);
|
|
||||||
}, delay);
|
|
||||||
}, deps);
|
|
||||||
|
|
||||||
return run as T;
|
|
||||||
}
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import useI18n from "../labs/i18n/useI18n";
|
||||||
|
|
||||||
|
export default useI18n;
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
import { createContext, useEffect, useState } from "react";
|
||||||
|
import i18nStore from "./i18nStore";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: React.ReactElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
const i18nContext = createContext(i18nStore.getState());
|
||||||
|
|
||||||
|
const I18nProvider: React.FC<Props> = (props: Props) => {
|
||||||
|
const { children } = props;
|
||||||
|
const [i18nState, setI18nState] = useState(i18nStore.getState());
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = i18nStore.subscribe((ns) => {
|
||||||
|
setI18nState(ns);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribe();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return <i18nContext.Provider value={i18nState}>{children}</i18nContext.Provider>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default I18nProvider;
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
type I18nState = Readonly<{
|
||||||
|
locale: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
type Listener = (ns: I18nState, ps?: I18nState) => void;
|
||||||
|
|
||||||
|
const createI18nStore = (preloadedState: I18nState) => {
|
||||||
|
const listeners: Listener[] = [];
|
||||||
|
let currentState = preloadedState;
|
||||||
|
|
||||||
|
const getState = () => {
|
||||||
|
return currentState;
|
||||||
|
};
|
||||||
|
|
||||||
|
const setState = (state: Partial<I18nState>) => {
|
||||||
|
const nextState = {
|
||||||
|
...currentState,
|
||||||
|
...state,
|
||||||
|
};
|
||||||
|
const prevState = currentState;
|
||||||
|
currentState = nextState;
|
||||||
|
|
||||||
|
for (const cb of listeners) {
|
||||||
|
cb(currentState, prevState);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const subscribe = (listener: Listener) => {
|
||||||
|
let isSubscribed = true;
|
||||||
|
listeners.push(listener);
|
||||||
|
|
||||||
|
const unsubscribe = () => {
|
||||||
|
if (!isSubscribed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = listeners.indexOf(listener);
|
||||||
|
listeners.splice(index, 1);
|
||||||
|
isSubscribed = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
getState,
|
||||||
|
setState,
|
||||||
|
subscribe,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultI18nState = {
|
||||||
|
locale: "en",
|
||||||
|
};
|
||||||
|
|
||||||
|
const i18nStore = createI18nStore(defaultI18nState);
|
||||||
|
|
||||||
|
export default i18nStore;
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import i18nStore from "./i18nStore";
|
||||||
|
import enLocale from "../../locales/en.json";
|
||||||
|
import zhLocale from "../../locales/zh.json";
|
||||||
|
|
||||||
|
type Locale = "en" | "zh";
|
||||||
|
|
||||||
|
const resources: Record<string, any> = {
|
||||||
|
en: enLocale,
|
||||||
|
zh: zhLocale,
|
||||||
|
};
|
||||||
|
|
||||||
|
const useI18n = () => {
|
||||||
|
const [{ locale }, setState] = useState(i18nStore.getState());
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = i18nStore.subscribe((ns) => {
|
||||||
|
setState(ns);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribe();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const translate = (key: string) => {
|
||||||
|
try {
|
||||||
|
return resources[locale][key] as string;
|
||||||
|
} catch (error) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const setLocale = (locale: Locale) => {
|
||||||
|
i18nStore.setState({
|
||||||
|
locale,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
t: translate,
|
||||||
|
locale,
|
||||||
|
setLocale,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useI18n;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"about": "About"
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"about": "关于"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue