Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 77f77c7

Browse files
committed
docs(i18n): explicit id support in Angular 4.x
based on PR #2941
1 parent 2f28b32 commit 77f77c7

File tree

5 files changed

+68
-21
lines changed

5 files changed

+68
-21
lines changed

public/docs/_examples/cb-i18n/ts/app/app.component.1.html

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ <h1 i18n>Hello i18n!</h1>
1010
<h1 i18n="An introduction header for this sample">Hello i18n!</h1>
1111
<!--#enddocregion i18n-attribute-desc-->
1212

13+
<!--#docregion i18n-attribute-meaning-->
14+
<h1 i18n="site header|An introduction header for this sample">Hello i18n!</h1>
15+
<!--#enddocregion i18n-attribute-meaning-->
16+
17+
<!--#docregion i18n-attribute-id-->
18+
<h1 i18n="An introduction header for this sample@@introductionHeader">Hello i18n!</h1>
19+
<!--#enddocregion i18n-attribute-id-->
20+
21+
<!--#docregion i18n-attribute-meaning-and-id-->
22+
<h1 i18n="site header|An introduction header for this sample@@introductionHeader">Hello i18n!</h1>
23+
<!--#enddocregion i18n-attribute-meaning-and-id-->
24+
25+
<!--#docregion i18n-attribute-solo-id-->
26+
<h1 i18n="@@introductionHeader">Hello i18n!</h1>
27+
<!--#enddocregion i18n-attribute-solo-id-->
28+
1329
<!--#docregion i18n-title-->
1430
<img [src]="logo" title="Angular 2 logo">
1531
<!--#enddocregion i18n-title-->

public/docs/_examples/cb-i18n/ts/app/app.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--#docregion-->
22
<!--#docregion i18n-attribute-meaning-->
3-
<h1 i18n="User welcome|An introduction header for this sample">Hello i18n!</h1>
3+
<h1 i18n="User welcome|An introduction header for this sample@@introductionHeader">Hello i18n!</h1>
44
<!--#enddocregion i18n-attribute-meaning-->
55

66
<!--#docregion i18n-ng-container-->

public/docs/_examples/cb-i18n/ts/locale/messages.es.xlf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
33
<file source-language="en" datatype="plaintext" original="ng2.template">
44
<body>
5-
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
5+
<trans-unit id="@@introductionHeader" datatype="html">
66
<source>Hello i18n!</source>
77
<target>¡Hola i18n!</target>
88
<note priority="1" from="description">An introduction header for this sample</note>

public/docs/_examples/cb-i18n/ts/locale/messages.es.xlf.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<file source-language="en" datatype="plaintext" original="ng2.template">
66
<body>
77
<!-- #docregion translated-hello -->
8-
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
8+
<trans-unit id="@@introductionHeader" datatype="html">
99
<source>Hello i18n!</source>
1010
<target>¡Hola i18n!</target>
1111
<note priority="1" from="description">An introduction header for this sample</note>

public/docs/ts/latest/cookbook/i18n.jade

