UI Components
Relevant source files
The following files were used as context for generating this wiki page:
UI Components provide a declarative way to handle internationalization within the user interface of applications using nuxt-i18n-micro. These components manage translation rendering, localized navigation, locale switching, and logical grouping of translation keys. The system supports multiple frameworks, including Vue and SolidJS, ensuring consistent i18n behavior across the ecosystem.
The core component set includes I18nT for translation interpolation, I18nLink for route localization, I18nSwitcher for language selection, and I18nGroup for scoped translation contexts.
Translation Rendering with I18nT
The I18nT component renders localized strings based on a provided keypath. It handles complex interpolation scenarios, including pluralization, number formatting, and date formatting.
Core Functionality
I18nT accepts parameters to replace placeholders in translation strings. It also supports rendering HTML content when specified.
The diagram above shows the decision logic for processing different props passed to the I18nT component.
Sources: packages/vue/playground/src/pages/Components.vue:10-25, packages/solid/playground/src/pages/Components.tsx:16-43
Key Properties
| Prop | Type | Description |
|---|---|---|
keypath | string | The translation key to resolve. |
params | object | Key-value pairs for string interpolation (e.g., { name: 'Vue' }). |
plural | number | Determines the plural form based on count. |
number | number | Formats the value as a localized number. |
date | Date | string | Formats the value as a localized date. |
relativeDate | Date | string | Formats the value as a localized relative time. |
html | boolean | If true, renders the result as HTML. |
tag | string | The HTML element to use as a wrapper (e.g., div). |
Sources: scripts/src/commands/docs-generate.ts:161-177, scripts/test/docs-components.test.ts:40-45, packages/solid/playground/src/pages/Components.tsx:40-42
Localized Navigation with I18nLink
The I18nLink component manages navigation by automatically prefixing routes with the current locale. It ensures that users stay within their selected language context when clicking links.
Behavior and Routing
I18nLink wraps the standard link component of the target framework (e.g., NuxtLink in Nuxt or A in SolidJS). It resolves the localized path before rendering the underlying anchor element.
This sequence diagram illustrates how I18nLink interacts with the routing strategy to resolve localized URLs.
Sources: packages/vue/playground/src/pages/Components.vue:28-33, test/fixtures/basic/pages/page.vue:85-87
Navigation Features
- Active Styling: Supports
active-styleoractiveClassto highlight the current route. - External Links: Detects external URLs (e.g.,
https://...) and skips localization logic. - Route Objects: Accepts both string paths and route location objects.
Sources: packages/vue/playground/src/pages/Components.vue:32, test/fixtures/basic/pages/page.vue:85, scripts/test/docs-components.test.ts:39-44
Scoped Contexts with I18nGroup
The I18nGroup component creates a scoped translation context by providing a prefix to all children. This reduces the need to repeat long, nested keypaths in complex UI sections.
Scoped Data Flow
I18nGroup uses a scoped slot (in Vue) or a context provider (in SolidJS) to expose a localized translation function t and the current prefix.
This diagram demonstrates the relationship between the I18nGroup prefix and the resulting translation keys resolved by its children.
Sources: packages/vue/playground/src/pages/Components.vue:37-43, scripts/test/docs-components.test.ts:32-37
Slot Bindings
| Binding | Description |
|---|---|
t | A localized translation function that automatically prepends the group prefix. |
prefix | The raw string prefix assigned to the group. |
Sources: scripts/test/docs-components.test.ts:36, packages/vue/playground/src/pages/Components.vue:39
Language Selection with I18nSwitcher
The I18nSwitcher provides a pre-built interface for users to change the application's locale. It retrieves available locales from the configuration and triggers the locale switch logic.
Implementation Details
- Locale Retrieval: Uses
$getLocales()to populate the list of options. - Style Customization: In SolidJS, it supports custom styling props such as
customButtonStyle,customDropdownStyle, andcustomActiveLinkStyle. - State Management: Tracks the current locale to disable the button for the active language.
Sources: packages/vue/playground/src/pages/Components.vue:47-48, packages/solid/playground/src/pages/Components.tsx:64-83, test/fixtures/basic/pages/page.vue:104-106
Summary
The UI Components in nuxt-i18n-micro facilitate efficient, performance-focused internationalization. I18nT handles complex text rendering, while I18nLink and I18nSwitcher manage navigation and state. I18nGroup optimizes developer experience by scoping translation keys. These components are validated through automated documentation generation and unit tests to ensure all props and slots remain correctly documented.
Sources: scripts/src/commands/docs-generate.ts:181-197, scripts/test/docs-components.test.ts:15-30