From 0ac8e102de9d3d578036755d0aec9bd8ce90747b Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 22 Dec 2016 11:23:35 -0800 Subject: [PATCH] test(i18n): add extraction to integration specs Closes #13648. --- .../compiler/test/i18n/integration_spec.ts | 130 +++++++++--------- 1 file changed, 68 insertions(+), 62 deletions(-) diff --git a/modules/@angular/compiler/test/i18n/integration_spec.ts b/modules/@angular/compiler/test/i18n/integration_spec.ts index 1bcd64aad46c3f..24e0a267172ef7 100644 --- a/modules/@angular/compiler/test/i18n/integration_spec.ts +++ b/modules/@angular/compiler/test/i18n/integration_spec.ts @@ -8,6 +8,10 @@ import {NgLocalization} from '@angular/common'; import {ResourceLoader} from '@angular/compiler'; +import {MessageBundle} from '@angular/compiler/src/i18n/message_bundle'; +import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb'; +import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser'; +import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config'; import {Component, DebugElement, TRANSLATIONS, TRANSLATIONS_FORMAT} from '@angular/core'; import {TestBed, async} from '@angular/core/testing'; import {By} from '@angular/platform-browser/src/dom/debug/by'; @@ -32,9 +36,16 @@ export function main() { TestBed.configureTestingModule({declarations: [I18nComponent]}); })); + it('should extract from templates', () => { + const catalog = new MessageBundle(new HtmlParser, [], {}); + const serializer = new Xmb(); + catalog.updateFromTemplate(HTML, '', DEFAULT_INTERPOLATION_CONFIG); - it('translate templates', () => { - const tb = TestBed.createComponent(I18nComponent); + expect(catalog.write(serializer)).toContain(XMB); + }); + + it('should translate templates', () => { + const tb = TestBed.overrideTemplate(I18nComponent, HTML).createComponent(I18nComponent); const cmp = tb.componentInstance; const el = tb.debugElement; @@ -105,58 +116,7 @@ function expectHtml(el: DebugElement, cssSelector: string): any { @Component({ selector: 'i18n-cmp', - template: ` -
-

i18n attribute on tags

- -

nested

- -

nested

- -

with placeholders

-

with placeholders

- -
-

-

-

-
- - -
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
- -
- {sex, select, m {male} f {female}} -
-
- {sexB, select, m {male} f {female}} -
- -
{{ "count = " + count }}
-
sex = {{ sex }}
-
{{ "custom name" //i18n(ph="CUSTOM_NAME") }}
-
- - -

Markers in html comments

-
-
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
- - -
it should work
- -
with an explicit ID
-
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
- - -
{ - response.getItemsList().length, - plural, - =0 {Found no results} - =1 {Found one result} - other {Found {{response.getItemsList().length}} results} -}
-` + template: '', }) class I18nComponent { count: number; @@ -204,12 +164,7 @@ const XTB = ` {VAR_PLURAL, plural, =0 {Pas de réponse} =1 {une réponse} other {INTERPOLATION réponse} } `; -// unused, for reference only -// can be generated from xmb_spec as follow: -// `fit('extract xmb', () => { console.log(toXmb(HTML)); });` -const XMB = ` - - i18n attribute on tags +const XMB = ` i18n attribute on tags nested nested <i>with placeholders</i> @@ -232,6 +187,57 @@ const XMB = ` it <b>should</b> work with an explicit ID {VAR_PLURAL, plural, =0 {zero} =1 {one} =2 {two} other {<b>many</b>} } - {VAR_PLURAL, plural, =0 {Found no results} =1 {Found one result} other {Found INTERPOLATION results} } - + {VAR_PLURAL, plural, =0 {Found no results} =1 {Found one result} other {Found INTERPOLATION results} }`; + +const HTML = ` +
+

i18n attribute on tags

+ +

nested

+ +

nested

+ +

with placeholders

+

with placeholders

+ +
+

+

+

+
+ + +
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
+ +
+ {sex, select, m {male} f {female}} +
+
+ {sexB, select, m {male} f {female}} +
+ +
{{ "count = " + count }}
+
sex = {{ sex }}
+
{{ "custom name" //i18n(ph="CUSTOM_NAME") }}
+
+ + +

Markers in html comments

+
+
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
+ + +
it should work
+ +
with an explicit ID
+
{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
+ + +
{ + response.getItemsList().length, + plural, + =0 {Found no results} + =1 {Found one result} + other {Found {{response.getItemsList().length}} results} +}
`;