diff --git a/composables/states.ts b/composables/states.ts index cba7079..68987d1 100644 --- a/composables/states.ts +++ b/composables/states.ts @@ -1,3 +1,5 @@ +// AUTH + export const useIsLoggedIn = () => { const isLoggedIn = useState( 'isLoggedIn', @@ -13,6 +15,34 @@ export const useIsLoggedIn = () => { }) } +// LOCALE + +const usePreferedLocaleState = () => + useState( + 'preferedLocale', + () => localStorage.getItem('preferedLocale') || 'auto', + ) + +export const usePreferedLocale = () => { + const preferedLocale = usePreferedLocaleState() + const { locale, getBrowserLocale, defaultLocale } = useNuxtApp().$i18n + + return computed({ + get: () => preferedLocale.value, + set: (value) => { + preferedLocale.value = value + localStorage.setItem('preferedLocale', value) + + if (value === 'auto') { + const browserLocale = getBrowserLocale() + value = browserLocale || defaultLocale + } + + locale.value = value + }, + }) +} + export const useRedirectUrl = () => useState('redirectUrl', () => null) diff --git a/locales/en.json b/locales/en.json index b78fbb7..4264c61 100644 --- a/locales/en.json +++ b/locales/en.json @@ -11,6 +11,7 @@ "processing": "Processing…" }, "app_list": "App list", + "automatic": "Automatic ({name})", "back_to_apps": "Go back to app list", "cancel": "Cancel", "change_password": "Change password", diff --git a/nuxt.config.ts b/nuxt.config.ts index 2dfe916..5cecdb5 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -40,6 +40,9 @@ export default defineNuxtConfig({ lazy: true, langDir: 'locales', locales: locales as LocaleObject[], + detectBrowserLanguage: { + useCookie: false, + }, }, colorMode: { preference: 'system', diff --git a/pages/edit.vue b/pages/edit.vue index 6d5d2d9..37af591 100644 --- a/pages/edit.vue +++ b/pages/edit.vue @@ -1,15 +1,27 @@