feat(i18n): pass translation config directly into ngc (#10622)
This commit is contained in:
parent
04c6b2fe85
commit
6580d67875
|
@ -2,3 +2,4 @@
|
||||||
<form><input type="button" [(ngModel)]="ctxProp" name="first"/></form>
|
<form><input type="button" [(ngModel)]="ctxProp" name="first"/></form>
|
||||||
<my-comp *ngIf="ctxBool"></my-comp>
|
<my-comp *ngIf="ctxBool"></my-comp>
|
||||||
<div *ngFor="let x of ctxArr" [attr.value]="x"></div>
|
<div *ngFor="let x of ctxArr" [attr.value]="x"></div>
|
||||||
|
<p id="welcomeMessage"><!--i18n-->Welcome<!--/i18n--></p>
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {NgFor, NgIf} from '@angular/common';
|
import {NgFor, NgIf} from '@angular/common';
|
||||||
import {Component, Inject} from '@angular/core';
|
import {Component, Inject, LOCALE_ID, TRANSLATIONS_FORMAT} from '@angular/core';
|
||||||
import {FORM_DIRECTIVES} from '@angular/forms';
|
import {FORM_DIRECTIVES} from '@angular/forms';
|
||||||
|
|
||||||
import {MultipleComponentsMyComp} from './a/multiple_components';
|
import {MultipleComponentsMyComp} from './a/multiple_components';
|
||||||
|
@ -23,5 +23,9 @@ export class BasicComp {
|
||||||
ctxProp: string;
|
ctxProp: string;
|
||||||
ctxBool: boolean;
|
ctxBool: boolean;
|
||||||
ctxArr: any[] = [];
|
ctxArr: any[] = [];
|
||||||
constructor() { this.ctxProp = 'initialValue'; }
|
constructor(
|
||||||
|
@Inject(LOCALE_ID) public localeId: string,
|
||||||
|
@Inject(TRANSLATIONS_FORMAT) public translationsFormat: string) {
|
||||||
|
this.ctxProp = 'initialValue';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
<translationbundle>
|
||||||
|
<translation id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4">käännä teksti</translation>
|
||||||
|
<translation id="65cc4ab3b4c438e07c89be2b677d08369fb62da2">tervetuloa</translation>
|
||||||
|
</translationbundle>
|
|
@ -44,24 +44,45 @@ describe('template codegen output', () => {
|
||||||
it('should support ngIf', () => {
|
it('should support ngIf', () => {
|
||||||
var compFixture = createComponent(BasicComp);
|
var compFixture = createComponent(BasicComp);
|
||||||
var debugElement = compFixture.debugElement;
|
var debugElement = compFixture.debugElement;
|
||||||
expect(debugElement.children.length).toBe(2);
|
expect(debugElement.children.length).toBe(3);
|
||||||
|
|
||||||
compFixture.componentInstance.ctxBool = true;
|
compFixture.componentInstance.ctxBool = true;
|
||||||
compFixture.detectChanges();
|
compFixture.detectChanges();
|
||||||
expect(debugElement.children.length).toBe(3);
|
expect(debugElement.children.length).toBe(4);
|
||||||
expect(debugElement.children[2].injector.get(MultipleComponentsMyComp)).toBeTruthy();
|
expect(debugElement.children[2].injector.get(MultipleComponentsMyComp)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support ngFor', () => {
|
it('should support ngFor', () => {
|
||||||
var compFixture = createComponent(BasicComp);
|
var compFixture = createComponent(BasicComp);
|
||||||
var debugElement = compFixture.debugElement;
|
var debugElement = compFixture.debugElement;
|
||||||
expect(debugElement.children.length).toBe(2);
|
expect(debugElement.children.length).toBe(3);
|
||||||
|
|
||||||
// test NgFor
|
// test NgFor
|
||||||
compFixture.componentInstance.ctxArr = [1, 2];
|
compFixture.componentInstance.ctxArr = [1, 2];
|
||||||
compFixture.detectChanges();
|
compFixture.detectChanges();
|
||||||
expect(debugElement.children.length).toBe(4);
|
expect(debugElement.children.length).toBe(5);
|
||||||
expect(debugElement.children[2].attributes['value']).toBe('1');
|
expect(debugElement.children[2].attributes['value']).toBe('1');
|
||||||
expect(debugElement.children[3].attributes['value']).toBe('2');
|
expect(debugElement.children[3].attributes['value']).toBe('2');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('i18n', () => {
|
||||||
|
it('should inject the locale into the component', () => {
|
||||||
|
const compFixture = createComponent(BasicComp);
|
||||||
|
expect(compFixture.componentInstance.localeId).toEqual('fi');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should inject the translations format into the component', () => {
|
||||||
|
const compFixture = createComponent(BasicComp);
|
||||||
|
expect(compFixture.componentInstance.translationsFormat).toEqual('xtb');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support i18n for content tags', () => {
|
||||||
|
const compFixture = createComponent(BasicComp);
|
||||||
|
const debugElement = compFixture.debugElement;
|
||||||
|
const containerElement = <any>debugElement.nativeElement;
|
||||||
|
const pElement = <any>containerElement.children.find((c: any) => c.name == 'p');
|
||||||
|
const pText = <string>pElement.children.map((c: any) => c.data).join('').trim();
|
||||||
|
expect(pText).toBe('tervetuloa');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -39,6 +39,7 @@ describe('template i18n extraction output', () => {
|
||||||
]>
|
]>
|
||||||
<messagebundle>
|
<messagebundle>
|
||||||
<msg id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4" desc="desc" meaning="meaning">translate me</msg>
|
<msg id="76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4" desc="desc" meaning="meaning">translate me</msg>
|
||||||
|
<msg id="65cc4ab3b4c438e07c89be2b677d08369fb62da2">Welcome</msg>
|
||||||
</messagebundle>`;
|
</messagebundle>`;
|
||||||
|
|
||||||
const xmbOutput = path.join(outDir, 'messages.xmb');
|
const xmbOutput = path.join(outDir, 'messages.xmb');
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
*/
|
*/
|
||||||
import * as compiler from '@angular/compiler';
|
import * as compiler from '@angular/compiler';
|
||||||
import {ComponentMetadata, NgModuleMetadata, ViewEncapsulation} from '@angular/core';
|
import {ComponentMetadata, NgModuleMetadata, ViewEncapsulation} from '@angular/core';
|
||||||
import {AngularCompilerOptions} from '@angular/tsc-wrapped';
|
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ import {GENERATED_FILES, ReflectorHost, ReflectorHostContext} from './reflector_
|
||||||
import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities';
|
import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities';
|
||||||
import {StaticReflector, StaticSymbol} from './static_reflector';
|
import {StaticReflector, StaticSymbol} from './static_reflector';
|
||||||
|
|
||||||
|
const nodeFs = require('fs');
|
||||||
|
|
||||||
const PREAMBLE = `/**
|
const PREAMBLE = `/**
|
||||||
* This file is generated by the Angular 2 template compiler.
|
* This file is generated by the Angular 2 template compiler.
|
||||||
* Do not edit.
|
* Do not edit.
|
||||||
|
@ -117,8 +119,9 @@ export class CodeGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
static create(
|
static create(
|
||||||
options: AngularCompilerOptions, program: ts.Program, compilerHost: ts.CompilerHost,
|
options: AngularCompilerOptions, cliOptions: NgcCliOptions, program: ts.Program,
|
||||||
reflectorHostContext?: ReflectorHostContext, xhr?: compiler.XHR): CodeGenerator {
|
compilerHost: ts.CompilerHost, reflectorHostContext?: ReflectorHostContext,
|
||||||
|
xhr?: compiler.XHR): CodeGenerator {
|
||||||
xhr = xhr || {
|
xhr = xhr || {
|
||||||
get: (s: string) => {
|
get: (s: string) => {
|
||||||
if (!compilerHost.fileExists(s)) {
|
if (!compilerHost.fileExists(s)) {
|
||||||
|
@ -128,11 +131,18 @@ export class CodeGenerator {
|
||||||
return Promise.resolve(compilerHost.readFile(s));
|
return Promise.resolve(compilerHost.readFile(s));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const transFile = cliOptions.i18nFile;
|
||||||
|
const locale = cliOptions.locale;
|
||||||
|
let transContent: string = '';
|
||||||
|
if (transFile && locale) {
|
||||||
|
transContent = nodeFs.readFileSync(transFile, 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
const urlResolver: compiler.UrlResolver = compiler.createOfflineCompileUrlResolver();
|
const urlResolver: compiler.UrlResolver = compiler.createOfflineCompileUrlResolver();
|
||||||
const reflectorHost = new ReflectorHost(program, compilerHost, options, reflectorHostContext);
|
const reflectorHost = new ReflectorHost(program, compilerHost, options, reflectorHostContext);
|
||||||
const staticReflector = new StaticReflector(reflectorHost);
|
const staticReflector = new StaticReflector(reflectorHost);
|
||||||
StaticAndDynamicReflectionCapabilities.install(staticReflector);
|
StaticAndDynamicReflectionCapabilities.install(staticReflector);
|
||||||
const htmlParser = new compiler.i18n.HtmlParser(new HtmlParser());
|
const htmlParser = new compiler.i18n.HtmlParser(new HtmlParser(), transContent);
|
||||||
const config = new compiler.CompilerConfig({
|
const config = new compiler.CompilerConfig({
|
||||||
genDebugInfo: options.debug === true,
|
genDebugInfo: options.debug === true,
|
||||||
defaultEncapsulation: ViewEncapsulation.Emulated,
|
defaultEncapsulation: ViewEncapsulation.Emulated,
|
||||||
|
@ -151,7 +161,8 @@ export class CodeGenerator {
|
||||||
config, console, elementSchemaRegistry, staticReflector);
|
config, console, elementSchemaRegistry, staticReflector);
|
||||||
const offlineCompiler = new compiler.OfflineCompiler(
|
const offlineCompiler = new compiler.OfflineCompiler(
|
||||||
resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
|
resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
|
||||||
new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost));
|
new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost), cliOptions.locale,
|
||||||
|
cliOptions.i18nFormat);
|
||||||
|
|
||||||
return new CodeGenerator(
|
return new CodeGenerator(
|
||||||
options, program, compilerHost, staticReflector, offlineCompiler, reflectorHost);
|
options, program, compilerHost, staticReflector, offlineCompiler, reflectorHost);
|
||||||
|
|
|
@ -28,8 +28,9 @@ import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabi
|
||||||
import {StaticReflector, StaticSymbol} from './static_reflector';
|
import {StaticReflector, StaticSymbol} from './static_reflector';
|
||||||
|
|
||||||
function extract(
|
function extract(
|
||||||
ngOptions: tsc.AngularCompilerOptions, program: ts.Program, host: ts.CompilerHost) {
|
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
|
||||||
const extractor = Extractor.create(ngOptions, program, host);
|
program: ts.Program, host: ts.CompilerHost) {
|
||||||
|
const extractor = Extractor.create(ngOptions, cliOptions.i18nFormat, program, host);
|
||||||
const bundlePromise: Promise<compiler.i18n.MessageBundle> = extractor.extract();
|
const bundlePromise: Promise<compiler.i18n.MessageBundle> = extractor.extract();
|
||||||
|
|
||||||
return (bundlePromise).then(messageBundle => {
|
return (bundlePromise).then(messageBundle => {
|
||||||
|
@ -126,8 +127,8 @@ export class Extractor {
|
||||||
}
|
}
|
||||||
|
|
||||||
static create(
|
static create(
|
||||||
options: tsc.AngularCompilerOptions, program: ts.Program, compilerHost: ts.CompilerHost,
|
options: tsc.AngularCompilerOptions, translationsFormat: string, program: ts.Program,
|
||||||
reflectorHostContext?: ReflectorHostContext): Extractor {
|
compilerHost: ts.CompilerHost, reflectorHostContext?: ReflectorHostContext): Extractor {
|
||||||
const xhr: compiler.XHR = {
|
const xhr: compiler.XHR = {
|
||||||
get: (s: string) => {
|
get: (s: string) => {
|
||||||
if (!compilerHost.fileExists(s)) {
|
if (!compilerHost.fileExists(s)) {
|
||||||
|
@ -163,7 +164,7 @@ export class Extractor {
|
||||||
config, console, elementSchemaRegistry, staticReflector);
|
config, console, elementSchemaRegistry, staticReflector);
|
||||||
const offlineCompiler = new compiler.OfflineCompiler(
|
const offlineCompiler = new compiler.OfflineCompiler(
|
||||||
resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
|
resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
|
||||||
new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost));
|
new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost), null, null);
|
||||||
|
|
||||||
// TODO(vicb): implicit tags & attributes
|
// TODO(vicb): implicit tags & attributes
|
||||||
let messageBundle = new compiler.i18n.MessageBundle(htmlParser, [], {});
|
let messageBundle = new compiler.i18n.MessageBundle(htmlParser, [], {});
|
||||||
|
@ -183,11 +184,13 @@ interface FileMetadata {
|
||||||
// Entry point
|
// Entry point
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
const args = require('minimist')(process.argv.slice(2));
|
const args = require('minimist')(process.argv.slice(2));
|
||||||
tsc.main(args.p || args.project || '.', args.basePath, extract)
|
const project = args.p || args.project || '.';
|
||||||
.then(exitCode => process.exit(exitCode))
|
const cliOptions = new tsc.I18nExtractionCliOptions(args);
|
||||||
.catch(e => {
|
tsc.main(project, cliOptions, extract)
|
||||||
|
.then((exitCode: any) => process.exit(exitCode))
|
||||||
|
.catch((e: any) => {
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
console.error('Extraction failed');
|
console.error('Extraction failed');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,18 +17,19 @@ import * as tsc from '@angular/tsc-wrapped';
|
||||||
import {CodeGenerator} from './codegen';
|
import {CodeGenerator} from './codegen';
|
||||||
|
|
||||||
function codegen(
|
function codegen(
|
||||||
ngOptions: tsc.AngularCompilerOptions, program: ts.Program, host: ts.CompilerHost) {
|
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.NgcCliOptions, program: ts.Program,
|
||||||
return CodeGenerator.create(ngOptions, program, host).codegen();
|
host: ts.CompilerHost) {
|
||||||
|
return CodeGenerator.create(ngOptions, cliOptions, program, host).codegen();
|
||||||
}
|
}
|
||||||
|
|
||||||
// CLI entry point
|
// CLI entry point
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
const args = require('minimist')(process.argv.slice(2));
|
const args = require('minimist')(process.argv.slice(2));
|
||||||
tsc.main(args.p || args.project || '.', args.basePath, codegen)
|
const project = args.p || args.project || '.';
|
||||||
.then(exitCode => process.exit(exitCode))
|
const cliOptions = new tsc.NgcCliOptions(args);
|
||||||
.catch(e => {
|
tsc.main(project, cliOptions, codegen).then(exitCode => process.exit(exitCode)).catch(e => {
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
console.error('Compilation failed');
|
console.error('Compilation failed');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {COMPILER_OPTIONS, Compiler, CompilerFactory, CompilerOptions, Component, Inject, Injectable, OptionalMetadata, PLATFORM_DIRECTIVES, PLATFORM_INITIALIZER, PLATFORM_PIPES, PlatformRef, Provider, ReflectiveInjector, Type, ViewEncapsulation, createPlatformFactory, isDevMode, platformCore} from '@angular/core';
|
import {COMPILER_OPTIONS, Compiler, CompilerFactory, CompilerOptions, Component, Inject, Injectable, OptionalMetadata, PLATFORM_DIRECTIVES, PLATFORM_INITIALIZER, PLATFORM_PIPES, PlatformRef, Provider, ReflectiveInjector, TRANSLATIONS, Type, ViewEncapsulation, createPlatformFactory, isDevMode, platformCore} from '@angular/core';
|
||||||
|
|
||||||
export * from './template_parser/template_ast';
|
export * from './template_parser/template_ast';
|
||||||
export {TEMPLATE_TRANSFORMS} from './template_parser/template_parser';
|
export {TEMPLATE_TRANSFORMS} from './template_parser/template_parser';
|
||||||
|
@ -65,7 +65,7 @@ export const COMPILER_PROVIDERS: Array<any|Type<any>|{[k: string]: any}|any[]> =
|
||||||
provide: i18n.HtmlParser,
|
provide: i18n.HtmlParser,
|
||||||
useFactory: (parser: HtmlParser, translations: string) =>
|
useFactory: (parser: HtmlParser, translations: string) =>
|
||||||
new i18n.HtmlParser(parser, translations),
|
new i18n.HtmlParser(parser, translations),
|
||||||
deps: [HtmlParser, [new OptionalMetadata(), new Inject(i18n.TRANSLATIONS)]]
|
deps: [HtmlParser, [new OptionalMetadata(), new Inject(TRANSLATIONS)]]
|
||||||
},
|
},
|
||||||
TemplateParser,
|
TemplateParser,
|
||||||
DirectiveNormalizer,
|
DirectiveNormalizer,
|
||||||
|
|
|
@ -6,12 +6,8 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {OpaqueToken} from '@angular/core';
|
|
||||||
|
|
||||||
export {HtmlParser} from './html_parser';
|
export {HtmlParser} from './html_parser';
|
||||||
export {MessageBundle} from './message_bundle';
|
export {MessageBundle} from './message_bundle';
|
||||||
export {Serializer} from './serializers/serializer';
|
export {Serializer} from './serializers/serializer';
|
||||||
export {Xmb} from './serializers/xmb';
|
export {Xmb} from './serializers/xmb';
|
||||||
export {Xtb} from './serializers/xtb';
|
export {Xtb} from './serializers/xtb';
|
||||||
|
|
||||||
export const TRANSLATIONS = new OpaqueToken('Translations');
|
|
||||||
|
|
|
@ -6,8 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {ANALYZE_FOR_ENTRY_COMPONENTS, ChangeDetectionStrategy, ChangeDetectorRef, ComponentFactory, ComponentFactoryResolver, ElementRef, Injector, NgModuleFactory, QueryList, RenderComponentType, Renderer, SecurityContext, SimpleChange, TemplateRef, ViewContainerRef, ViewEncapsulation} from '@angular/core';
|
import {ANALYZE_FOR_ENTRY_COMPONENTS, ChangeDetectionStrategy, ChangeDetectorRef, ComponentFactory, ComponentFactoryResolver, ElementRef, Injector, LOCALE_ID as LOCALE_ID_, NgModuleFactory, QueryList, RenderComponentType, Renderer, SecurityContext, SimpleChange, TRANSLATIONS_FORMAT as TRANSLATIONS_FORMAT_, TemplateRef, ViewContainerRef, ViewEncapsulation} from '@angular/core';
|
||||||
|
|
||||||
import {AnimationGroupPlayer as AnimationGroupPlayer_, AnimationKeyframe as AnimationKeyframe_, AnimationSequencePlayer as AnimationSequencePlayer_, AnimationStyles as AnimationStyles_, AppElement, AppView, ChangeDetectorStatus, CodegenComponentFactoryResolver, DebugAppView, DebugContext, EMPTY_ARRAY, EMPTY_MAP, NgModuleInjector, NoOpAnimationPlayer as NoOpAnimationPlayer_, StaticNodeDebugInfo, TemplateRef_, UNINITIALIZED, ValueUnwrapper, ViewType, ViewUtils, balanceAnimationKeyframes as impBalanceAnimationKeyframes, castByValue, checkBinding, clearStyles as impClearStyles, collectAndResolveStyles as impCollectAndResolveStyles, devModeEqual, flattenNestedViewRenderNodes, interpolate, prepareFinalAnimationStyles as impBalanceAnimationStyles, pureProxy1, pureProxy10, pureProxy2, pureProxy3, pureProxy4, pureProxy5, pureProxy6, pureProxy7, pureProxy8, pureProxy9, renderStyles as impRenderStyles} from '../core_private';
|
import {AnimationGroupPlayer as AnimationGroupPlayer_, AnimationKeyframe as AnimationKeyframe_, AnimationSequencePlayer as AnimationSequencePlayer_, AnimationStyles as AnimationStyles_, AppElement, AppView, ChangeDetectorStatus, CodegenComponentFactoryResolver, DebugAppView, DebugContext, EMPTY_ARRAY, EMPTY_MAP, NgModuleInjector, NoOpAnimationPlayer as NoOpAnimationPlayer_, StaticNodeDebugInfo, TemplateRef_, UNINITIALIZED, ValueUnwrapper, ViewType, ViewUtils, balanceAnimationKeyframes as impBalanceAnimationKeyframes, castByValue, checkBinding, clearStyles as impClearStyles, collectAndResolveStyles as impCollectAndResolveStyles, devModeEqual, flattenNestedViewRenderNodes, interpolate, prepareFinalAnimationStyles as impBalanceAnimationStyles, pureProxy1, pureProxy10, pureProxy2, pureProxy3, pureProxy4, pureProxy5, pureProxy6, pureProxy7, pureProxy8, pureProxy9, renderStyles as impRenderStyles} from '../core_private';
|
||||||
|
|
||||||
import {CompileIdentifierMetadata, CompileTokenMetadata} from './compile_metadata';
|
import {CompileIdentifierMetadata, CompileTokenMetadata} from './compile_metadata';
|
||||||
|
@ -252,6 +251,13 @@ export class Identifiers {
|
||||||
moduleUrl: ANIMATION_STYLE_UTIL_ASSET_URL,
|
moduleUrl: ANIMATION_STYLE_UTIL_ASSET_URL,
|
||||||
runtime: impCollectAndResolveStyles
|
runtime: impCollectAndResolveStyles
|
||||||
});
|
});
|
||||||
|
static LOCALE_ID = new CompileIdentifierMetadata(
|
||||||
|
{name: 'LOCALE_ID', moduleUrl: assetUrl('core', 'i18n/tokens'), runtime: LOCALE_ID_});
|
||||||
|
static TRANSLATIONS_FORMAT = new CompileIdentifierMetadata({
|
||||||
|
name: 'TRANSLATIONS_FORMAT',
|
||||||
|
moduleUrl: assetUrl('core', 'i18n/tokens'),
|
||||||
|
runtime: TRANSLATIONS_FORMAT_
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function identifierToken(identifier: CompileIdentifierMetadata): CompileTokenMetadata {
|
export function identifierToken(identifier: CompileIdentifierMetadata): CompileTokenMetadata {
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import {BaseException, SchemaMetadata} from '@angular/core';
|
import {BaseException, SchemaMetadata} from '@angular/core';
|
||||||
|
|
||||||
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
|
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeMetadata, CompileProviderMetadata, CompileTokenMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
|
||||||
import {DirectiveNormalizer} from './directive_normalizer';
|
import {DirectiveNormalizer} from './directive_normalizer';
|
||||||
import {ListWrapper} from './facade/collection';
|
import {ListWrapper} from './facade/collection';
|
||||||
import {Identifiers} from './identifiers';
|
import {Identifiers} from './identifiers';
|
||||||
|
@ -33,7 +33,8 @@ export class OfflineCompiler {
|
||||||
private _metadataResolver: CompileMetadataResolver,
|
private _metadataResolver: CompileMetadataResolver,
|
||||||
private _directiveNormalizer: DirectiveNormalizer, private _templateParser: TemplateParser,
|
private _directiveNormalizer: DirectiveNormalizer, private _templateParser: TemplateParser,
|
||||||
private _styleCompiler: StyleCompiler, private _viewCompiler: ViewCompiler,
|
private _styleCompiler: StyleCompiler, private _viewCompiler: ViewCompiler,
|
||||||
private _ngModuleCompiler: NgModuleCompiler, private _outputEmitter: OutputEmitter) {}
|
private _ngModuleCompiler: NgModuleCompiler, private _outputEmitter: OutputEmitter,
|
||||||
|
private _localeId: string, private _translationFormat: string) {}
|
||||||
|
|
||||||
analyzeModules(ngModules: StaticSymbol[]): NgModulesSummary {
|
analyzeModules(ngModules: StaticSymbol[]): NgModulesSummary {
|
||||||
const ngModuleByComponent = new Map<StaticSymbol, CompileNgModuleMetadata>();
|
const ngModuleByComponent = new Map<StaticSymbol, CompileNgModuleMetadata>();
|
||||||
|
@ -107,7 +108,16 @@ export class OfflineCompiler {
|
||||||
|
|
||||||
private _compileModule(ngModuleType: StaticSymbol, targetStatements: o.Statement[]): string {
|
private _compileModule(ngModuleType: StaticSymbol, targetStatements: o.Statement[]): string {
|
||||||
const ngModule = this._metadataResolver.getNgModuleMetadata(<any>ngModuleType);
|
const ngModule = this._metadataResolver.getNgModuleMetadata(<any>ngModuleType);
|
||||||
let appCompileResult = this._ngModuleCompiler.compile(ngModule, []);
|
let appCompileResult = this._ngModuleCompiler.compile(ngModule, [
|
||||||
|
new CompileProviderMetadata({
|
||||||
|
token: new CompileTokenMetadata({identifier: Identifiers.LOCALE_ID}),
|
||||||
|
useValue: this._localeId
|
||||||
|
}),
|
||||||
|
new CompileProviderMetadata({
|
||||||
|
token: new CompileTokenMetadata({identifier: Identifiers.TRANSLATIONS_FORMAT}),
|
||||||
|
useValue: this._translationFormat
|
||||||
|
})
|
||||||
|
]);
|
||||||
appCompileResult.dependencies.forEach((dep) => {
|
appCompileResult.dependencies.forEach((dep) => {
|
||||||
dep.placeholder.name = _componentFactoryName(dep.comp);
|
dep.placeholder.name = _componentFactoryName(dep.comp);
|
||||||
dep.placeholder.moduleUrl = _ngfactoryModuleUrl(dep.comp.moduleUrl);
|
dep.placeholder.moduleUrl = _ngfactoryModuleUrl(dep.comp.moduleUrl);
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
|
|
||||||
import {DirectiveResolver, XHR, i18n} from '@angular/compiler';
|
import {DirectiveResolver, XHR, i18n} from '@angular/compiler';
|
||||||
import {MockDirectiveResolver} from '@angular/compiler/testing';
|
import {MockDirectiveResolver} from '@angular/compiler/testing';
|
||||||
import {Compiler, Component, DebugElement, Injector} from '@angular/core';
|
import {Compiler, Component, DebugElement, Injector, TRANSLATIONS} from '@angular/core';
|
||||||
import {TestBed, TestComponentBuilder, fakeAsync} from '@angular/core/testing';
|
import {TestBed, TestComponentBuilder, fakeAsync} from '@angular/core/testing';
|
||||||
|
|
||||||
import {beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit,} from '@angular/core/testing/testing_internal';
|
import {beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit,} from '@angular/core/testing/testing_internal';
|
||||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||||
|
@ -30,7 +31,7 @@ export function main() {
|
||||||
providers: [
|
providers: [
|
||||||
{provide: XHR, useClass: SpyXHR},
|
{provide: XHR, useClass: SpyXHR},
|
||||||
{provide: NgLocalization, useClass: FrLocalization},
|
{provide: NgLocalization, useClass: FrLocalization},
|
||||||
{provide: i18n.TRANSLATIONS, useValue: XTB},
|
{provide: TRANSLATIONS, useValue: XTB},
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -213,4 +214,4 @@ const XMB = `
|
||||||
<ph name="START_TAG_DIV_1"><ex><div></ex></ph><ph name="ICU"/><ph name="CLOSE_TAG_DIV"><ex></div></ex></ph>
|
<ph name="START_TAG_DIV_1"><ex><div></ex></ph><ph name="ICU"/><ph name="CLOSE_TAG_DIV"><ex></div></ex></ph>
|
||||||
</msg>
|
</msg>
|
||||||
<msg id="93a30c67d4e6c9b37aecfe2ac0f2b5d366d7b520">it <ph name="START_BOLD_TEXT"><ex><b></ex></ph>should<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> work</msg>
|
<msg id="93a30c67d4e6c9b37aecfe2ac0f2b5d366d7b520">it <ph name="START_BOLD_TEXT"><ex><b></ex></ph>should<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> work</msg>
|
||||||
</messagebundle>`;
|
</messagebundle>`;
|
||||||
|
|
|
@ -25,6 +25,7 @@ export * from './src/testability/testability';
|
||||||
export * from './src/change_detection';
|
export * from './src/change_detection';
|
||||||
export * from './src/platform_directives_and_pipes';
|
export * from './src/platform_directives_and_pipes';
|
||||||
export * from './src/platform_core_providers';
|
export * from './src/platform_core_providers';
|
||||||
|
export {TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID} from './src/i18n/tokens';
|
||||||
export {APPLICATION_COMMON_PROVIDERS, ApplicationModule} from './src/application_module';
|
export {APPLICATION_COMMON_PROVIDERS, ApplicationModule} from './src/application_module';
|
||||||
export {wtfCreateScope, wtfLeave, wtfStartTimeRange, wtfEndTimeRange, WtfScopeFn} from './src/profile/profile';
|
export {wtfCreateScope, wtfLeave, wtfStartTimeRange, wtfEndTimeRange, WtfScopeFn} from './src/profile/profile';
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/**
|
||||||
|
* @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 {OpaqueToken} from '../di/opaque_token';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @experimental i18n support is experimental.
|
||||||
|
*/
|
||||||
|
export const LOCALE_ID = new OpaqueToken('LocaleId');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @experimental i18n support is experimental.
|
||||||
|
*/
|
||||||
|
export const TRANSLATIONS = new OpaqueToken('Translations');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @experimental i18n support is experimental.
|
||||||
|
*/
|
||||||
|
export const TRANSLATIONS_FORMAT = new OpaqueToken('TranslationsFormat');
|
|
@ -33,7 +33,7 @@ cp -v package.json $TMP
|
||||||
|
|
||||||
./node_modules/.bin/tsc --version
|
./node_modules/.bin/tsc --version
|
||||||
# Compile the compiler-cli integration tests
|
# Compile the compiler-cli integration tests
|
||||||
./node_modules/.bin/ngc
|
./node_modules/.bin/ngc --i18nFile=src/messages.fi.xtb --locale=fi --i18nFormat=xtb
|
||||||
./node_modules/.bin/ng-xi18n
|
./node_modules/.bin/ng-xi18n
|
||||||
|
|
||||||
./node_modules/.bin/jasmine init
|
./node_modules/.bin/jasmine init
|
||||||
|
|
|
@ -2,5 +2,6 @@ export {MetadataWriterHost, TsickleHost} from './src/compiler_host';
|
||||||
export {CodegenExtension, main} from './src/main';
|
export {CodegenExtension, main} from './src/main';
|
||||||
|
|
||||||
export {default as AngularCompilerOptions} from './src/options';
|
export {default as AngularCompilerOptions} from './src/options';
|
||||||
|
export * from './src/cli_options';
|
||||||
export * from './src/collector';
|
export * from './src/collector';
|
||||||
export * from './src/schema';
|
export * from './src/schema';
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
export class CliOptions {
|
||||||
|
public basePath: string;
|
||||||
|
constructor({basePath = null}: {basePath?: string}) { this.basePath = basePath; }
|
||||||
|
}
|
||||||
|
|
||||||
|
export class I18nExtractionCliOptions extends CliOptions {
|
||||||
|
public i18nFormat: string;
|
||||||
|
|
||||||
|
constructor({i18nFormat = null}: {i18nFormat?: string}) {
|
||||||
|
super({});
|
||||||
|
this.i18nFormat = i18nFormat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NgcCliOptions extends CliOptions {
|
||||||
|
public i18nFormat: string;
|
||||||
|
public i18nFile: string;
|
||||||
|
public locale: string;
|
||||||
|
|
||||||
|
constructor({i18nFormat = null, i18nFile = null, locale = null, basePath = null}:
|
||||||
|
{i18nFormat?: string, i18nFile?: string, locale?: string, basePath?: string}) {
|
||||||
|
super({basePath: basePath});
|
||||||
|
this.i18nFormat = i18nFormat;
|
||||||
|
this.i18nFile = i18nFile;
|
||||||
|
this.locale = locale;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,11 +6,14 @@ import {check, tsc} from './tsc';
|
||||||
|
|
||||||
import NgOptions from './options';
|
import NgOptions from './options';
|
||||||
import {MetadataWriterHost, TsickleHost} from './compiler_host';
|
import {MetadataWriterHost, TsickleHost} from './compiler_host';
|
||||||
|
import {CliOptions} from './cli_options';
|
||||||
|
|
||||||
export type CodegenExtension = (ngOptions: NgOptions, program: ts.Program, host: ts.CompilerHost) =>
|
export type CodegenExtension =
|
||||||
Promise<void>;
|
(ngOptions: NgOptions, cliOptions: CliOptions, program: ts.Program, host: ts.CompilerHost) =>
|
||||||
|
Promise<void>;
|
||||||
|
|
||||||
export function main(project: string, basePath?: string, codegen?: CodegenExtension): Promise<any> {
|
export function main(
|
||||||
|
project: string, cliOptions: CliOptions, codegen?: CodegenExtension): Promise<any> {
|
||||||
try {
|
try {
|
||||||
let projectDir = project;
|
let projectDir = project;
|
||||||
if (fs.lstatSync(project).isFile()) {
|
if (fs.lstatSync(project).isFile()) {
|
||||||
|
@ -18,7 +21,7 @@ export function main(project: string, basePath?: string, codegen?: CodegenExtens
|
||||||
}
|
}
|
||||||
|
|
||||||
// file names in tsconfig are resolved relative to this absolute path
|
// file names in tsconfig are resolved relative to this absolute path
|
||||||
basePath = path.resolve(process.cwd(), basePath || projectDir);
|
const basePath = path.resolve(process.cwd(), cliOptions.basePath || projectDir);
|
||||||
|
|
||||||
// read the configuration options from wherever you store them
|
// read the configuration options from wherever you store them
|
||||||
const {parsed, ngOptions} = tsc.readConfiguration(project, basePath);
|
const {parsed, ngOptions} = tsc.readConfiguration(project, basePath);
|
||||||
|
@ -32,7 +35,7 @@ export function main(project: string, basePath?: string, codegen?: CodegenExtens
|
||||||
if (ngOptions.skipTemplateCodegen || !codegen) {
|
if (ngOptions.skipTemplateCodegen || !codegen) {
|
||||||
codegen = () => Promise.resolve(null);
|
codegen = () => Promise.resolve(null);
|
||||||
}
|
}
|
||||||
return codegen(ngOptions, program, host).then(() => {
|
return codegen(ngOptions, cliOptions, program, host).then(() => {
|
||||||
// Create a new program since codegen files were created after making the old program
|
// Create a new program since codegen files were created after making the old program
|
||||||
const newProgram = ts.createProgram(parsed.fileNames, parsed.options, host, program);
|
const newProgram = ts.createProgram(parsed.fileNames, parsed.options, host, program);
|
||||||
tsc.typeCheck(host, newProgram);
|
tsc.typeCheck(host, newProgram);
|
||||||
|
@ -59,11 +62,11 @@ export function main(project: string, basePath?: string, codegen?: CodegenExtens
|
||||||
// CLI entry point
|
// CLI entry point
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
const args = require('minimist')(process.argv.slice(2));
|
const args = require('minimist')(process.argv.slice(2));
|
||||||
main(args.p || args.project || '.', args.basePath)
|
const project = args.p || args.project || '.';
|
||||||
.then(exitCode => process.exit(exitCode))
|
const cliOptions = new CliOptions(args);
|
||||||
.catch(e => {
|
main(project, cliOptions).then((exitCode: any) => process.exit(exitCode)).catch((e: any) => {
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
console.error('Compilation failed');
|
console.error('Compilation failed');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -780,6 +780,9 @@ export declare class KeyValueDiffers {
|
||||||
static extend(factories: KeyValueDifferFactory[]): Provider;
|
static extend(factories: KeyValueDifferFactory[]): Provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @experimental */
|
||||||
|
export declare const LOCALE_ID: OpaqueToken;
|
||||||
|
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
export declare function lockRunMode(): void;
|
export declare function lockRunMode(): void;
|
||||||
|
|
||||||
|
@ -1330,6 +1333,12 @@ export interface TrackByFn {
|
||||||
/** @experimental */
|
/** @experimental */
|
||||||
export declare function transition(stateChangeExpr: string, steps: AnimationMetadata | AnimationMetadata[]): AnimationStateTransitionMetadata;
|
export declare function transition(stateChangeExpr: string, steps: AnimationMetadata | AnimationMetadata[]): AnimationStateTransitionMetadata;
|
||||||
|
|
||||||
|
/** @experimental */
|
||||||
|
export declare const TRANSLATIONS: OpaqueToken;
|
||||||
|
|
||||||
|
/** @experimental */
|
||||||
|
export declare const TRANSLATIONS_FORMAT: OpaqueToken;
|
||||||
|
|
||||||
/** @experimental */
|
/** @experimental */
|
||||||
export declare function trigger(name: string, animation: AnimationMetadata[]): AnimationEntryMetadata;
|
export declare function trigger(name: string, animation: AnimationMetadata[]): AnimationEntryMetadata;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue