-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
43 lines (37 loc) · 1.39 KB
/
utils.ts
File metadata and controls
43 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { ui, defaultLang, showDefaultLang } from "./ui";
export function useTranslatedPath(lang: keyof typeof ui) {
return function translatePath(path: string, l: string = lang) {
return !showDefaultLang && l === defaultLang ? path : `/${l}${path}`;
};
}
export function getLangFromUrl(url: URL) {
const [, lang] = url.pathname.split("/");
if (lang in ui) return lang as keyof typeof ui;
return defaultLang;
}
/** Strip leading /zh or /en from a pathname. */
export function stripLangPrefix(pathname: string): string {
const segments = pathname.split("/").filter(Boolean);
if (segments.length > 0 && segments[0] in ui) {
const rest = segments.slice(1).join("/");
return rest ? `/${rest}` : "/";
}
return pathname || "/";
}
/**
* Path for the language switcher — always uses /zh/... or /en/... so switching
* to the default language does not hit / (browser-language redirect).
*/
export function getSwitcherPath(url: URL, targetLang: keyof typeof ui): string {
const pathWithoutLang = stripLangPrefix(url.pathname);
const suffix = pathWithoutLang === "/" ? "/" : pathWithoutLang;
return `/${targetLang}${suffix}`;
}
export function useTranslations(lang: keyof typeof ui) {
return function t(key: keyof (typeof ui)[typeof defaultLang]) {
return ui[lang][key] || ui[defaultLang][key];
};
}
export function tOfUrl(url: URL) {
return useTranslations(getLangFromUrl(url));
}