feat(config): add shared browser cookie policy
This commit is contained in:
@@ -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 } }));
|
||||
|
||||
Reference in New Issue
Block a user