Vue Integration
Relevant source files
The following files were used as context for generating this wiki page:
Vue Integration provides the essential bindings, components, and routing helpers to implement the i18n-micro system within Vue 3 applications. This module focuses on low runtime cost and small bundle sizes by providing framework-specific wrappers around the core i18n logic found in @i18n-micro/core and @i18n-micro/utils.
Sources: packages/vue/package.json:3-12, AGENTS.md:12-19
Core Architecture
The Vue integration is structured as a set of SPA bindings located in packages/vue/, which includes entries for general Vue logic and a specific adapter for vue-router. It relies on peer dependencies of vue and vue-router to manage reactivity and navigation.
Sources: packages/vue/package.json:38-65, AGENTS.md:43-52
Dependency Flow
The following diagram illustrates the relationship between the Vue package and the shared internal libraries:
The Vue package acts as a consumer of shared domain logic while providing the reactive interface for the end-user.
Sources: packages/vue/package.json:67-85, AGENTS.md:8-15
Runtime Components
The integration provides several built-in components for handling translations, links, and locale switching directly in Vue templates.
| Component | Purpose | Key Features |
|---|---|---|
i18n-t | Translation | Supports keypath, pluralization, and parameters. |
i18n-link | Localized Routing | Automatically prefixes paths based on current locale. |
i18n-switcher | Locale Selection | Renders UI for changing the active language. |
i18n-group | Scoped Context | Provides scoped slot bindings for t and prefix. |
Sources: scripts/src/commands/docs-generate.ts:251-260, scripts/test/docs-components.test.ts:12-30
Component Features and Slots
- i18n-t: Supports pluralization via the
:pluralprop and custom plural rules. It can also render HTML content when thehtmlprop is set to true. - i18n-link: Handles internal navigation using the
:toprop (supporting both strings and objects) and automatically resolves localized paths. - i18n-group: Provides a default slot with
prefixandtbindings, allowing child elements to use context-aware translations.
Sources: scripts/test/docs-components.test.ts:32-46, test/fixtures/basic/pages/page.vue:24-30, test/fixtures/basic/pages/locale-test.vue:42-44
Injected Helpers and Composables
The integration injects several helper functions into the Vue/Nuxt application context, enabling access to i18n features in both templates and script blocks.
Translation and Formatting
$t(key, params): Translates a key with optional parameter interpolation.$tc(key, count): Performs pluralized translation based on the provided count.$ts(key): A reactive version of the translation helper often used in computed properties.$tn(value): Formats numbers according to locale rules.$td(date): Formats dates.$tdr(date): Formats relative dates.
Sources: playground/pages/subpage.vue:150-160, test/fixtures/basic/pages/page.vue:104-120, test/fixtures/basic/pages/locale-test.vue:53-65
State and Navigation
$getLocale(): Returns the current active locale code.$getLocales(): Returns an array of all available locale objects.$switchLocale(code): Updates the application to a new locale.$localeRoute(route, locale): Generates a localized route object.$localePath(path): Returns a localized path string.$has(key, route): Checks for the existence of a translation key.
Sources: playground/pages/subpage.vue:140-155, test/fixtures/basic/pages/page.vue:40-65
Translation Resolution Flow
The system ensures that translations are retrieved from a centralized storage and remain reactive to locale changes.
Sources: test/fixtures/basic/pages/page.vue:110-120, AGENTS.md:46-51
Routing Adapter
The routing adapter, exported via @i18n-micro/vue/router, integrates with vue-router to manage path strategies. It handles locale prefixes such as prefix, prefix_except_default, and no_prefix.
- Custom Path Mapping: You can define specific paths for different locales using the
localeRoutesproperty in$defineI18nRoute. - Route Exclusion: The generator can exclude specific routes from localization using
excludePatterns. - Alias Localization: The adapter correctly localizes route aliases defined in the router configuration.
Sources: packages/vue/package.json:47-58, README.md:92-98, packages/route-strategy/tests/general.test.ts:6-25, playground/pages/subpage.vue:163-172
Page-Level Configuration
Individual Vue components can define their own localized content and routing overrides using the $defineI18nRoute method. This allows for splitting translations between global files and page-specific logic.
$defineI18nRoute({
locales: {
en: { greeting: 'Hello' },
ru: { greeting: 'Привет' }
},
localeRoutes: {
ru: '/privet-page'
}
})
Sources: playground/pages/subpage.vue:163-177, test/fixtures/basic/pages/locale-test.vue:75-105
The Vue Integration ensures that internationalization is both performant and deeply integrated into the Vue lifecycle, providing developers with familiar tools like components and composables while maintaining the efficiency of the underlying micro-architecture.