Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(compiler): add a version parameter for i18n extraction
`i18nVersion` could be '0' or '1'
  • Loading branch information
ocombe committed Jul 17, 2017
commit ed6b3064f508999606b83c594884abbb2102fb06
15 changes: 10 additions & 5 deletions packages/compiler-cli/src/extract_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
*/


/**
* Extract i18n messages from source code
*/
// Must be imported first, because Angular decorators throw on load.
import 'reflect-metadata';

import * as tsc from '@angular/tsc-wrapped';
import * as ts from 'typescript';

import {normalizeI18nVersion, normalizeI18nFormat} from './i18n_options';

import {Extractor} from './extractor';

/**
* Extract i18n messages from the Angular templates
*/
function extract(
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
program: ts.Program, host: ts.CompilerHost) {
return Extractor.create(ngOptions, program, host, cliOptions.locale)
.extract(cliOptions.i18nFormat !, cliOptions.outFile);
const version = normalizeI18nVersion(cliOptions.i18nVersion);
const format = normalizeI18nFormat(cliOptions.i18nFormat);

return Extractor.create(version, ngOptions, program, host, cliOptions.locale)
.extract(format, cliOptions.outFile);
}

// Entry point
Expand Down
46 changes: 15 additions & 31 deletions packages/compiler-cli/src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
*/


/**
* Extract i18n messages from source code
*/
// Must be imported first, because Angular decorators throw on load.
import 'reflect-metadata';

Expand All @@ -22,20 +19,23 @@ import * as ts from 'typescript';
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
import {PathMappedCompilerHost} from './path_mapped_compiler_host';

/**
* Extract i18n messages from the Angular templates
*/
export class Extractor {
constructor(
private options: tsc.AngularCompilerOptions, private ngExtractor: compiler.Extractor,
public host: ts.CompilerHost, private ngCompilerHost: CompilerHost,
private program: ts.Program) {}
private version: I18nVersion, private options: tsc.AngularCompilerOptions,
private ngExtractor: compiler.Extractor, public host: ts.CompilerHost,
private ngCompilerHost: CompilerHost, private program: ts.Program) {}

