Professional Documents
Culture Documents
(i18n) ts COOKBOOK
INTERNATIONALIZATION (I18N)
Angular's internationalization ("i18n") tools help make your app available in multiple
languages.
Table of contents
Angular and i18n template translation
Mark text with the i18n attribute
Create a translation source 䅉怀le with the ng-xi18n tool
Translate
Merge the completed translation 䅉怀le into the app
JiT con䅉怀guration
AoT con䅉怀guration
https://angular.io/docs/ts/latest/cookbook/i18n.html 1/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
This page describes the i18n tools to assist translation of component template text
into multiple languages.
2. An angular i18n tool extracts the marked messages into an industry standard
translation source 䅉怀le.
3. A translator edits that 䅉怀le, translating the extracted text messages into the
target language, and returns the 䅉怀le to you.
4. The Angular compiler imports the completed translation 䅉怀les, replaces the
original messages with translated text, and generates a new version of the
application in the target language.
You build and deploy a separate version of the application for each supported
language.
https://angular.io/docs/ts/latest/cookbook/i18n.html 2/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
app/app.component.html
<h1>Hello i18n!</h1>
app/app.component.html
app/app.component.html
The true meaning of the text may require some application context. Add a
contextual meaning to the assigned string, separating it from the description with
the | character:
https://angular.io/docs/ts/latest/cookbook/i18n.html 3/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
app/app.component.html
While all appearance of a message with the same meaning should have the same
translation, a message with different meanings could have different translations.
The Angular extraction tool preserves both the meaning and the description in the
translation source 䅉怀le to facilitiate contextually-speci䅉怀c translations.
Open a terminal window at the root of the application project and enter the ng-
xi18n command:
./node_modules/.bin/ng-xi18n
https://angular.io/docs/ts/latest/cookbook/i18n.html 4/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
By default the tool generates a translation 䅉怀le named messages.xlf in the XML
Localisation Interchange File Format (XLIFF, version 1.2).
./node_modules/.bin/ng-xi18n --i18nFormat=xmb
"./node_modules/.bin/ng-xi18n"
"scripts": {
"i18n": "ng-xi18n",
...
}
One approach is to dedicate a folder to localization and store related assets (e.g.
internationalization 䅉怀les) there.
This sample follows that suggestion. It has locale folder immediately under the
project root. Assets within the folder carry a 䅉怀lename extension that matches a
language-culture code from a well-known codeset.
Move messages.xlf into the locale folder where it will become the source for
all language-speci䅉怀c translations. Then make a copy for the French language named
messages.fr.xlf .
Translate
https://angular.io/docs/ts/latest/cookbook/i18n.html 6/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
In the real world, you send the messages.fr.xlf 䅉怀le to a French translator who
would 䅉怀ll in the translations using one of the many XLIFF 䅉怀le editors.
This sample 䅉怀le is easy to translate without a special editor or knowledge of French.
Open messages.fr.xlf and 䅉怀nd the <trans-unit> section:
locale/messages.fr.xlf (<trans-unit>)
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2"
datatype="html">
<source>Hello i18n!</source>
<target/>
<note priority="1" from="description">An introduction header for
this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
This XML element represents the translation of the <h1> greeting tag you marked
with the i18n attribute.
Using the source, description, and meaning elements to guide your translation,
replace the <target/> tag with the French greeting:
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2"
datatype="html">
<source>Hello i18n!</source>
<target>Bonjour i18n!</target>
<note priority="1" from="description">An introduction header for
this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
https://angular.io/docs/ts/latest/cookbook/i18n.html 7/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
Note that the id is generated by the tool. Don't touch it. Its value depends on the
content of the message and its assigned meaning. Change either factor and the id
changes as well.
Repeat the extraction process whenever you add new app messages or edit
existing ones. Be careful not to lose the previous translations. Specialized
software can help manage the change process.
understands.
You provide the Angular compiler with three new pieces of information:
https://angular.io/docs/ts/latest/cookbook/i18n.html 8/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
How you provide this information depends upon whether you compile with the JiT
(Just-in-Time) compiler or the AoT (Ahead-of-Time) compiler.
<script>
// Get the locale id somehow
document.locale = 'fr';
https://angular.io/docs/ts/latest/cookbook/i18n.html 9/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
System.import('app').catch(function(err){ console.error(err);
});
</script>
SystemJS doesn't ship with a raw text plugin but it's easy to add. Create the
following systemjs-text-plugin.js in the root folder:
systemjs-text-plugin.js
/*
SystemJS Text plugin from
https://github.com/systemjs/plugin-text/blob/master/text.js
*/
exports.translate = function(load) {
if (this.builder && this.transpiler) {
load.metadata.format = 'esm';
return 'exp' + 'ort var __useDefault = true; exp' + 'ort
default ' + JSON.stringify(load.source) + ';';
}
load.metadata.format = 'amd';
return 'def' + 'ine(function() {\nreturn ' +
JSON.stringify(load.source) + ';\n});';
}
https://angular.io/docs/ts/latest/cookbook/i18n.html 10/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
app/i18n-providers.ts
https://angular.io/docs/ts/latest/cookbook/i18n.html 11/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
It gets the locale from the global document.locale variable that was set in
index.html .
It creates a transaction 䅉怀lename from the locale according to the name and
location convention described earlier.
The callback composes a providers array with the three translation providers.
https://angular.io/docs/ts/latest/cookbook/i18n.html 12/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
app/main.ts
getTranslationProviders().then(providers => {
const options = { providers };
platformBrowserDynamic().bootstrapModule(AppModule, options);
});
The app is now internationalized for English and French and there is a clear path for
adding more languages.
The AoT (Ahead-of-Time) compiler is part of a build process that produces a small,
fast, ready-to-run application package. When you internationalize with the AoT
compiler, you pre-build a separate application package for each language. Then in
the host web page ( index.html ), you determine which language the user needs
and serve the appropriate application package.
This cookbook doesn't cover how to build multiple application packages and serve
them according to the user's language preference. It does explain the few steps
necessary to tell the AoT to apply a translations 䅉怀le.
Internationalization with the AoT compiler requires some setup speci䅉怀cally for AoT.
Start with application project as shown just before merging the translation 䅉怀le and
refer to the AoT cookbook to make it AoT-ready.
Next, issue an ngc compile command for each supported language (including
English). The result is a separate version of the application for each language.
Tell AoT how to translate by adding three options to the ngc command:
./node_modules/.bin/ngc --i18nFile=./locale/messages.fr.xlf --
locale=fr --i18nFormat=xlf
https://angular.io/docs/ts/latest/cookbook/i18n.html 14/15
11/20/2016 Internationalization (i18n) ts COOKBOOK
"./node_modules/.bin/ngc" --
i18nFile=./locale/messages.fr.xlf --locale=fr --
i18nFormat=xlf
https://angular.io/docs/ts/latest/cookbook/i18n.html 15/15