This commit is contained in:
mdares
2026-01-06 21:51:08 +00:00
parent 05a30b2a21
commit 7790361a0a
6 changed files with 283 additions and 119 deletions

54
lib/i18n/localeStore.ts Normal file
View File

@@ -0,0 +1,54 @@
import type { Locale } from "./translations";
const LOCALE_COOKIE = "mis_locale";
let initialized = false;
let currentLocale: Locale = "en";
const listeners = new Set<() => void>();
function readCookieLocale(): Locale | null {
const match = document.cookie
.split(";")
.map((part) => part.trim())
.find((part) => part.startsWith(`${LOCALE_COOKIE}=`));
if (!match) return null;
const value = match.split("=")[1];
if (value === "es-MX" || value === "en") return value;
return null;
}
function readLocaleFromDocument(): Locale {
const cookieLocale = readCookieLocale();
if (cookieLocale) return cookieLocale;
const docLang = document.documentElement.getAttribute("lang");
if (docLang === "es-MX" || docLang === "en") return docLang;
return "en";
}
function ensureInitialized() {
if (initialized) return;
currentLocale = readLocaleFromDocument();
initialized = true;
}
export function getLocaleSnapshot(): Locale {
if (typeof document === "undefined") return "en";
ensureInitialized();
return currentLocale;
}
export function subscribeLocale(listener: () => void) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
export function setLocale(next: Locale) {
if (typeof document !== "undefined") {
document.documentElement.setAttribute("lang", next);
document.cookie = `${LOCALE_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`;
}
currentLocale = next;
listeners.forEach((listener) => listener());
}