feat(i18n): implement new locale resolution logic and service layer
This commit is contained in:
+6
-20
@@ -1,28 +1,14 @@
|
||||
import type { TextDirection } from "../../lib/i18n";
|
||||
import {
|
||||
directionForLocale as inferDirectionForLocale,
|
||||
normalizeLocaleCode,
|
||||
} from "../../lib/i18n/locale-resolution";
|
||||
|
||||
export const LOCALE_COOKIE = "neta_locale";
|
||||
export const LOCALE_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
||||
export const DEFAULT_TEXT_DIRECTION: TextDirection = "ltr";
|
||||
|
||||
export function normalizeLocaleCode(value: string | null | undefined): string | null {
|
||||
if (!value) return null;
|
||||
const normalized = value.trim();
|
||||
return /^[a-z]{2}(?:-[A-Z]{2}[0-9]?)?$/.test(normalized) ? normalized : null;
|
||||
}
|
||||
export { normalizeLocaleCode };
|
||||
|
||||
export function directionForLocale(locale: string, explicit?: TextDirection | null): TextDirection {
|
||||
if (explicit) return explicit;
|
||||
return /^(ar|fa|he|ur)(-|$)/.test(locale) ? "rtl" : DEFAULT_TEXT_DIRECTION;
|
||||
}
|
||||
|
||||
export function buildLocaleCookie(value: string) {
|
||||
return {
|
||||
name: LOCALE_COOKIE,
|
||||
value,
|
||||
maxAge: LOCALE_COOKIE_MAX_AGE,
|
||||
sameSite: "lax" as const,
|
||||
path: "/",
|
||||
httpOnly: false,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
};
|
||||
return inferDirectionForLocale(locale) ?? DEFAULT_TEXT_DIRECTION;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user