mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-21 06:12:17 +03:00
Locale aliases (uk, fil/tl, zh-hant, legacy in→id) with fixed browser/CLI/cookie detection; config/i18n.json shipped in the npm package; duplicate 'in' locale retired (42 honest locales); real-translation ratio gate with ratchet baseline (advisory); add-locale orchestrator + shared libs (batched translation backend, config-generated language bars, run-translation --adopt/--targets-only, scaffold helpers); tests derive the locale count from config with surface parity guards; docs and every public locale-count claim aligned.
24 lines
919 B
TypeScript
24 lines
919 B
TypeScript
/**
|
|
* Resolves a locale requested via cookie (`NEXT_LOCALE`) or the `x-locale`
|
|
* header to a configured locale. Accepts declared aliases so a cookie written
|
|
* before a locale was retired (`in` → `id`) or a bare tag (`uk` → `uk-UA`)
|
|
* keeps working. Dependency-free: shared by `request.ts` and unit tests.
|
|
*/
|
|
export function resolveRequestedLocale(
|
|
requested: string,
|
|
locales: readonly string[],
|
|
aliases: Readonly<Record<string, readonly string[]>>,
|
|
fallback: string
|
|
): string {
|
|
if (!requested) return fallback;
|
|
if (locales.includes(requested)) return requested;
|
|
const lower = requested.toLowerCase();
|
|
const exact = locales.find((locale) => locale.toLowerCase() === lower);
|
|
if (exact) return exact;
|
|
for (const [code, tags] of Object.entries(aliases)) {
|
|
if (!locales.includes(code)) continue;
|
|
if (tags.some((tag) => tag.toLowerCase() === lower)) return code;
|
|
}
|
|
return fallback;
|
|
}
|