Composables (useI18n)
Relevant source files
The following files were used as context for generating this wiki page:
Composables provide the primary interface for accessing internationalization logic within the Nuxt 3 and Vue runtime environments. The useI18n composable acts as a bridge to the underlying translation service, exposing methods for message translation, pluralization, and formatting of numbers and dates.
This module prioritizes performance by utilizing a framework-agnostic core (@i18n-micro/core) while providing framework-specific bindings. In Nuxt applications, these helpers are typically injected through plugins and made available to components via composables located in src/runtime/composables.
Sources: AGENTS.md, scripts/src/commands/docs-generate.ts:440
Architecture and Data Flow
The useI18n composable facilitates access to the PluginsInjections interface, which is defined in the Nuxt plugin layer. When a developer calls a translation function, the composable retrieves the active translation storage and formatting rules to produce the localized output.
The diagram shows the request flow from a component through the composable to the core translation and formatting logic.
Sources: scripts/src/commands/docs-generate.ts:424-434, packages/vue/playground/src/pages/Components.vue:73
Key Helpers and Methods
The useI18n composable returns a set of helpers that handle different types of content localization. These methods correspond to the PluginsInjections and NuxtAppOnlyInjections interfaces.
Core Translation Methods
| Helper | Purpose | Parameters |
|---|---|---|
$t / t | Translates a key into the current locale. | (key: string, params?: object) |
$tc / tc | Handles pluralization based on a count. | (key: string, count: number) |
$ts / ts | A reactive translation helper for use in computed properties. | (key: string) |
$has / has | Checks if a specific translation key exists. | (key: string) |
Sources: test/fixtures/basic/pages/page.vue:116-118, scripts/src/commands/docs-generate.ts:133-143, packages/vue/playground/src/pages/Components.vue:73
Formatting Helpers
For non-textual data, the composable provides specialized formatters that adhere to the current locale's rules.
$tn/tn: Formats numeric values according to locale-specific decimal and grouping symbols.$td/td: Formats JavaScriptDateobjects or ISO strings into localized date formats.$tdr/tdr: Generates relative time strings (e.g., "1 hour ago").
Sources: test/fixtures/basic/pages/locale-test.vue:35-51, packages/vue/playground/src/pages/Components.vue:73
Composable Varieties
The system includes several specialized composables to handle specific aspects of the internationalization lifecycle.
| Composable | Responsibility |
|---|---|
useI18n | Main entry point for translation and formatting helpers. |
useI18nLocale | Manages the current active locale and provides methods to switch locales. |
useLocaleHead | Generates SEO-related metadata (hreflang, og:locale) for the current route. |
useI18nHead | Allows per-page overrides for i18n SEO metadata. |
Sources: scripts/test/docs-symbols.test.ts:36-39, packages/utils/CHANGELOG.md:89
Usage Implementation
In a Nuxt 3 component, you destructure the required helpers from useNuxtApp or the useI18n composable. If you need reactivity within a computed property, use the $ts helper to ensure updates trigger correctly upon locale changes.
// Example from test/fixtures/basic/pages/page.vue
import { computed } from 'vue'
import { useNuxtApp } from '#imports'
const { $t, $ts, $getLocale } = useNuxtApp()
// Computed properties remain reactive to locale changes using $ts
const computedLabel = computed(() => {
return $ts('page.example')
})
Sources: test/fixtures/basic/pages/page.vue:116-126
Integration with Routing
The useI18n interface integrates with @i18n-micro/path-strategy to provide localized navigation helpers.
$localeRoute: Returns a route object with the correct locale prefix applied.$localePath: Returns a string path for the requested route in the target locale.$switchLocale: Updates the application state and URL to a different language.
Sources: test/fixtures/basic/pages/page.vue:53-61, playground/pages/subpage.vue:132
The useI18n composable serves as the central API for developers to interact with the high-performance localization core. By providing specialized methods for translation, pluralization, and formatting, it ensures that applications remain efficient while supporting complex internationalization requirements.