Files
blog/frontend/app/composables/useLocale.ts
T
2026-09-08 23:34:42 +02:00

22 lines
568 B
TypeScript

import { STRINGS, type Strings } from '../i18n/strings'
export type Locale = 'it' | 'en'
/**
* Site language, persisted in a cookie so SSR renders the right language on
* the first request. `t(key)` reads the current locale's UI string.
*/
export function useLocale() {
const locale = useCookie<Locale>('lang', { default: () => 'it', sameSite: 'lax' })
function setLocale(value: Locale) {
locale.value = value
}
function t<K extends keyof Strings>(key: K): Strings[K] {
return STRINGS[locale.value][key]
}
return { locale, setLocale, t }
}