Files
OmniRoute/src/i18n/resolveRequestedLocale.ts
Diego Rodrigues de Sa e Souza 143c087d47 feat(i18n): locale-expansion tooling — add-locale orchestrator, aliases, translation-ratio gate; retire duplicate 'in' locale (#12496)
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.
2026-09-02 17:45:50 -03:00

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;
}