Skip to content

Instantly share code, notes, and snippets.

@idesignzone
Created January 10, 2024 14:31
Show Gist options
  • Save idesignzone/5f03cd969cca78d9cf7642dc73f4a6a4 to your computer and use it in GitHub Desktop.
Save idesignzone/5f03cd969cca78d9cf7642dc73f4a6a4 to your computer and use it in GitHub Desktop.
Nuxt UI i18n language switcher
<template>
<USelectMenu
v-model="$i18n.locale"
:options="availableLocales"
variant="none"
size="md"
:popper="{ arrow: false }"
:ui="{ option: { container: 'flex items-center gap-2 min-w-max' } }">
<template #leading>
<UIcon name="i-heroicons-language" class="w-5 h-5" />
</template>
<template #label>
<span v-if="$i18n.locale" class="ml-1 truncate">{{
languageNames.of($i18n.locale)
}}</span>
</template>
<template #option="{ option: lang }">
<ULink
:to="switchLocalePath(lang.code)"
active-class="min-w-full truncate w-full inline-flex"
inactive-class="min-w-full truncate w-full inline-flex">
{{ lang.name }}
</ULink>
</template>
</USelectMenu>
</template>
<script setup>
const { locale, locales } = useI18n();
const switchLocalePath = useSwitchLocalePath();
const availableLocales = computed(() => {
return locales.value.filter((i) => i.code !== locale.value);
});
const langs = locales.value.map((obj) => obj.code);
const languageNames = new Intl.DisplayNames(langs, {
type: "language",
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment