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:

When using namespaces, reference strings outside the default namespace by prefixing them with the namespace, e.g., navigation:home.title.

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:

Relative time formatting automatically 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:

  1. Install the loader:

  1. Configure your project structure:

  1. 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 instance

  • ea: EventAggregator instance

  • Intl: 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(): string

    • Returns the current active locale

  • tr(key: string, options?: any): string

    • Translates the given key with optional parameters

    • Returns the translated string

  • nf(options?: Intl.NumberFormatOptions, locale?: string): Intl.NumberFormat

    • Creates a number formatter

    • Returns NumberFormat instance

  • df(options?: Intl.DateTimeFormatOptions, locale?: string): Intl.DateTimeFormat

    • Creates a date formatter

    • Returns DateTimeFormat instance

TCustomAttribute

Static Methods

  • configureAliases(aliases: string[]): void

    • Configures custom aliases for the translation attribute

Properties

  • service: I18N

  • element: Element

  • params: any

Configuration Interfaces

I18NConfigOptions

Events and Signals

Event Aggregator Events

  • i18n:locale:changed

    • Triggered when locale changes

    • Payload: { oldValue: string, newValue: string }

Binding Signals

  • aurelia-translation-signal

    • Triggers update of all t binding behaviors

  • aurelia-relativetime-signal

    • Triggers update of relative time bindings

Value Converters

TValueConverter

  • Parameters:

    • value: string: Translation key

    • options?: any: Translation options

    • locale?: string: Specific locale (optional)

NfValueConverter

  • Parameters:

    • value: number: Number to format

    • options?: Intl.NumberFormatOptions: Formatting options

    • locale?: string: Specific locale (optional)

DfValueConverter

  • Parameters:

    • value: Date: Date to format

    • options?: Intl.DateTimeFormatOptions: Formatting options

    • locale?: string: Specific locale (optional)

RtValueConverter

  • Parameters:

    • value: Date: Date to format relative to now

Backend Options

XHR Backend

Aurelia Loader Backend

Some features may require additional configuration depending on your build setup and environment.

All configuration options from i18next are also supported. Refer to i18next documentation for additional options.

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

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

Consider implementing a CI check to ensure translation files are properly formatted and complete.

Last updated

Was this helpful?