Current Locale
useLocale() returns the current locale, a switcher function, a loading flag, and the active locale's metadata.
ts
import { useLocale } from 'vue-i18n-kit'
const { locale, setLocale, isLoading, localeMeta } = useLocale()Return value
locale
Ref<string> — currently active locale code (reactive).
setLocale
(lang: string) => Promise<void> — switch to a different locale. Lazy-loads the JSON if needed, then updates locale. Throws if lang is not registered.
isLoading
Ref<boolean> — true while a locale's JSON is being fetched.
localeMeta
ComputedRef<Record<string, unknown> | undefined> — metadata of the active locale from its LocaleDefinition.meta. Updates reactively on locale switch.
Pass a generic type for typed localeMeta without manual casting:
ts
interface AppLocaleMeta {
display: string
flag: string
author?: string
}
const { localeMeta } = useLocale<AppLocaleMeta>()
localeMeta.value?.display // string | undefined — fully typedExample — locale switcher
vue
<script setup lang="ts">
import { useLocale, useAvailableLocales } from 'vue-i18n-kit'
const { locale, setLocale, isLoading, localeMeta } = useLocale()
const { availableLocales } = useAvailableLocales()
async function handleChange(code: string) {
try {
await setLocale(code)
} catch (err) {
console.error('Failed to load locale:', err)
}
}
</script>
<template>
<span>{{ localeMeta?.flag }} {{ localeMeta?.display ?? locale }}</span>
<select :value="locale" @change="handleChange(($event.target as HTMLSelectElement).value)">
<option v-for="loc in availableLocales" :key="loc.code" :value="loc.code">
{{ loc.meta?.flag }} {{ loc.meta?.display ?? loc.code }}
</option>
</select>
<span v-if="isLoading">Loading…</span>
</template>