+49-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ a#top
99
* [Angular and i18n template translation](#angular-i18n)
1010
* [Mark text with the _i18n_ attribute](#i18n-attribute)
1111
* [Add _i18n-..._ translation attributes](#translate-attributes)
12+
* [Set translation ids explicitly](#explicit-id)
1213
* [Handle singular and plural](#cardinality)
1314
* [Select among alternative texts](#select)
1415
* [Create a translation source file with the **_ng-xi18n_ extraction tool**](#ng-xi18n)
@@ -81,7 +82,7 @@ a#i18n-attribute
8182
+makeExample('cb-i18n/ts/app/app.component.1.html', 'i18n-attribute', 'app/app.component.html')(format=".")
8283

8384
:marked
84-
### Help the translator with a _description_ and _intent_
85+
### Help the translator with a _description_ and _meaning_
8586

8687
In order to translate it accurately, the translator may
8788
need a description of the message.
@@ -91,19 +92,42 @@ a#i18n-attribute
9192

9293
:marked
9394
In order to deliver a correct translation, the translator may need to
94-
know your _intent_&mdash;the true _meaning_ of the text
95-
within _this particular_ application context.
96-
In front of the description, add some contextual meaning to the assigned string,
97-
separating it from the description with the `|` character (`<meaning>|<description>`):
95+
know the _meaning_ or _intent_ of the text within _this particular_ application context.
96+
97+
You add context by beginning the string with the _meaning_ and
98+
separating it from the _description_ with the `|` character (`<meaning>|<description>`):
9899

99-
+makeExample('cb-i18n/ts/app/app.component.html', 'i18n-attribute-meaning', 'app/app.component.html')(format=".")
100+
+makeExample('cb-i18n/ts/app/app.component.1.html', 'i18n-attribute-meaning', 'app/app.component.html')(format=".")
100101

101102
:marked
102103
While all appearances of a message with the _same_ meaning have the _same_ translation,
103104
a message with *a variety of possible meanings* could have different translations.
104105
The Angular extraction tool preserves both the _meaning_ and the _description_ in the translation source file
105106
to facilitiate contextually-specific translations.
106107

108+
a#explicit-id
109+
:marked
110+
### Consider setting a custom _id_ explicitly to improve search and maintenance
111+
112+
By default, the angular _i18n_ extractor tool generates a unique ID for each `i18n` attribute in a template.
113+
This _id_ is ugly, arbitrary, and unfit for humans.
114+
115+
Worse, when you change the translatable text, perhaps to fix a typo,
116+
the extractor tool generates a new _id_ for that translation.
117+
The new _id_ most likely breaks the application and it certainly [complicates maintenance](a#maintenance).
118+
119+
Consider specifying your own, meaninful _id_ in the `i18n` attribute, **prefixed with `@@`**.
120+
+makeExample('cb-i18n/ts/app/app.component.1.html', 'i18n-attribute-solo-id', 'app/app.component.html')(format=".")
121+
:marked
122+
The extractor tool and compiler will use your _id_ and never change it.
123+
Here is the attribute with a _definition_, followed by the explicit `id`:
124+
+makeExample('cb-i18n/ts/app/app.component.1.html', 'i18n-attribute-id', 'app/app.component.html')(format=".")
125+
126+
:marked
127+
Here is a _meaning_ and a _description_ and the _id_ at the end:
128+
+makeExample('cb-i18n/ts/app/app.component.1.html', 'i18n-attribute-meaning-and-id', 'app/app.component.html')(format=".")
129+
130+
:marked
107131
### Translate text without creating an element
108132

109133
Suppose there is a stretch of text that you'd like to translate.
@@ -329,18 +353,20 @@ a#localization-folder
329353
replace the `<target/>` tag with the Spanish greeting:
330354
+makeExample('cb-i18n/ts/locale/messages.es.xlf.html', 'translated-hello', 'locale/messages.es.xlf (<trans-unit>, after translation)')(format=".")
331355

332-
.alert.is-important
333-
:marked
334-
Note that the tool generates the `id`. **Don't touch it.**
335-
Its value depends on the content of the message and its assigned meaning.
336-
Change either factor and the `id` changes as well.
337-
See the **[translation file maintenance discussion](#maintenance)**.
338-
339356
:marked
340357
Translate the other text nodes the same way:
341358

342359
+makeExample('cb-i18n/ts/locale/messages.es.xlf.html', 'translated-other-nodes', 'locale/messages.es.xlf (<trans-unit>)')(format=".")
343360

361+
.callout.is-critical
362+
header.
363+
Beware of generated ids
364+
:marked
365+
**The tool generated the `id`s for these translation units. Don't touch them.**
366+
Each `id` depend upon the content of the message and its assigned meaning.
367+
Change either factor and the `id` changes as well.
368+
See the **[translation file maintenance discussion](#maintenance)**.
369+
344370
a#translate-plural-select
345371
:marked
346372
## Translate _plural_ and _select_
@@ -561,16 +587,21 @@ a#maintenance
561587

562588
As the application evolves, you will change the _i18n_ markup
563589
and re-run the `ng-xi18n` extraction tool many times.
564-
The _new_ markup that you add is not a problem;
565-
but _most_ changes to _existing_ markup trigger
566-
generation of _new_ `id`s for the affected translation units.
590+
The _new_ markup that you add is not a problem.
591+
But the `id` _can be a serious problem!_
592+
593+
If the `id` is generated by the tool, _most_ changes to _existing_ markup
594+
cause the tool to generate a _new_ `id` for the affected translation unit.
567595

568596
After an `id` changes, the translation files are no longer in-sync.
569597
**All translated versions of the application will fail** during re-compilation.
570598
The error messages identify the old `id`s that are no longer valid but
571599
they don't tell you what the new `id`s should be.
600+
601+
You can avoid this problem by supplying your own [custom id explicitly](#explicit-id "Set a custom id explicitly").
602+
The tooling preserves your `id` as you make changes to the corresponding translation unit.
572603

573-
**Commit all translation message files to source control**,
604+
Whether you use generated or explicit `ids`, **always commit all translation message files to source control**,
574605
especially the English source `messages.xlf`.
575606
The difference between the old and the new `messages.xlf` file
576-
help you find and update `id` changes across your translation files.
607+
help you find and update `id` and other changes across your translation files.

0 commit comments

Comments
 (0)