feat(config): add shared browser cookie policy
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@wrnexus/i18n",
|
||||
"version": "0.8.9",
|
||||
"version": "0.8.10",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
|
||||
@@ -30,6 +30,7 @@ export interface LocaleLoadOptions {
|
||||
export interface I18nCookieConfig {
|
||||
name?: string;
|
||||
maxAge?: number;
|
||||
domain?: string;
|
||||
path?: string;
|
||||
sameSite?: "Strict" | "Lax" | "None";
|
||||
secure?: boolean;
|
||||
@@ -253,6 +254,7 @@ export function resolveI18n(
|
||||
cookie: {
|
||||
name: config.cookie?.name ?? LANG_COOKIE,
|
||||
maxAge: config.cookie?.maxAge ?? 31_536_000,
|
||||
domain: config.cookie?.domain ?? "",
|
||||
path: config.cookie?.path ?? "/",
|
||||
sameSite: config.cookie?.sameSite ?? "Lax",
|
||||
secure: config.cookie?.sameSite === "None" ? true : (config.cookie?.secure ?? false),
|
||||
@@ -464,10 +466,16 @@ export const I18N_RUNTIME = String.raw`
|
||||
var current = state();
|
||||
if (current.langs && current.langs.indexOf(lang) === -1) return false;
|
||||
var cookie = current.cookie || {};
|
||||
var secure = cookie.secure || location.protocol === "https:";
|
||||
document.cookie = encodeURIComponent(cookie.name || "${LANG_COOKIE}") + "=" + encodeURIComponent(lang) +
|
||||
";path=" + (cookie.path || "/") + ";max-age=" + (cookie.maxAge || 31536000) +
|
||||
";samesite=" + (cookie.sameSite || "Lax") + (secure ? ";secure" : "");
|
||||
var cookieName = cookie.name || "${LANG_COOKIE}";
|
||||
if (window.wrnCookies && typeof window.wrnCookies.set === "function") {
|
||||
window.wrnCookies.set(cookieName, lang, "language", cookie);
|
||||
} else {
|
||||
var secure = cookie.secure || location.protocol === "https:";
|
||||
var domain = cookie.domain ? ";domain=" + cookie.domain : "";
|
||||
document.cookie = encodeURIComponent(cookieName) + "=" + encodeURIComponent(lang) +
|
||||
";path=" + (cookie.path || "/") + ";max-age=" + (cookie.maxAge || 31536000) +
|
||||
";samesite=" + (cookie.sameSite || "Lax") + domain + (secure ? ";secure" : "");
|
||||
}
|
||||
document.documentElement.lang = lang;
|
||||
document.documentElement.dir = (current.directions && current.directions[lang]) || "ltr";
|
||||
window.dispatchEvent(new CustomEvent("wrnexus:language-change", { detail: { locale: lang } }));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import { resolveI18n, makeT, resolveLang, translateHtml } from "../src/index.ts";
|
||||
import { I18N_RUNTIME, resolveI18n, makeT, resolveLang, translateHtml } from "../src/index.ts";
|
||||
|
||||
const i18n = resolveI18n(
|
||||
{
|
||||
@@ -27,6 +27,10 @@ test("resolveLang: cookie → Accept-Language → default", () => {
|
||||
expect(resolveLang(i18n, "xx", "de")).toBe("en"); // invalid cookie + unsupported header
|
||||
});
|
||||
|
||||
test("language changes use the shared browser cookie helper", () => {
|
||||
expect(I18N_RUNTIME).toContain('window.wrnCookies.set(cookieName, lang, "language", cookie)');
|
||||
});
|
||||
|
||||
test("translateHtml resolves data-t text and t:attr attributes", () => {
|
||||
const t = makeT(i18n, "en");
|
||||
const out = translateHtml('<input t:placeholder="ph"><span data-t="nav.home"></span>', t);
|
||||
|
||||
Reference in New Issue
Block a user