| 1 | import { defaultLocale, locales } from "./config"; |
| 2 | |
| 3 | /** Routes whose page bodies are genuinely localized beyond English/Chinese. */ |
| 4 | const ROUTE_CONTENT_LOCALES: Readonly<Record<string, readonly string[]>> = { |
| 5 | "/": locales, |
| 6 | "/docs/guide": ["en", "zh", "fr", "de", "ca", "hi", "tr", "it", "pl", "ar"], |
| 7 | }; |
| 8 | |
| 9 | /** Most first-party page bodies currently ship in English and Chinese. */ |
| 10 | const DEFAULT_CONTENT_LOCALES = ["en", "zh"] as const; |
| 11 | |
| 12 | function normalizedPath(path: string): string { |
| 13 | if (path === "" || path === "/") return "/"; |
| 14 | return `/${path.replace(/^\/+|\/+$/g, "")}`; |
| 15 | } |
| 16 | |
| 17 | /** Locales with a genuine page-body translation for an indexable route. */ |
| 18 | export function contentLocalesForPath(path: string): readonly string[] { |
| 19 | return ROUTE_CONTENT_LOCALES[normalizedPath(path)] ?? DEFAULT_CONTENT_LOCALES; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Canonical locale for a rendered route. |
| 24 | * |
| 25 | * Partial locale routes stay accessible in the product, but an English-body |
| 26 | * fallback points crawlers at the English source instead of presenting a |
| 27 | * duplicate as a translated page. |
| 28 | */ |
| 29 | export function canonicalLocaleForPath(path: string, locale: string): string { |
| 30 | return contentLocalesForPath(path).includes(locale) ? locale : defaultLocale; |
| 31 | } |
| 32 |