feat(compiler-cli): add param to set MissingTranslationStrategy on ngc (#15987)

This commit adds a new parameter to ngc named `missingTranslation`  to set the MissingTranslationStrategy for AoT, it takes the value `error`, `warning` or `ignore`.

Fixes #15808

PR Close #15987
This commit is contained in:
Olivier Combe 2017-04-13 09:17:37 +02:00 committed by Miško Hevery
parent 3f46645f5f
commit 6e2abcd5fc
5 changed files with 40 additions and 12 deletions

View File

@ -11,9 +11,9 @@
* Intended to be used in a build step. * Intended to be used in a build step.
*/ */
import * as compiler from '@angular/compiler'; import * as compiler from '@angular/compiler';
import {MissingTranslationStrategy} from '@angular/core';
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped'; import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
import {readFileSync} from 'fs'; import {readFileSync} from 'fs';
import * as path from 'path';
import * as ts from 'typescript'; import * as ts from 'typescript';
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host'; import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
@ -61,20 +61,35 @@ export class CodeGenerator {
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) : ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
new CompilerHost(program, options, context); new CompilerHost(program, options, context);
} }
const transFile = cliOptions.i18nFile;
const locale = cliOptions.locale;
let transContent: string = ''; let transContent: string = '';
if (transFile) { if (cliOptions.i18nFile) {
if (!locale) { if (!cliOptions.locale) {
throw new Error( throw new Error(
`The translation file (${transFile}) locale must be provided. Use the --locale option.`); `The translation file (${cliOptions.i18nFile}) locale must be provided. Use the --locale option.`);
}
transContent = readFileSync(cliOptions.i18nFile, 'utf8');
}
let missingTranslation = MissingTranslationStrategy.Warning;
if (cliOptions.missingTranslation) {
switch (cliOptions.missingTranslation) {
case 'error':
missingTranslation = MissingTranslationStrategy.Error;
break;
case 'warning':
missingTranslation = MissingTranslationStrategy.Warning;
break;
case 'ignore':
missingTranslation = MissingTranslationStrategy.Ignore;
break;
default:
throw new Error(
`Unknown option for missingTranslation (${cliOptions.missingTranslation}). Use either error, warning or ignore.`);
} }
transContent = readFileSync(transFile, 'utf8');
} }
const {compiler: aotCompiler} = compiler.createAotCompiler(ngCompilerHost, { const {compiler: aotCompiler} = compiler.createAotCompiler(ngCompilerHost, {
translations: transContent, translations: transContent,
i18nFormat: cliOptions.i18nFormat, i18nFormat: cliOptions.i18nFormat,
locale: cliOptions.locale, locale: cliOptions.locale, missingTranslation,
enableLegacyTemplate: options.enableLegacyTemplate !== false, enableLegacyTemplate: options.enableLegacyTemplate !== false,
genFilePreamble: PREAMBLE, genFilePreamble: PREAMBLE,
}); });

View File

@ -35,6 +35,7 @@ export interface NgTools_InternalApi_NG2_CodeGen_Options {
i18nFormat?: string; i18nFormat?: string;
i18nFile?: string; i18nFile?: string;
locale?: string; locale?: string;
missingTranslation?: string;
readResource: (fileName: string) => Promise<string>; readResource: (fileName: string) => Promise<string>;
@ -95,6 +96,7 @@ export class NgTools_InternalApi_NG_2 {
i18nFormat: options.i18nFormat !, i18nFormat: options.i18nFormat !,
i18nFile: options.i18nFile !, i18nFile: options.i18nFile !,
locale: options.locale !, locale: options.locale !,
missingTranslation: options.missingTranslation !,
basePath: options.basePath basePath: options.basePath
}; };

View File

@ -51,12 +51,12 @@ export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCom
StaticAndDynamicReflectionCapabilities.install(staticReflector); StaticAndDynamicReflectionCapabilities.install(staticReflector);
const console = new Console(); const console = new Console();
const htmlParser = new I18NHtmlParser( const htmlParser = new I18NHtmlParser(
new HtmlParser(), translations, options.i18nFormat, MissingTranslationStrategy.Warning, new HtmlParser(), translations, options.i18nFormat, options.missingTranslation, console);
console);
const config = new CompilerConfig({ const config = new CompilerConfig({
defaultEncapsulation: ViewEncapsulation.Emulated, defaultEncapsulation: ViewEncapsulation.Emulated,
useJit: false, useJit: false,
enableLegacyTemplate: options.enableLegacyTemplate !== false, enableLegacyTemplate: options.enableLegacyTemplate !== false,
missingTranslation: options.missingTranslation,
}); });
const normalizer = new DirectiveNormalizer( const normalizer = new DirectiveNormalizer(
{get: (url: string) => compilerHost.loadResource(url)}, urlResolver, htmlParser, config); {get: (url: string) => compilerHost.loadResource(url)}, urlResolver, htmlParser, config);

View File

@ -6,10 +6,13 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {MissingTranslationStrategy} from '@angular/core';
export interface AotCompilerOptions { export interface AotCompilerOptions {
locale?: string; locale?: string;
i18nFormat?: string; i18nFormat?: string;
translations?: string; translations?: string;
missingTranslation?: MissingTranslationStrategy;
enableLegacyTemplate?: boolean; enableLegacyTemplate?: boolean;
/** preamble for all generated source files */ /** preamble for all generated source files */
genFilePreamble?: string; genFilePreamble?: string;

View File

@ -31,12 +31,20 @@ export class NgcCliOptions extends CliOptions {
public i18nFormat: string; public i18nFormat: string;
public i18nFile: string; public i18nFile: string;
public locale: string; public locale: string;
public missingTranslation: string;
constructor({i18nFormat = null, i18nFile = null, locale = null, basePath = null}: constructor({i18nFormat = null, i18nFile = null, locale = null, missingTranslation = null,
{i18nFormat?: string, i18nFile?: string, locale?: string, basePath?: string}) { basePath = null}: {
i18nFormat?: string,
i18nFile?: string,
locale?: string,
missingTranslation?: string,
basePath?: string
}) {
super({basePath: basePath}); super({basePath: basePath});
this.i18nFormat = i18nFormat; this.i18nFormat = i18nFormat;
this.i18nFile = i18nFile; this.i18nFile = i18nFile;
this.locale = locale; this.locale = locale;
this.missingTranslation = missingTranslation;
} }
} }