Skip to content

Commit 13ac6c2

Browse files
BittuBarnwal7479Your Nameautofix-ci[bot]
authored
fix(i18n): update language switch immediately without refresh (#2525)
Co-authored-by: Your Name <your@email.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 2b89cee commit 13ac6c2

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

app/pages/settings.vue

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
<script setup lang="ts">
2+
import type { LocaleObject } from '@nuxtjs/i18n'
3+
24
const router = useRouter()
35
const { settings } = useSettings()
4-
const { locale: currentLocale, locales, setLocale: setNuxti18nLocale } = useI18n()
6+
const { locale, locales, setLocale: setNuxti18nLocale } = useI18n()
57
const colorMode = useColorMode()
68
const { currentLocaleStatus, isSourceLocale } = useI18nStatus()
79
const keyboardShortcutsEnabled = useKeyboardShortcuts()
810
const { toggleCodeLigatures } = useCodeLigatures()
911
12+
// Create a computed property to handle locale binding properly
13+
const localeCodes = computed<LocaleObject['code'][]>(() =>
14+
locales.value.map(loc => loc.code as LocaleObject['code']),
15+
)
16+
17+
function isLocaleCode(value: string): value is LocaleObject['code'] {
18+
return localeCodes.value.includes(value as LocaleObject['code'])
19+
}
20+
21+
const currentLocale = computed<string>({
22+
get: () => locale.value as string,
23+
set: (newLocale: string) => {
24+
if (!newLocale || !isLocaleCode(newLocale)) return
25+
26+
settings.value.selectedLocale = newLocale
27+
setNuxti18nLocale(newLocale)
28+
},
29+
})
30+
1031
// Escape to go back (but not when focused on form elements or modal is open)
1132
onKeyStroke(
1233
e =>
@@ -29,20 +50,6 @@ useSeoMeta({
2950
ogDescription: () => $t('settings.meta_description'),
3051
twitterDescription: () => $t('settings.meta_description'),
3152
})
32-
33-
defineOgImage(
34-
'Page.takumi',
35-
{
36-
title: () => $t('settings.title'),
37-
description: () => $t('settings.tagline'),
38-
},
39-
{ alt: () => `${$t('settings.title')} — npmx` },
40-
)
41-
42-
const setLocale: typeof setNuxti18nLocale = newLocale => {
43-
settings.value.selectedLocale = newLocale
44-
return setNuxti18nLocale(newLocale)
45-
}
4653
</script>
4754

4855
<template>
@@ -248,7 +255,6 @@ const setLocale: typeof setNuxti18nLocale = newLocale => {
248255
id="language-select"
249256
:items="locales.map(loc => ({ label: loc.name ?? '', value: loc.code }))"
250257
v-model="currentLocale"
251-
@update:modelValue="setLocale($event as typeof currentLocale)"
252258
block
253259
size="sm"
254260
class="max-w-48"

0 commit comments

Comments
 (0)