useLocale
The useLocale composable provides comprehensive internationalization (i18n) capabilities, allowing you to manage multiple locales, switch between them dynamically, and translate messages with variable replacement and message linking. Built on createSingle for single-locale selection and supports custom adapters for integration with different i18n libraries.
Installation
Install the Locale plugin in your app’s entry point:
import { createApp } from 'vue'
import { createLocalePlugin } from '@vuetify/v0'
import App from './App.vue'
const app = createApp(App)
app.use(
createLocalePlugin({
default: 'en',
messages: {
en: {
hello: 'Hello',
welcome: 'Welcome, {name}!',
},
es: {
hello: 'Hola',
welcome: '¡Bienvenido, {name}!',
},
},
})
)
app.mount('#app')Usage
Once the plugin is installed, use the useLocale composable in any component:
<script setup lang="ts">
import { useLocale } from '@vuetify/v0'
const locale = useLocale()
function changeLocale(id: string) {
locale.select(id)
}
</script>
<template>
<div>
<h1>{{ locale.t('hello') }}</h1>
<p>{{ locale.t('welcome', { name: 'John' }) }}</p>
<button @click="changeLocale('en')">English</button>
<button @click="changeLocale('es')">Español</button>
<button @click="changeLocale('fr')">Français</button>
</div>
</template>Architecture
useLocale extends createSingle for locale selection with message interpolation:
Reactivity
Locale selection is reactive via createSingle. Translation methods return static strings.
| Property | Reactive | Notes |
|---|---|---|
selectedId | Current locale ID | |
selectedItem | Current locale ticket | |
selectedValue | Current locale value | |
selectedIndex | Index in registry |
Functions
createLocaleFallback
() => RcreateLocaleContext
<_E>(_options?: LocaleContextOptions | undefined) => ContextTrinity<_E>createLocalePlugin
(_options?: LocaleContextOptions | undefined) => PluginuseLocale
<_E>(namespace?: string) => _EOptions
disabled
MaybeRefOrGetter<boolean> | undefinedDisabled state for the entire model instance
Default: false
multiple
MaybeRefOrGetter<boolean> | undefinedAllow multiple tickets to be selected simultaneously
Default: false
mandatory
MaybeRefOrGetter<boolean | "force"> | undefinedControls mandatory selection behavior: - `false` (default): No mandatory selection enforcement - `true`: Prevents deselecting the last selected item - `'force'`: Automatically selects the first non-disabled item on registration
adapter
LocaleAdapter | undefineddefault
ID | undefinedfallback
ID | undefinedmessages
Record<ID, Z> | undefinedProperties
selectedValues
ComputedRef<Set<E["value"] extends Ref<infer U, infer U> ? U : E["value"]>>Computed Set of selected ticket values
selectedId
ComputedRef<ID | undefined>selectedIndex
ComputedRef<number>selectedItem
ComputedRef<E | undefined>selectedValue
ComputedRef<E["value"] | undefined>Methods
move
(id: ID, toIndex: number) => E | undefinedSeek for a ticket based on direction and optional predicate
seek
(direction?: "first" | "last", from?: number, predicate?: (ticket) => boolean) => E | undefinedon
<K extends Extensible<RegistryEventName>>(event: K, cb: EventHandler<E, K>) => voidListen for registry events
off
<K extends Extensible<RegistryEventName>>(event: K, cb: EventHandler<E, K>) => voidStop listening for registry events
emit
<K extends Extensible<RegistryEventName>>(event: K, data: EventPayload<E, K>) => voidEmit an event with data
batch
<R>(fn: () => R) => RExecute operations in a batch, deferring cache invalidation and event emission until complete
t
(key: string, params?: Record<string, unknown>, fallback?: string) => stringTranslate a message key with optional parameters and fallback.
n
(value: number) => string