Skip to content

Commit

Permalink
feat: add global $li18n provide with renderKey to reactively rend…
Browse files Browse the repository at this point in the history
…er locale changes
  • Loading branch information
NamesMT committed Nov 16, 2024
1 parent c68e80f commit dbf604c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
7 changes: 1 addition & 6 deletions apps/frontend/app/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ definePageMeta({
})
const { t, locale, setLocale } = useI18n()
const dayjsRenderKey = ref(0)
// Fast trigger dayjs re-render after locale change
watch(locale, () => setTimeout(() => ++dayjsRenderKey.value, 100))
const runtimeConfig = useRuntimeConfig()
const colorMode = useColorMode()
const { $apiClient, $auth } = useNuxtApp()
Expand Down Expand Up @@ -90,7 +85,7 @@ const { isPending, isError, data, error } = useQuery({
@pointerdown="setLocale(locale === 'en' ? 'vi' : 'en')"
/>

<div :key="dayjsRenderKey">
<div :key="$li18n.renderKey">
{{ dayjs().format('dddd') }}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defaultOptions } from 'primevue/config'

export default defineNuxtPlugin({
name: 'local-i18n',
name: 'local-li18n',
parallel: true,
dependsOn: [
'local-auth',
Expand All @@ -15,15 +15,26 @@ export default defineNuxtPlugin({
firstDayOfWeek: 1,
}

const li18n = reactive({
renderKey: 0,
})

watchImmediate(() => $i18n.locale.value, async (locale) => {
// @ts-expect-error locale might not be defined
await setDayjsLocale(locale).catch(() => { console.error(`Failed to set '${locale}' for dayjs hook`) })

switch (locale) {
default:
primevue.config.locale = { ...baseLocale }
break
}

++li18n.renderKey
})

return {
provide: {
li18n,
},
}
},
})

0 comments on commit dbf604c

Please sign in to comment.