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:

committed by
Miško Hevery

parent
3f46645f5f
commit
6e2abcd5fc
@ -11,9 +11,9 @@
|
||||
* Intended to be used in a build step.
|
||||
*/
|
||||
import * as compiler from '@angular/compiler';
|
||||
import {MissingTranslationStrategy} from '@angular/core';
|
||||
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
|
||||
import {readFileSync} from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
|
||||
@ -61,20 +61,35 @@ export class CodeGenerator {
|
||||
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
|
||||
new CompilerHost(program, options, context);
|
||||
}
|
||||
const transFile = cliOptions.i18nFile;
|
||||
const locale = cliOptions.locale;
|
||||
let transContent: string = '';
|
||||
if (transFile) {
|
||||
if (!locale) {
|
||||
if (cliOptions.i18nFile) {
|
||||
if (!cliOptions.locale) {
|
||||
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, {
|
||||
translations: transContent,
|
||||
i18nFormat: cliOptions.i18nFormat,
|
||||
locale: cliOptions.locale,
|
||||
locale: cliOptions.locale, missingTranslation,
|
||||
enableLegacyTemplate: options.enableLegacyTemplate !== false,
|
||||
genFilePreamble: PREAMBLE,
|
||||
});
|
||||
|
@ -35,6 +35,7 @@ export interface NgTools_InternalApi_NG2_CodeGen_Options {
|
||||
i18nFormat?: string;
|
||||
i18nFile?: string;
|
||||
locale?: string;
|
||||
missingTranslation?: string;
|
||||
|
||||
readResource: (fileName: string) => Promise<string>;
|
||||
|
||||
@ -95,6 +96,7 @@ export class NgTools_InternalApi_NG_2 {
|
||||
i18nFormat: options.i18nFormat !,
|
||||
i18nFile: options.i18nFile !,
|
||||
locale: options.locale !,
|
||||
missingTranslation: options.missingTranslation !,
|
||||
basePath: options.basePath
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user