@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
156 lines (154 loc) • 6.34 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_getStorageAttributes = require('../getStorageAttributes.cjs');
let __intlayer_config_built = require("@intlayer/config/built");
__intlayer_config_built = require_rolldown_runtime.__toESM(__intlayer_config_built);
//#region src/utils/localeStorage.ts
const buildCookieString = (name, value, attributes) => {
const parts = [`${name}=${encodeURIComponent(value)}`];
if (attributes.path) parts.push(`Path=${attributes.path}`);
if (attributes.domain) parts.push(`Domain=${attributes.domain}`);
if (attributes.expires instanceof Date) parts.push(`Expires=${attributes.expires.toUTCString()}`);
if (attributes.secure) parts.push("Secure");
if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite}`);
return parts.join("; ");
};
/**
* Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function checks storage locations in order of priority as defined in the configuration.
*
* @returns The locale if found in any storage, or undefined if not found
*/
const getLocaleFromStorage = (options) => {
const { routing, internationalization } = __intlayer_config_built.default;
const { locales } = internationalization;
const { storage } = routing;
if (storage === false || options?.isCookieEnabled === false) return void 0;
const storageAttributes = require_getStorageAttributes.getStorageAttributes(storage);
const isValidLocale = (value) => {
if (!value) return false;
return locales.includes(value);
};
const readCookie = (name) => {
try {
const fromOption = options?.getCookie?.(name);
if (fromOption !== null) return fromOption;
} catch {}
try {
const cookieString = document.cookie ?? "";
if (!cookieString) return;
const pairs = cookieString.split(";");
for (let i = 0; i < pairs.length; i++) {
const part = pairs[i].trim();
if (!part) continue;
const equalIndex = part.indexOf("=");
if ((equalIndex >= 0 ? part.substring(0, equalIndex) : part) === name) {
const rawValue = equalIndex >= 0 ? part.substring(equalIndex + 1) : "";
try {
return decodeURIComponent(rawValue);
} catch {
return rawValue;
}
}
}
} catch {}
};
for (let i = 0; i < storageAttributes.cookies.length; i++) {
const { name } = storageAttributes.cookies[i];
const value = readCookie(name);
if (isValidLocale(value)) return value;
}
for (let i = 0; i < storageAttributes.localStorage.length; i++) {
const { name } = storageAttributes.localStorage[i];
try {
const value = options?.getLocaleStorage?.(name);
if (isValidLocale(value)) return value;
} catch {}
}
for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
const { name } = storageAttributes.sessionStorage[i];
try {
const value = options?.getSessionStorage?.(name);
if (isValidLocale(value)) return value;
} catch {}
}
for (let i = 0; i < storageAttributes.headers.length; i++) {
const { name } = storageAttributes.headers[i];
try {
const value = options?.getHeader?.(name);
if (isValidLocale(value)) return value;
} catch {}
}
};
/**
* Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function writes to all configured storage locations according to their attributes.
* Respects overwrite flags for localStorage and sessionStorage.
*
* @param locale - The locale to store
*/
const setLocaleInStorage = (locale, options) => {
if (__intlayer_config_built.default.routing.storage === false || options?.isCookieEnabled === false) return;
const storageAttributes = require_getStorageAttributes.getStorageAttributes(__intlayer_config_built.default.routing.storage);
for (let i = 0; i < storageAttributes.cookies.length; i++) {
const { name, attributes } = storageAttributes.cookies[i];
try {
if (options?.setCookieStore) options?.setCookieStore?.(name, locale, {
...attributes,
expires: attributes.expires instanceof Date ? attributes.expires.getTime() : attributes.expires
});
} catch {
try {
if (options?.setCookieString) {
const cookieString = buildCookieString(name, locale, attributes);
options?.setCookieString?.(name, cookieString);
}
} catch {}
}
}
if (options?.setLocaleStorage) for (let i = 0; i < storageAttributes.localStorage.length; i++) {
const { name } = storageAttributes.localStorage[i];
try {
if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {
if (options?.getLocaleStorage?.(name)) continue;
}
options?.setLocaleStorage?.(name, locale);
} catch {}
}
if (options?.setSessionStorage) for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
const { name } = storageAttributes.sessionStorage[i];
try {
if (!(options?.overwrite ?? true) && options?.getSessionStorage) {
if (options?.getSessionStorage?.(name)) continue;
}
options?.setSessionStorage?.(name, locale);
} catch {}
}
if (options?.setHeader) for (let i = 0; i < storageAttributes.headers.length; i++) {
const { name } = storageAttributes.headers[i];
try {
options?.setHeader?.(name, locale);
} catch {}
}
};
/**
* Utility object to get and set the locale in the storage by considering the configuration
*
* @property getLocale - Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function checks storage locations in order of priority as defined in the configuration.
*
* @property setLocale - Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function writes to all configured storage locations according to their attributes.
* Respects overwrite flags for localStorage and sessionStorage.
*
* @returns The locale if found in any storage, or undefined if not found
*/
const LocaleStorage = (options) => ({
getLocale: () => getLocaleFromStorage(options),
setLocale: (locale) => setLocaleInStorage(locale, options)
});
//#endregion
exports.LocaleStorage = LocaleStorage;
exports.getLocaleFromStorage = getLocaleFromStorage;
exports.setLocaleInStorage = setLocaleInStorage;
//# sourceMappingURL=localeStorage.cjs.map