Pluralization & Formatting
Relevant source files
The following files were used as context for generating this wiki page:
Pluralization and formatting in nuxt-i18n-micro provide the logic for selecting translation variants based on numeric counts and interpolating dynamic data into strings. The system prioritizes performance by using simple string splitting and regular expression replacements rather than heavy parsing libraries.
Pluralization Logic
The pluralization system selects a specific segment of a translation string based on a provided numeric value. Translation keys that support pluralization use the pipe character | to separate different forms.
Standard Pluralization Rules
The module follows a specific index selection logic based on the number of segments provided in the translation string:
- Three segments: The system maps
countto indices as follows:count === 0: Index 0 (e.g., "no apples")count === 1: Index 1 (e.g., "one apple")count > 1: Index 2 (e.g., "{count} apples")
- Two segments: The system maps
countto:count === 1: Index 0other: Index 1
- Fallback: If the count exceeds the number of available segments, the system selects the last available segment.
Sources: src/plural-template.ts, playground/locales/pages/subpage/en.json:3
The plural Function
The plural function processes the translation string and performs the selection and placeholder replacement.
The diagram above shows the decision path for selecting the correct plural form based on the input count.
Sources: src/plural-template.ts
Translation Helpers and Components
The module exposes specific APIs to handle pluralization and interpolation within Vue components.
$tc and $t Methods
$t(key, params): Interpolates named parameters into a translation string. It searches for placeholders wrapped in curly braces (e.g.,{username}).$tc(key, count, params): Specifically handles pluralization. It identifies the correct segment based oncountand then applies parameter interpolation.
Sources: test/fixtures/basic/pages/page.vue:18-24, playground/locales/pages/subpage/en.json:2
i18n-t Component
The <i18n-t> component allows for pluralization via props.
| Prop | Type | Description |
|---|---|---|
keypath | string | The translation key to look up. |
plural | number | The count used to determine the plural form. |
custom-plural-rule | Function | A function to override default plural selection logic. |
Sources: test/fixtures/basic/pages/page.vue:25-33
Custom Pluralization Rules
Users can define custom logic for selecting plural forms. This is useful for languages with complex pluralization requirements that exceed the standard three-form logic. A custom rule receives the key, count, parameters, locale, and a translation function as arguments.
const customPluralRule = (key, count, _params, _locale, t) => {
const translation = t(key)
if (!translation) return null
const forms = translation.toString().split('|')
if (count === 0 && forms.length > 2) {
return forms[0].trim()
}
if (count === 1 && forms.length > 1) {
return forms[1].trim()
}
return (forms.length > 2 ? forms[2].trim() : forms[forms.length - 1].trim())
.replace('{count}', count.toString())
}
Sources: test/fixtures/basic/pages/page.vue:112-126
Data Formatting and Placeholders
The FormatService manages the replacement of placeholders within translation strings.
Interpolation Mechanism
The system identifies placeholders using the syntax {variableName}. When a translation is requested with a parameters object, the FormatService iterates through the keys and replaces the placeholders with the corresponding values from the object.
| Feature | Syntax Example | Result Example |
|---|---|---|
| Simple Interpolation | "Welcome, {username}" | "Welcome, Alice" |
| Plural Interpolation | "{count} messages" | "5 messages" |
| Nested Keys | "key.subkey" | "Deeply nested value" |
Sources: playground/locales/pages/subpage/en.json:2-12, packages/core/src/format-service.ts
Selection Process for Plural Forms
The following sequence diagram illustrates the interaction between the Vue component and the pluralization service.
Pluralization and formatting in this module remain lightweight by avoiding complex AST parsing, relying instead on efficient string manipulation to meet the performance goals of the project.
Sources: README.md:12-25