1
1
include ../_util-fns
2
2
3
3
:marked
4
- Angular's _internationalization_ ("_i18n_") tools help make your app availble in multiple languages.
4
+ Angular's _internationalization_ ("_i18n_") tools help make your app available in multiple languages.
5
5
6
6
<a id="top"></a>
7
7
## Table of contents
8
8
9
9
* [Angular and i18n template translation](#angular-i18n)
10
10
* [Mark text with the _i18n_ attribute](#i18n-attribute)
11
- * [Extract text with ng-xi18n ](#ng-xi18n)
11
+ * [Create a translation source file with the _ng-xi18n_ tool ](#ng-xi18n)
12
12
* [Translate](#translate)
13
- * [Merge the translation file into the app](#merge)
13
+ * [Merge the completed translation file into the app](#merge)
14
14
* [JiT configuration](#jit)
15
15
* [AoT configuration](#aot)
16
16
@@ -30,6 +30,7 @@ a#angular-i18n
30
30
This page describes the _i18n_ tools to assist translation of component template text
31
31
into multiple languages.
32
32
33
+
33
34
.l-sub-section
34
35
:marked
35
36
Practitioners of _internationalization_ refer to a translatable text as a "_message_".
@@ -61,7 +62,9 @@ a#i18n-attribute
61
62
62
63
.alert.is-helpful
63
64
:marked
64
- `i18n` is not an Angular _directive_. It's a custom _attribute_, recognized by Angular tools and compilers.
65
+ `i18n` is not an Angular _directive_.
66
+ It's a custom _attribute_, recognized by Angular tools and compilers.
67
+ It will be removed by the compiler _after_ translation.
65
68
66
69
:marked
67
70
In the accompanying sample, an `<h1>` tag displays a simple English language greeting which you will translate to French:
@@ -87,16 +90,17 @@ a#i18n-attribute
87
90
While all appearance of a message with the _same_ meaning should have the _same_ translation,
88
91
a message with *different meanings* could have different translations.
89
92
The Angular extraction tool preserves both the _meaning_ and the _description_ in the translation source file
90
- to facilitiate contextually-specific transations .
93
+ to facilitiate contextually-specific translations .
91
94
92
95
a#ng-xi18n
93
96
.l-main-section
94
97
:marked
95
- ## Extract translatable text with the _ng-xi18n_ command
98
+ ## Create a translation source file with the _ng-xi18n_ tool
96
99
97
- Use the `ng-xi18n` extraction tool to generate a translation source file in an industry standard format.
100
+ Use the `ng-xi18n` extraction tool to extract the `i18n`-marked texts
101
+ into a translation source file in an industry standard format.
98
102
99
- This is an Angular CLI tool based on the `ngc` compiler in the `@angular/compiler-cli` npm package.
103
+ This is an Angular CLI tool in the `@angular/compiler-cli` npm package.
100
104
If you haven't already installed the CLI and its `platform-server` peer dependency, do so now:
101
105
102
106
code-example( language ="sh" class ="code-shell" ) .
@@ -151,18 +155,26 @@ a#translate
151
155
The next step is to translate the English language template text into the specific language translation
152
156
files. The cookbook sample creates a French translation file.
153
157
154
- a#i18n-file-structure
158
+ a#localization-folder
155
159
:marked
156
- ### Create an i18n project structure
160
+ ### Create a localization folder
157
161
158
162
You will probably translate into more than one other language so it's a good idea
159
163
for the project structure to reflect your entire internationalization effort.
160
164
161
- One approach is to create an `i18n` folder with subfolders for each language or locale, e.g. `i18n/fr` for French.
162
- This sample puts `i18n/fr` under the project root.
165
+ One approach is to dedicate a folder to localization and store related assets
166
+ (e.g. internationalization files) there.
167
+ .l-sub-section
168
+ :marked
169
+ Localization and internationalization are
170
+ <a href =" " target =" _blank" >different but closely related terms</a >.
171
+ :marked
172
+ This sample follows that suggestion. It has `locale` folder immediately under the project root.
173
+ Assets within the folder carry a filename extension that matches a language-culture code from a
174
+ <a href="https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx" target="_blank">well-known codeset</a>.
163
175
164
- Move the `messages.xlf` into the `i18n ` folder where it will become the source for all language-specific translations.
165
- Then copy `messages.xlf` into `i18n/fr` and rename it as `messages.fr.xlf` .
176
+ Move `messages.xlf` into the `locale ` folder where it will become the source for all language-specific translations.
177
+ Then make a copy for the French language named `messages.fr.xlf` .
166
178
167
179
Follow the same convention for each target language.
168
180
@@ -174,17 +186,17 @@ a#i18n-file-structure
174
186
This sample file is easy to translate without a special editor or knowledge of French.
175
187
Open `messages.fr.xlf` and find the `<trans-unit>` section:
176
188
177
- + makeExample('cb-i18n/ts/i18n /trans-unit.html' , '' , 'i18n /messages.fr.xlf (<trans-unit>)' )( format ="." )
189
+ + makeExample('cb-i18n/ts/locale /trans-unit.html' , '' , 'locale /messages.fr.xlf (<trans-unit>)' )( format ="." )
178
190
:marked
179
191
This XML element represents the translation of the `<h1>` greeting tag you marked with the `i18n` attribute.
180
192
181
193
Using the _source_, _description_, and _meaning_ elements to guide your translation,
182
194
replace the `<target/>` tag with the French greeting:
183
- + makeExample('cb-i18n/ts/i18n/fr/ messages.fr.xlf.html' , 'trans-unit' , 'i18n/fr /messages.fr.xlf (<trans-unit>, after translation)' )( format ="." )
195
+ + makeExample('cb-i18n/ts/locale/ messages.fr.xlf.html' , 'trans-unit' , 'locale /messages.fr.xlf (<trans-unit>, after translation)' )( format ="." )
184
196
:marked
185
197
Note that the `id` is generated by the tool. Don't touch it.
186
- Its value depends on the content of the message, its meaning, and its description .
187
- Change any of these factors and the `id` changes as well.
198
+ Its value depends on the content of the message and its assigned meaning .
199
+ Change either factor and the `id` changes as well.
188
200
.alert.is-helpful
189
201
:marked
190
202
Repeat the extraction process whenever you add new app messages or edit existing ones.
@@ -202,19 +214,19 @@ a#i18n-file-structure
202
214
cb-i18n/ts/app/app.component.ts,
203
215
cb-i18n/ts/app/app.module.ts,
204
216
cb-i18n/ts/app/main.1.ts,
205
- cb-i18n/ts/i18n/fr /messages.fr.xlf.html
217
+ cb-i18n/ts/locale /messages.fr.xlf.html
206
218
` , '' , `
207
219
app/app.component.html,
208
220
app/app.component.ts,
209
221
app/app.module.ts,
210
222
app/main.ts,
211
- i18n/fr /messages.fr.xlf
223
+ locale /messages.fr.xlf
212
224
` )
213
225
214
226
a#merge
215
227
.l-main-section
216
228
:marked
217
- ## Merge the translation file
229
+ ## Merge the completed translation file
218
230
219
231
To merge the translated text into component templates,
220
232
you compile the application with the completed translation file.
@@ -224,7 +236,8 @@ a#merge
224
236
You provide the Angular compiler with three new pieces of information:
225
237
* the translation file
226
238
* the translation file format
227
- * the _Locale ID_ (`en-US` for instance)
239
+ * the <a href="https://en.wikipedia.org/wiki/XLIFF" target="_blank">_Locale ID_</a>
240
+ (`fr` or `en-US` for instance)
228
241
229
242
_How_ you provide this information depends upon whether you compile with
230
243
the JiT (_Just-in-Time_) compiler or the AoT (_Ahead-of-Time_) compiler.
@@ -280,12 +293,12 @@ a#text-plugin
280
293
:marked
281
294
* It gets the locale from the global `document.locale` variable that was set in `index.html`.
282
295
283
- * If there is no locale or the language is English, there is no need to translate.
296
+ * If there is no locale or the language is U.S. English (`en-US`) , there is no need to translate.
284
297
The function returns an empty `noProviders` array as a `Promise`.
285
298
It must return a `Promise` because this function could read a translation file asynchronously from the server.
286
299
287
300
* It creates a transaction filename from the locale according to the name and location convention
288
- [described earlier](#i18n-file-structure ).
301
+ [described earlier](#localization-folder ).
289
302
290
303
* The `getTranslationsWithSystemJs` method reads the translation and returns the contents as a string.
291
304
Notice that it appends `!text` to the filename, telling SystemJS to use the [text plugin](#text-plugin).
@@ -341,10 +354,10 @@ a#aot
341
354
342
355
For this sample, the French language command would be
343
356
code-example( language ="sh" class ="code-shell" ) .
344
- ./node_modules/.bin/ngc --i18nFile=./i18n/fr /messages.fr.xlf --locale=fr --i18nFormat=xlf
357
+ ./node_modules/.bin/ngc --i18nFile=./locale /messages.fr.xlf --locale=fr --i18nFormat=xlf
345
358
346
359
.l-sub-section
347
360
:marked
348
361
Windows users may have to quote the command:
349
362
code-example( language ="sh" class ="code-shell" ) .
350
- "./node_modules/.bin/ngc" --i18nFile=./i18n/fr /messages.fr.xlf --locale=fr --i18nFormat=xlf
363
+ "./node_modules/.bin/ngc" --i18nFile=./locale /messages.fr.xlf --locale=fr --i18nFormat=xlf
0 commit comments