Files
kiss-translator/src/hooks/I18n.js

30 lines
692 B
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import { useSetting } from "./Setting";
import { I18N, URL_RAW_PREFIX } from "../config";
2023-08-06 21:12:01 +08:00
import { useFetch } from "./Fetch";
2023-07-20 13:45:41 +08:00
2024-04-20 15:54:41 +08:00
export const getI18n = (uiLang, key, defaultText = "") => {
return I18N?.[key]?.[uiLang] ?? defaultText;
};
export const useLangMap = (uiLang) => {
return (key, defaultText = "") => getI18n(uiLang, key, defaultText);
};
2023-07-20 13:45:41 +08:00
/**
* 多语言 hook
* @returns
*/
export const useI18n = () => {
2023-08-30 18:05:37 +08:00
const {
setting: { uiLang },
} = useSetting();
2024-04-20 15:54:41 +08:00
return useLangMap(uiLang);
2023-07-20 13:45:41 +08:00
};
export const useI18nMd = (key) => {
const i18n = useI18n();
const fileName = i18n(key);
2023-08-15 13:34:09 +08:00
const url = fileName ? `${URL_RAW_PREFIX}/${fileName}` : "";
2023-08-14 14:58:57 +08:00
return useFetch(url);
2023-07-20 13:45:41 +08:00
};