extract(formatName: string, outFile: string|null): Promise<string[]> {
// Checks the format and returns the extension
const ext = this.getExtension(formatName);

const promiseBundle = this.extractBundle();

return promiseBundle.then(bundle => {
const content = this.serialize(bundle, formatName);
return this.extractBundle().then(bundle => {
const serializer = compiler.createSerializer(formatName, this.version);
const content = this.serialize(bundle, serializer);
const dstFile = outFile || `messages.${ext}`;
const dstPath = path.join(this.options.genDir, dstFile);
this.host.writeFile(dstPath, content, false);
Expand All @@ -50,23 +50,7 @@ export class Extractor {
return this.ngExtractor.extract(files);
}

serialize(bundle: compiler.MessageBundle, formatName: string): string {
const format = formatName.toLowerCase();
let serializer: compiler.Serializer;

switch (format) {
case 'xmb':
serializer = new compiler.Xmb(I18nVersion.V0);
break;
case 'xliff2':
case 'xlf2':
serializer = new compiler.Xliff2(I18nVersion.V0);
break;
case 'xlf':
case 'xliff':
default:
serializer = new compiler.Xliff(I18nVersion.V0);
}
serialize(bundle: compiler.MessageBundle, serializer: compiler.Serializer): string {
return bundle.write(
serializer, (sourcePath: string) => this.options.basePath ?
path.relative(this.options.basePath, sourcePath) :
Expand All @@ -91,18 +75,18 @@ export class Extractor {
}

static create(
options: tsc.AngularCompilerOptions, program: ts.Program, tsCompilerHost: ts.CompilerHost,
locale?: string|null, compilerHostContext?: CompilerHostContext,
ngCompilerHost?: CompilerHost): Extractor {
version: I18nVersion, options: tsc.AngularCompilerOptions, program: ts.Program,
tsCompilerHost: ts.CompilerHost, locale?: string|null,
compilerHostContext?: CompilerHostContext, ngCompilerHost?: CompilerHost): Extractor {
if (!ngCompilerHost) {
const usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
const context = compilerHostContext || new ModuleResolutionHostAdapter(tsCompilerHost);
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
new CompilerHost(program, options, context);
}

const {extractor: ngExtractor} = compiler.Extractor.create(ngCompilerHost, locale || null);
const {extractor} = compiler.Extractor.create(version, ngCompilerHost, locale || null);

return new Extractor(options, ngExtractor, tsCompilerHost, ngCompilerHost, program);
return new Extractor(version, options, extractor, tsCompilerHost, ngCompilerHost, program);
}
}
31 changes: 31 additions & 0 deletions packages/compiler-cli/src/i18n_options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @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 {I18nVersion} from '@angular/core';

/** @internal */
export function normalizeI18nFormat(i18nFormat: string | null): string {
return i18nFormat || 'xlf';
}

/** @internal */
export function normalizeI18nVersion(i18nVersion: string | number | null): I18nVersion {
if (i18nVersion) {
switch (i18nVersion) {
case '0':
case 0:
return I18nVersion.V0;
case '1':
case 1:
return I18nVersion.V1;
default:
throw new Error(`Unknown option for i18nVersion (${i18nVersion}). Use either "0" or "1".`);
}
}
return I18nVersion.V0;
}
4 changes: 3 additions & 1 deletion packages/compiler-cli/src/ngtools_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import {AotCompilerHost, AotSummaryResolver, StaticReflector, StaticSymbolCache, StaticSymbolResolver} from '@angular/compiler';
import {I18nVersion} from '@angular/core';
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
import * as ts from 'typescript';

Expand Down Expand Up @@ -153,7 +154,8 @@ export class NgTools_InternalApi_NG_2 {
// Create the i18n extractor.
const locale = options.locale || null;
const extractor = Extractor.create(
options.angularCompilerOptions, options.program, options.host, locale, hostContext);
I18nVersion.V0, options.angularCompilerOptions, options.program, options.host, locale,
hostContext);

return extractor.extract(options.i18nFormat !, options.outFile || null);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/src/i18n/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ export class Extractor {
});
}

static create(host: ExtractorHost, locale: string|null):
static create(version: I18nVersion, host: ExtractorHost, locale: string|null):
{extractor: Extractor, staticReflector: StaticReflector} {
const htmlParser = new I18NHtmlParser(new HtmlParser(), I18nVersion.V0);
const htmlParser = new I18NHtmlParser(new HtmlParser(), version);

const urlResolver = createOfflineCompileUrlResolver();
const symbolCache = new StaticSymbolCache();
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

export {Extractor, ExtractorHost} from './extractor';
export {I18NHtmlParser} from './i18n_html_parser';
export {I18NHtmlParser, createSerializer} from './i18n_html_parser';
export {MessageBundle} from './message_bundle';
export {Serializer} from './serializers/serializer';
export {Xliff} from './serializers/xliff';
Expand Down
4 changes: 4 additions & 0 deletions scripts/ci/offline_compiler_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ cp -v package.json $TMP
# Copy the html files from source to the emitted output
cp flat_module/src/*.html node_modules/flat_module/src

# Test compilation without translations file
./node_modules/.bin/ngc -p tsconfig-build-alt.json --missingTranslation=error --i18nFormat=xlf

# Test compilation with translations file
./node_modules/.bin/ngc -p tsconfig-build.json --i18nFile=src/messages.fi.xlf --locale=fi --i18nFormat=xlf

# Test extraction
./node_modules/.bin/ng-xi18n -p tsconfig-xi18n.json --i18nFormat=xlf --locale=fr
./node_modules/.bin/ng-xi18n -p tsconfig-xi18n.json --i18nFormat=xlf2 --outFile=messages.xliff2.xlf
./node_modules/.bin/ng-xi18n -p tsconfig-xi18n.json --i18nFormat=xmb --outFile=custom_file.xmb
Expand Down
5 changes: 4 additions & 1 deletion tools/@angular/tsc-wrapped/src/cli_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ export class I18nExtractionCliOptions extends CliOptions {
i18nFormat: string|null;
locale: string|null;
outFile: string|null;
i18nVersion: string|null;

constructor({i18nFormat = null, locale = null, outFile = null}: {
constructor({i18nFormat = null, locale = null, outFile = null, i18nVersion = null}: {
i18nFormat?: string,
locale?: string,
outFile?: string,
i18nVersion?: string,
}) {
super({});
this.i18nFormat = i18nFormat;
this.locale = locale;
this.outFile = outFile;
this.i18nVersion = i18nVersion;
}
}

Expand Down