UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

49 lines (48 loc) • 1.95 kB
//#region src/ui/locale.ts function readLocaleCookie(cookie, cookieName) { for (const part of cookie.split(";")) { const [name, ...valueParts] = part.trim().split("="); if (name !== cookieName) continue; try { return decodeURIComponent(valueParts.join("=")); } catch { return; } } } function matchLocale(candidate, locales) { if (!candidate) return void 0; const normalized = candidate.trim().toLowerCase(); const exact = locales.find((locale) => locale.toLowerCase() === normalized); if (exact) return exact; const base = normalized.split("-")[0]; return locales.find((locale) => locale.toLowerCase() === base); } function readAcceptLanguage(header) { return (header ?? "").split(",").map((part, index) => { const [language = "", ...parameters] = part.trim().split(";"); const quality = parameters.find((parameter) => parameter.trim().startsWith("q=")); return { language, quality: quality ? Number(quality.trim().slice(2)) : 1, index }; }).filter(({ language, quality }) => language && quality > 0 && quality <= 1).sort((a, b) => b.quality - a.quality || a.index - b.index).map(({ language }) => language); } function resolveLocale({ preferredLocale, cookie, browserLocales, locales, defaultLocale, cookieName }) { const preferred = matchLocale(preferredLocale, locales); if (preferred) return preferred; const saved = matchLocale(readLocaleCookie(cookie, cookieName), locales); if (saved) return saved; for (const browserLocale of browserLocales) { const matched = matchLocale(browserLocale, locales); if (matched) return matched; } return defaultLocale; } function serializeLocaleCookie(cookieName, locale, secure) { return `${cookieName}=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`; } //#endregion export { matchLocale, readAcceptLanguage, readLocaleCookie, resolveLocale, serializeLocaleCookie }; //# sourceMappingURL=locale.mjs.map