i18n Internationalization
Overview
Aurelia's internationalization (i18n) features are provided through the aurelia-i18n plugin, which is built on top of the widely-used i18next library. This combination offers a powerful and flexible solution for adding multilingual support to your Aurelia applications, including translations, number formatting, date formatting, and relative time displays.
Features
Text translations with variable substitution
Number and date formatting based on locale
Relative time formatting
Multiple translation namespaces
HTML content support in translations
Flexible backend options for loading translations
TypeScript support
Value converters and binding behaviors for declarative usage
Requirements
Aurelia application
Node.js and NPM
Basic understanding of JSON formatting
Installation
Using Aurelia CLI
Install the required packages using npm:
For XHR backend support (optional):
Using Webpack
Install the same packages as above:
And optionally:
TypeScript Support
If you're using TypeScript, install the necessary type definitions:
Setup and Configuration
Basic Setup
1. Configure Manual Bootstrapping
Update your index.html to use manual bootstrapping:
2. Project Structure
Create the following folder structure in your project:
3. Translation File Format
Create your translation files using this structure:
Configuration Options
Basic Configuration
Configure the plugin in your main.js/ts:
Using Built-in Aurelia Loader
Alternative configuration using Aurelia's built-in loader:
Multiple Namespaces
You can organize translations into multiple namespaces for better organization:
This allows you to structure your translations like this:
Ensure all your translation files are properly formatted JSON and that all namespaces are present in each language folder to avoid runtime errors.
Using Translations
Setting and Getting Locales
Setting the Active Locale
Change the application's language using the setLocale method:
Getting the Active Locale
Retrieve the current locale using getLocale:
Translation Methods
Translating via Code
Use the tr method for programmatic translations:
Translating via HTML Attributes
Use the t attribute in your templates:
Available special attributes:
[text]: Sets textContent (default)[html]: Sets innerHTML[append]: Appends translation to existing content[prepend]: Prepends translation to existing content
Using Parameters in Templates
Pass parameters using the t-params attribute:
Using Value Converters
The t value converter provides a declarative way to translate content:
Using Binding Behaviors
For automatic updates when locale changes, use the t binding behavior:
Working with Images
Images can be translated when different images need to be displayed for different languages:
Translation file:
Complex Parameter Handling
Object Parameters
Context-Based Translations
Plural Forms
Formatting
Number Formatting
Via Code
Use the nf method for programmatic number formatting:
Using the Number Value Converter
Format numbers declaratively in templates:
Date Formatting
Via Code
Use the df method for date formatting:
Using the Date Value Converter
Format dates in templates:
Relative Time Formatting
Via Code
Use the RelativeTime service:
Using the Relative Time Value Converter
Format relative times in templates:
When using number or date formatting with value converters, ensure your locale binding is properly set up to trigger updates when the locale changes.
Error Handling and Debugging
Missing Translations
Common Issues and Solutions
Issue: Translations not updating when locale changes
Solution: Ensure you're using the binding behavior (
&t) instead of value converter (|t)
Issue: HTML content displayed as escaped text
Solution: Use the
[html]attribute:t="[html]myKey"
Issue: Parameters not being replaced
Solution: Check parameter naming matches exactly in translation files
Debugging Tools
Build and Bundle Configuration
Aurelia CLI Configuration
The latest versions of Aurelia CLI include built-in support for loading locale files. If you're using the standard CLI setup, no additional configuration is required.
Webpack Configuration
Using i18next-resource-store-loader
For bundling all translations with your main application:
Install the loader:
Configure your project structure:
Configure your main.js/ts:
Copying Translation Files
Add to your webpack.config.js to copy translation files:
API Reference
Core Classes
I18N Class
Properties
i18next: The underlying i18next instanceea: EventAggregator instanceIntl: Reference to the Intl API
Methods
setup(options: I18NConfigOptions): Promise<i18next>Initializes the plugin with the provided options
Returns a promise that resolves when initialization is complete
setLocale(locale: string): Promise<any>Changes the active locale
Returns a promise that resolves when the locale is changed
getLocale(): stringReturns the current active locale
tr(key: string, options?: any): stringTranslates the given key with optional parameters
Returns the translated string
nf(options?: Intl.NumberFormatOptions, locale?: string): Intl.NumberFormatCreates a number formatter
Returns NumberFormat instance
df(options?: Intl.DateTimeFormatOptions, locale?: string): Intl.DateTimeFormatCreates a date formatter
Returns DateTimeFormat instance
TCustomAttribute
Static Methods
configureAliases(aliases: string[]): voidConfigures custom aliases for the translation attribute
Properties
service: I18Nelement: Elementparams: any
Configuration Interfaces
I18NConfigOptions
Events and Signals
Event Aggregator Events
i18n:locale:changedTriggered when locale changes
Payload:
{ oldValue: string, newValue: string }
Binding Signals
aurelia-translation-signalTriggers update of all t binding behaviors
aurelia-relativetime-signalTriggers update of relative time bindings
Value Converters
TValueConverter
Parameters:
value: string: Translation keyoptions?: any: Translation optionslocale?: string: Specific locale (optional)
NfValueConverter
Parameters:
value: number: Number to formatoptions?: Intl.NumberFormatOptions: Formatting optionslocale?: string: Specific locale (optional)
DfValueConverter
Parameters:
value: Date: Date to formatoptions?: Intl.DateTimeFormatOptions: Formatting optionslocale?: string: Specific locale (optional)
RtValueConverter
Parameters:
value: Date: Date to format relative to now
Backend Options
XHR Backend
Aurelia Loader Backend
Integrations
Using with Aurelia Validation
Integration with Aurelia Dialog
Custom Backend Integration
Real-World Examples
Dynamic Form with Translations
Dynamic Menu with Language Switching
Localized Data Grid
Best Practices and Common Patterns
Project Organization
Recommended Translation Structure
Translation Key Naming Conventions
Translation Tips
Keep Translations Maintainable
Handle Pluralization Properly
Use Parameters Effectively
Performance Considerations
Lazy Loading
Load translations by namespace when needed
Consider splitting large translation files
Use route-based translation loading for large applications
Caching
Enable localStorage caching for faster subsequent loads
Configure cache expiration based on your update frequency
Common Patterns
Handling Dynamic Content
Form Validation Messages
Error Handling
Testing
Translation Key Coverage
Automated Checks
Verify all locales have the same keys
Check for missing translations
Validate parameter usage
Always test your applications with different locales to catch any internationalization-related issues early.
Last updated
Was this helpful?