Internationalization
Translate your app with Angular's @angular/localize. Mark text with i18n or $localize, extract it with ng extract-i18n, and load the right translation when the app starts. One bundle serves every language.
What's different on Lynx
- Translations load at runtime. Angular's build-time translation, with one build per language, isn't supported. Your app calls
loadTranslations()before it starts. - The language comes from the host app. AngularLynx reads
appLocalefrom the host app's GlobalData. The iOS and Android apps thatng build --platformgenerates send the phone's language as a tag such as'fr-FR'. Lynx Explorer sends none, so there your app uses thesourceLocalefrom angular.json. - Lynx has no
Intl.$localizeand Angular's pipes don't need it. For code that callsIntl, the build can add the APIs it uses. - Switching language means restarting the app, for most text. Template
i18ntext andLOCALE_IDdon't change after they're first used. Some strings can switch live.
Set up
This command:
- installs
@angular/localize, at the same version as@angular/core - adds
"i18n": { "sourceLocale": "en-US" }to your project in angular.json - adds
@angular/localize/initto the build'spolyfills, so$localizeexists before your code runs - adds
@angular/localizetotypesin tsconfig.app.json, so TypeScript knows$localize - adds
provideLocale()to your app config - sets up
ng extract-i18nto write your messages as JSON tosrc/locale
provideLocale() sets Angular's LOCALE_ID from appLocale. It falls back to sourceLocale, then to en-US:
- When the host app sends no
appLocale. - When Angular has no locale data for
appLocale. Angular's date and number pipes would throw for it, soprovideLocale()falls back and logs a warning.
So a phone set to a language your app doesn't support gets your source language, as a native app does. Register the data of each language you support with registerLocaleData().
Mark text for translation
i18non an element marks its text for translation.@@greetingsets the message ID.$localizemarks a string in TypeScript.:@@welcome:sets its ID, and:userName:names the placeholder for${this.userName()}.- Give every message an
@@ID. Translation files are keyed by ID, and a custom ID stays the same when you edit the English text.
Extract the messages
It writes src/locale/messages.json with every message in your source language:
Copy it to one file per language, such as messages.fr.json, and translate the values. Keep placeholders like {$userName} as they are.
Why JSON
The JSON format's translations object is exactly what loadTranslations() takes. That's why add-i18n picks it over Angular's default, XLIFF.
To extract XLIFF for a translation tool, pass --format xlf. Then convert each translated file to an object yourself.
The extract-i18n target
ng extract-i18n takes Angular's options. add-i18n sets two of them on the extract-i18n target in angular.json:
The builder runs Angular's own extractor. Angular CLI can't pick an extractor for the AngularLynx build by itself, so an app needs this target:
ng addwrites it.ng update @blotch/angular-lynxadds it to apps made with 22.2.2 or older.
Load a translation
Call loadTranslations() in main.ts, before bootstrapApplication():
getLynxAppLocale()readsappLocalewithout Angular's dependency injection, so it works before the app starts. It returnsundefinedwhen the host app hasn't set one.- Match the language, not the whole tag. The phone may say
'fr-FR','fr-CA'or'fr'. - Load before bootstrap. Angular translates template text and
$localizestrings when it creates each component. It uses whatever was loaded at that moment. - Messages without a translation show the source text and log a warning.
Next steps
- Read and switch the locale: show the current locale, and change language while the app runs.
- Use
Intl: add theIntlAPIs Lynx lacks, for code that formats or sorts by locale.
Related
- Locale API reference:
LynxLocale,provideLocale()andgetLynxAppLocale() - Schematics: what
add-i18nchanges - Pass data from your native app: setting GlobalData such as
appLocale - i18n example
- Angular's i18n guide
