Internationalization (i18n)
Analog supports runtime internationalization using Angular's built-in $localize system. This allows you to serve translated content with a single build, detecting the user's locale at runtime on both the server and client.
Setup
1. Install @angular/localize
Add the @angular/localize package to your project:
npm install @angular/localize
2. Initialize $localize
Import the $localize polyfill in your application's entry point (src/main.ts or src/main.server.ts):
import '@angular/localize/init';
3. Create Translation Files
Create JSON translation files for each supported locale. For example:
src/
i18n/
en.json
fr.json
de.json
Each file maps message IDs to translated strings:
{
"greeting": "Bonjour",
"farewell": "Au revoir"
}
Message IDs are generated automatically by Angular's $localize runtime based on the template content, meaning, and description.
4. Provide i18n Configuration
Add provideI18n() to your application config:
// src/app/app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideFileRouter, provideI18n } from '@analogjs/router';
export const appConfig: ApplicationConfig = {
providers: [
provideFileRouter(),
provideI18n({
defaultLocale: 'en',
locales: ['en', 'fr', 'de'],
loader: async (locale) => {
const translations = await import(`../i18n/${locale}.json`);
return translations.default;
},
}),
],
};
The provideI18n() function accepts an I18nConfig object with the following properties:
| Property | Type | Description |
|---|---|---|
defaultLocale | string | The default locale when none is detected |
locales | string[] | List of supported locale identifiers |
loader | (locale: string) => Promise<Record<string, string>> | Record<string, string> | Function that returns translations for a locale |
Using Translations in Templates
Use Angular's i18n attribute to mark text for translation:
<h1 i18n="@@greeting">Hello</h1>
<p i18n="@@farewell">Goodbye</p>
Or use $localize directly in component code:
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
template: `<h1>{{ title }}</h1>`,
})
export class HomeComponent {
title = $localize`:@@greeting:Hello`;
}
Locale Detection
Analog detects the user's locale automatically in both SSR and client-only modes.
Server-Side Rendering
During SSR, the locale is detected from the incoming request using two strategies, in order of priority:
- URL path prefix — A locale prefix in the URL path (e.g.,
/fr/aboutresolves tofr) Accept-Languageheader — The browser's preferred language from the request headers
Client-Only Mode
When SSR is disabled (ssr: false), provideI18n() detects the locale from window.location.pathname by matching the first URL segment against the configured locales list. If no match is found, defaultLocale is used.
Accessing the Current Locale
The detected locale is available through the LOCALE injection token. Inject it anywhere in your application:
import { Component } from '@angular/core';
import { injectLocale } from '@analogjs/router/tokens';
@Component({
selector: 'app-language-switcher',
template: `<span>Current locale: {{ locale }}</span>`,
})
export class LanguageSwitcherComponent {
locale = injectLocale();
}