fix: sync language prefixes
This commit is contained in:
17
src/i18n.tsx
17
src/i18n.tsx
@@ -5,7 +5,11 @@ import React, {
|
|||||||
useMemo,
|
useMemo,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { languageForHomePathname } from "./languageRoutes";
|
import {
|
||||||
|
languageForHomePathname,
|
||||||
|
languageFromPathname,
|
||||||
|
langPathPrefix,
|
||||||
|
} from "./languageRoutes";
|
||||||
import type { Dict } from "./locales/types";
|
import type { Dict } from "./locales/types";
|
||||||
import { zhDict } from "./locales/zh-CN";
|
import { zhDict } from "./locales/zh-CN";
|
||||||
import { enDict } from "./locales/en";
|
import { enDict } from "./locales/en";
|
||||||
@@ -40,9 +44,14 @@ const LANG_KEY = "ark_lang";
|
|||||||
|
|
||||||
export function I18nProvider({ children }: { children: React.ReactNode }) {
|
export function I18nProvider({ children }: { children: React.ReactNode }) {
|
||||||
const [lang, setLangState] = useState<Lang>(() => {
|
const [lang, setLangState] = useState<Lang>(() => {
|
||||||
const routeLang = languageForHomePathname(window.location.pathname);
|
const path = window.location.pathname;
|
||||||
if (routeLang) return routeLang;
|
// Any URL whose first path segment is a known language prefix wins
|
||||||
if (window.location.pathname === "/") return "en";
|
// (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);
|
const s = localStorage.getItem(LANG_KEY);
|
||||||
if (s === "zh" || s === "zh-TW") return "zh-CN";
|
if (s === "zh" || s === "zh-TW") return "zh-CN";
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
homePathForLang,
|
homePathForLang,
|
||||||
isHomePathname,
|
isHomePathname,
|
||||||
languageFromPathname,
|
languageFromPathname,
|
||||||
|
localizePath,
|
||||||
stripLangPrefix,
|
stripLangPrefix,
|
||||||
} from "../languageRoutes";
|
} from "../languageRoutes";
|
||||||
import { useLocalizedPath } from "../useLocalizedPath";
|
import { useLocalizedPath } from "../useLocalizedPath";
|
||||||
@@ -318,7 +319,15 @@ export function PublicLayout() {
|
|||||||
const homePath = homePathForLang(lang);
|
const homePath = homePathForLang(lang);
|
||||||
const changeLang = (nextLang: Lang) => {
|
const changeLang = (nextLang: Lang) => {
|
||||||
setLang(nextLang);
|
setLang(nextLang);
|
||||||
if (isHome) nav(homePathForLang(nextLang), { replace: true });
|
if (isHome) {
|
||||||
|
nav(homePathForLang(nextLang), { replace: true });
|
||||||
|
} else {
|
||||||
|
// Preserve sub-path and query/hash; only swap the language prefix.
|
||||||
|
const canonical = stripLangPrefix(pathname);
|
||||||
|
nav(localizePath(canonical, nextLang) + search + hash, {
|
||||||
|
replace: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const footerInContentFlow = stripLangPrefix(pathname) === "/browse";
|
const footerInContentFlow = stripLangPrefix(pathname) === "/browse";
|
||||||
// Current page name shown in the header brand slot (falls back to the brand).
|
// Current page name shown in the header brand slot (falls back to the brand).
|
||||||
|
|||||||
Reference in New Issue
Block a user