57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
/**
|
|
* Cookie names for language preference
|
|
*/
|
|
const LANGUAGE_COOKIE_NAME = 'votianlt.language';
|
|
const COOKIE_MAX_AGE_DAYS = 365; // Cookie gültig für 1 Jahr
|
|
|
|
/**
|
|
* Sets the language cookie with the selected language code
|
|
* @param languageCode - The language code (e.g., 'de', 'en', 'fr', 'es')
|
|
*/
|
|
export function setLanguageCookie(languageCode: string): void {
|
|
const maxAge = COOKIE_MAX_AGE_DAYS * 24 * 60 * 60; // Convert days to seconds
|
|
document.cookie = `${LANGUAGE_COOKIE_NAME}=${languageCode};path=/;max-age=${maxAge};SameSite=Lax`;
|
|
}
|
|
|
|
/**
|
|
* Gets the language code from the cookie
|
|
* @returns The language code or null if not found
|
|
*/
|
|
export function getLanguageCookie(): string | null {
|
|
const cookies = document.cookie.split(';');
|
|
for (const cookie of cookies) {
|
|
const [name, value] = cookie.trim().split('=');
|
|
if (name === LANGUAGE_COOKIE_NAME) {
|
|
return decodeURIComponent(value);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Clears the language cookie
|
|
*/
|
|
export function clearLanguageCookie(): void {
|
|
document.cookie = `${LANGUAGE_COOKIE_NAME}=;path=/;max-age=0;SameSite=Lax`;
|
|
}
|
|
|
|
/**
|
|
* Maps Language enum values to locale strings
|
|
*/
|
|
export const languageToLocale: Record<string, string> = {
|
|
DE: 'de',
|
|
EN: 'en',
|
|
FR: 'fr',
|
|
ES: 'es',
|
|
};
|
|
|
|
/**
|
|
* Maps locale strings to Language enum values
|
|
*/
|
|
export const localeToLanguage: Record<string, string> = {
|
|
de: 'DE',
|
|
en: 'EN',
|
|
fr: 'FR',
|
|
es: 'ES',
|
|
};
|