fix: sync language prefixes
This commit is contained in:
17
src/i18n.tsx
17
src/i18n.tsx
@@ -5,7 +5,11 @@ import React, {
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react";
|
||||
import { languageForHomePathname } from "./languageRoutes";
|
||||
import {
|
||||
languageForHomePathname,
|
||||
languageFromPathname,
|
||||
langPathPrefix,
|
||||
} from "./languageRoutes";
|
||||
import type { Dict } from "./locales/types";
|
||||
import { zhDict } from "./locales/zh-CN";
|
||||
import { enDict } from "./locales/en";
|
||||
@@ -40,9 +44,14 @@ const LANG_KEY = "ark_lang";
|
||||
|
||||
export function I18nProvider({ children }: { children: React.ReactNode }) {
|
||||
const [lang, setLangState] = useState<Lang>(() => {
|
||||
const routeLang = languageForHomePathname(window.location.pathname);
|
||||
if (routeLang) return routeLang;
|
||||
if (window.location.pathname === "/") return "en";
|
||||
const path = window.location.pathname;
|
||||
// Any URL whose first path segment is a known language prefix wins
|
||||
// (covers /malay, /malay/browse, /korean/category/foo, etc.).
|
||||
const homeLang = languageForHomePathname(path);
|
||||
if (homeLang) return homeLang;
|
||||
const deepLang = languageFromPathname(path);
|
||||
if (langPathPrefix(deepLang)) return deepLang;
|
||||
if (path === "/") return "en";
|
||||
|
||||
const s = localStorage.getItem(LANG_KEY);
|
||||
if (s === "zh" || s === "zh-TW") return "zh-CN";
|
||||
|
||||
Reference in New Issue
Block a user