The best way to control Claude Code, Codex and more from any computer or phone

main
ts 51 lines 2.23 KB
Raw
1 // `code` is Astro's locale code and the BCP 47 tag for `<html lang>` and hreflang. `path` is the URL
2 // segment. pt-BR lives at `/pt-br`: lowercase like every other path on the site, with the region
3 // kept so a future pt-PT can sit beside it. zh is Simplified Chinese.
4 export const LOCALES = [
5 { code: "en", path: "", name: "English" },
6 { code: "es", path: "es", name: "Español" },
7 { code: "zh-Hans", path: "zh", name: "简体中文" },
8 { code: "pt-BR", path: "pt-br", name: "Português (Brasil)" },
9 { code: "fr", path: "fr", name: "Français" },
10 { code: "de", path: "de", name: "Deutsch" },
11 { code: "ja", path: "ja", name: "日本語" },
12 { code: "ko", path: "ko", name: "한국어" },
13 { code: "ru", path: "ru", name: "Русский" },
14 ] as const;
15
16 export type Locale = (typeof LOCALES)[number]["code"];
17
18 export const DEFAULT_LOCALE: Locale = "en";
19
20 /** Pages that exist in every locale. Legal pages, the 404, and /95 stay English-only. */
21 export const LOCALIZED_PATHS = ["/", "/download"] as const;
22
23 export type LocalizedPath = (typeof LOCALIZED_PATHS)[number];
24
25 export const isLocalizedPath = (path: string): path is LocalizedPath =>
26 (LOCALIZED_PATHS as ReadonlyArray<string>).includes(path);
27
28 export const localeInfo = (locale: Locale) => LOCALES.find((entry) => entry.code === locale)!;
29
30 export const localizedHref = (locale: Locale, path: LocalizedPath): string => {
31 const prefix = localeInfo(locale).path;
32 if (!prefix) return path;
33 return path === "/" ? `/${prefix}` : `/${prefix}${path}`;
34 };
35
36 /** `getStaticPaths` entries for a `[...locale]` route: English at the root, the rest prefixed. */
37 export const localeStaticPaths = () =>
38 LOCALES.map((entry) => ({
39 params: { locale: entry.path || undefined },
40 props: { locale: entry.code as Locale },
41 }));
42
43 /** Splits `"On ARM? {deb} or {appimage}."` so callers can render each `{key}` as markup. */
44 export const templateParts = (template: string): Array<string | { key: string }> =>
45 template
46 .split(/(\{\w+\})/)
47 .filter(Boolean)
48 .map((part) => (/^\{\w+\}$/.test(part) ? { key: part.slice(1, -1) } : part));
49
50 export const fillTemplate = (template: string, values: Record<string, string>): string =>
51 template.replace(/\{(\w+)\}/g, (match, key: string) => values[key] ?? match);