i18n Key Patterns for Angular Applications
How to structure i18n translation keys for Angular applications. Covers Angular built-in i18n, ngx-translate, and Transloco patterns for key organization, lazy loading, and type-safe translations.
Detailed Explanation
i18n Key Patterns for Angular
Angular offers multiple approaches to internationalization, each with different key structure requirements. The three main options are Angular's built-in i18n (compile-time), ngx-translate (runtime), and Transloco (runtime with modern features).
Angular Built-in i18n
Angular's built-in i18n uses XLIFF or XMB files with auto-generated IDs:
<h1 i18n="site title|The main heading@@homeTitle">Welcome to Our App</h1>
<p i18n="@@homeDescription">The best tool for developers</p>
Generated XLIFF:
<trans-unit id="homeTitle" datatype="html">
<source>Welcome to Our App</source>
<target></target>
<note from="description">site title</note>
<note from="meaning">The main heading</note>
</trans-unit>
Key structure: Custom IDs (@@homeTitle) or auto-generated hashes. Custom IDs are recommended for readability.
ngx-translate
ngx-translate uses runtime JSON files with dot-notation keys:
{
"HOME": {
"TITLE": "Welcome to Our App",
"DESCRIPTION": "The best tool for developers",
"CTA": "Get Started"
},
"COMMON": {
"BUTTONS": {
"SUBMIT": "Submit",
"CANCEL": "Cancel"
}
}
}
Usage in templates:
<h1>{{ 'HOME.TITLE' | translate }}</h1>
<button>{{ 'COMMON.BUTTONS.SUBMIT' | translate }}</button>
Convention: ngx-translate projects commonly use SCREAMING_SNAKE_CASE for keys, though this is not required.
Transloco
Transloco is the modern alternative to ngx-translate:
{
"home": {
"title": "Welcome to Our App",
"description": "The best tool for developers"
},
"common": {
"buttons": {
"submit": "Submit",
"cancel": "Cancel"
}
}
}
Usage:
<ng-container *transloco="let t">
<h1>{{ t('home.title') }}</h1>
<button>{{ t('common.buttons.submit') }}</button>
</ng-container>
Transloco supports scope-based lazy loading:
@NgModule({
providers: [
{ provide: TRANSLOCO_SCOPE, useValue: 'dashboard' }
]
})
export class DashboardModule {}
Angular-Specific Best Practices
- Use custom IDs with built-in i18n -- auto-generated IDs break when source text changes
- Prefer lowercase keys with Transloco -- matches modern JS/TS conventions
- Lazy-load translation scopes per route module for performance
- Use ICU MessageFormat for pluralization and gender -- Angular's built-in i18n supports it natively
- Extract strings with
ng extract-i18nfor built-in i18n ortransloco-keys-managerfor Transloco
Comparison
| Feature | Built-in i18n | ngx-translate | Transloco |
|---|---|---|---|
| Key format | XLIFF/XMB | JSON | JSON |
| Runtime/Compile | Compile | Runtime | Runtime |
| Lazy loading | N/A (compiled) | Manual | Built-in |
| Type safety | N/A | No | Plugin |
| Active maintenance | Yes | Low | Yes |
Use Case
Angular developers choosing between built-in i18n, ngx-translate, and Transloco need to understand how each approach structures keys. Built-in i18n is best for projects that can afford separate builds per locale. ngx-translate and Transloco are better for runtime locale switching. The i18n Key Generator produces keys compatible with all three approaches.
Try It — i18n Key Generator
Related Topics
i18n Key Structure for React Applications
Framework Guide
i18n Key Structure for Vue Applications
Framework Guide
Translation File Formats Comparison: JSON vs YAML vs Properties vs XLIFF vs PO
File Formats
i18n Key Naming Conventions: Dot Notation, snake_case, and camelCase
Naming Conventions
Organizing i18n Keys with Namespaces and Scopes
Key Structure