/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {DirectiveResolver, XHR, i18n} from '@angular/compiler'; import {MockDirectiveResolver} from '@angular/compiler/testing'; import {Compiler, Component, DebugElement, Injector, TRANSLATIONS} from '@angular/core'; import {TestBed, fakeAsync} from '@angular/core/testing'; import {beforeEach, TestComponentBuilder, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit,} from '@angular/core/testing/testing_internal'; import {expect} from '@angular/platform-browser/testing/matchers'; import {By} from '@angular/platform-browser/src/dom/debug/by'; import {SpyXHR} from '../spies'; import {NgLocalization} from '@angular/common'; import {stringifyElement} from '@angular/platform-browser/testing/browser_util'; export function main() { describe('i18n integration spec', () => { let compiler: Compiler; let xhr: SpyXHR; let tcb: TestComponentBuilder; let dirResolver: MockDirectiveResolver; let injector: Injector; beforeEach(() => { TestBed.configureCompiler({ providers: [ {provide: XHR, useClass: SpyXHR}, {provide: NgLocalization, useClass: FrLocalization}, {provide: TRANSLATIONS, useValue: XTB}, ] }); }); beforeEach(fakeAsync(inject( [Compiler, TestComponentBuilder, XHR, DirectiveResolver, Injector], (_compiler: Compiler, _tcb: TestComponentBuilder, _xhr: SpyXHR, _dirResolver: MockDirectiveResolver, _injector: Injector) => { compiler = _compiler; tcb = _tcb; xhr = _xhr; dirResolver = _dirResolver; injector = _injector; }))); it('translate templates', () => { const tb = tcb.createSync(I18nComponent); const cmp = tb.componentInstance; const el = tb.debugElement; expectHtml(el, 'h1').toBe('

attributs i18n sur les balises

'); expectHtml(el, '#i18n-1').toBe('

imbriqué

'); expectHtml(el, '#i18n-2').toBe('

imbriqué

'); expectHtml(el, '#i18n-3') .toBe('

avec des espaces réservés

'); expectHtml(el, '#i18n-4') .toBe('

'); expectHtml(el, '#i18n-5').toBe('

'); expectHtml(el, '#i18n-6').toBe('

'); cmp.count = 0; tb.detectChanges(); expect(el.query(By.css('#i18n-7')).nativeElement).toHaveText('zero'); expect(el.query(By.css('#i18n-14')).nativeElement).toHaveText('zero'); cmp.count = 1; tb.detectChanges(); expect(el.query(By.css('#i18n-7')).nativeElement).toHaveText('un'); expect(el.query(By.css('#i18n-14')).nativeElement).toHaveText('un'); cmp.count = 2; tb.detectChanges(); expect(el.query(By.css('#i18n-7')).nativeElement).toHaveText('deux'); expect(el.query(By.css('#i18n-14')).nativeElement).toHaveText('deux'); cmp.count = 3; tb.detectChanges(); expect(el.query(By.css('#i18n-7')).nativeElement).toHaveText('beaucoup'); expect(el.query(By.css('#i18n-14')).nativeElement).toHaveText('beaucoup'); cmp.sex = 'm'; tb.detectChanges(); expect(el.query(By.css('#i18n-8')).nativeElement).toHaveText('homme'); cmp.sex = 'f'; tb.detectChanges(); expect(el.query(By.css('#i18n-8')).nativeElement).toHaveText('femme'); cmp.count = 123; tb.detectChanges(); expectHtml(el, '#i18n-9').toEqual('
count = 123
'); cmp.sex = 'f'; tb.detectChanges(); expectHtml(el, '#i18n-10').toEqual('
sexe = f
'); expectHtml(el, '#i18n-11').toEqual('
custom name
'); expectHtml(el, '#i18n-12') .toEqual('

Balises dans les commentaires html

'); expectHtml(el, '#i18n-13') .toBe('
'); expectHtml(el, '#i18n-15').toMatch(/ca devrait<\/b> marcher/); }); }); } function expectHtml(el: DebugElement, cssSelector: string): any { return expect(stringifyElement(el.query(By.css(cssSelector)).nativeElement)); } @Component({ selector: 'i18n-cmp', template: `

i18n attribute on tags

nested

nested

with placeholders

{count, plural, =0 {zero} =1 {one} =2 {two} other {many}}
{sex, sex, 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
` }) class I18nComponent { count: number = 0; sex: string = 'm'; } class FrLocalization extends NgLocalization { getPluralCategory(value: number): string { switch (value) { case 0: case 1: return 'one'; default: return 'other'; } } } const XTB = ` attributs i18n sur les balises imbriqué imbriqué avec des espaces réservés sur des balises non traductibles sur des balises traductibles {count, plural, =0 {zero} =1 {un} =2 {deux} other {beaucoup}} {sex, sex, m {homme} f {femme}} sexe = dans une section traductible Balises dans les commentaires html ca devrait marcher `; // unused, for reference only // can be generated from xmb_spec as follow: // `iit('extract xmb', () => { console.log(toXmb(HTML)); });` const XMB = ` i18n attribute on tags nested nested <i>with placeholders</i> on not translatable node on translatable node {count, plural, =0 {zero}=1 {one}=2 {two}other {<b>many</b>}} {sex, sex, m {male}f {female}} sex = in a translatable section <h1>Markers in html comments</h1> <div></div> <div></div> it <b>should</b> work `;