refactor: admin language first

This commit is contained in:
naiba 2024-12-15 14:38:35 +08:00
parent ace8e7d691
commit 647bdb9fa1
6 changed files with 4932 additions and 7 deletions

4919
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ import ErrorPage from "./pages/ErrorPage"
import NotFound from "./pages/NotFound" import NotFound from "./pages/NotFound"
import Server from "./pages/Server" import Server from "./pages/Server"
import ServerDetail from "./pages/ServerDetail" import ServerDetail from "./pages/ServerDetail"
import { useTranslation } from "react-i18next"
const App: React.FC = () => { const App: React.FC = () => {
const { data: settingData, error } = useQuery({ const { data: settingData, error } = useQuery({
@ -18,6 +19,7 @@ const App: React.FC = () => {
refetchOnMount: true, refetchOnMount: true,
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
}) })
const { i18n } = useTranslation()
const InjectContext = useCallback((content: string) => { const InjectContext = useCallback((content: string) => {
const tempDiv = document.createElement("div") const tempDiv = document.createElement("div")
@ -61,6 +63,10 @@ const App: React.FC = () => {
return null return null
} }
if (settingData?.data?.language && !localStorage.getItem("language")) {
i18n.changeLanguage(settingData?.data?.language)
}
if (settingData?.data?.custom_code) { if (settingData?.data?.custom_code) {
InjectContext(settingData?.data?.custom_code) InjectContext(settingData?.data?.custom_code)
} }

View File

@ -23,7 +23,7 @@ export function LanguageSwitcher() {
const localeItems = [ const localeItems = [
{ name: t("language.zh-CN"), code: "zh-CN" }, { name: t("language.zh-CN"), code: "zh-CN" },
{ name: t("language.zh-TW"), code: "zh-TW" }, { name: t("language.zh-TW"), code: "zh-TW" },
{ name: t("language.en"), code: "en" }, { name: t("language.en-US"), code: "en-US" },
] ]
return ( return (

View File

@ -6,7 +6,7 @@ import zhCNTranslation from "./locales/zh-CN/translation.json"
import zhTWTranslation from "./locales/zh-TW/translation.json" import zhTWTranslation from "./locales/zh-TW/translation.json"
const resources = { const resources = {
en: { "en-US": {
translation: enTranslation, translation: enTranslation,
}, },
"zh-CN": { "zh-CN": {
@ -18,13 +18,13 @@ const resources = {
} }
const getStoredLanguage = () => { const getStoredLanguage = () => {
return localStorage.getItem("language") || "en" return localStorage.getItem("language") || "en-US"
} }
i18n.use(initReactI18next).init({ i18n.use(initReactI18next).init({
resources, resources,
lng: getStoredLanguage(), // 使用localStorage中存储的语言或默认值 lng: getStoredLanguage(), // 使用localStorage中存储的语言或默认值
fallbackLng: "en", // 当前语言的翻译没有找到时,使用的备选语言 fallbackLng: "en-US", // 当前语言的翻译没有找到时,使用的备选语言
interpolation: { interpolation: {
escapeValue: false, // react已经安全地转义 escapeValue: false, // react已经安全地转义
}, },

View File

@ -79,7 +79,7 @@
"language": { "language": {
"zh-CN": "简体中文", "zh-CN": "简体中文",
"zh-TW": "繁體中文", "zh-TW": "繁體中文",
"en": "English" "en-US": "English"
}, },
"theme": { "theme": {
"light": "Light", "light": "Light",

View File

@ -79,7 +79,7 @@
"language": { "language": {
"zh-CN": "简体中文", "zh-CN": "简体中文",
"zh-TW": "繁體中文", "zh-TW": "繁體中文",
"en": "English" "en-US": "English"
}, },
"theme": { "theme": {
"light": "亮色", "light": "亮色",