Skip to content

Commit 6e2abcd

Browse files
ocombemhevery
authored andcommitted
feat(compiler-cli): add param to set MissingTranslationStrategy on ngc (angular#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 angular#15808 PR Close angular#15987
1 parent 3f46645 commit 6e2abcd

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

packages/compiler-cli/src/codegen.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* Intended to be used in a build step.
1212
*/
1313
import * as compiler from '@angular/compiler';
14+
import {MissingTranslationStrategy} from '@angular/core';
1415
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
1516
import {readFileSync} from 'fs';
16-
import * as path from 'path';
1717
import * as ts from 'typescript';
1818

1919
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
@@ -61,20 +61,35 @@ export class CodeGenerator {
6161
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
6262
new CompilerHost(program, options, context);
6363
}
64-
const transFile = cliOptions.i18nFile;
65-
const locale = cliOptions.locale;
6664
let transContent: string = '';
67-
if (transFile) {
68-
if (!locale) {
65+
if (cliOptions.i18nFile) {
66+
if (!cliOptions.locale) {
6967
throw new Error(
70-
`The translation file (${transFile}) locale must be provided. Use the --locale option.`);
68+
`The translation file (${cliOptions.i18nFile}) locale must be provided. Use the --locale option.`);
69+
}
70+
transContent = readFileSync(cliOptions.i18nFile, 'utf8');
71+
}
72+
let missingTranslation = MissingTranslationStrategy.Warning;
73+
if (cliOptions.missingTranslation) {
74+
switch (cliOptions.missingTranslation) {
75+
case 'error':
76+
missingTranslation = MissingTranslationStrategy.Error;
77+
break;
78+
case 'warning':
79+
missingTranslation = MissingTranslationStrategy.Warning;
80+
break;
81+
case 'ignore':
82+
missingTranslation = MissingTranslationStrategy.Ignore;
83+
break;
84+
default:
85+
throw new Error(
86+
`Unknown option for missingTranslation (${cliOptions.missingTranslation}). Use either error, warning or ignore.`);
7187
}
72-
transContent = readFileSync(transFile, 'utf8');
7388
}
7489
const {compiler: aotCompiler} = compiler.createAotCompiler(ngCompilerHost, {
7590
translations: transContent,
7691
i18nFormat: cliOptions.i18nFormat,
77-
locale: cliOptions.locale,
92+
locale: cliOptions.locale, missingTranslation,
7893
enableLegacyTemplate: options.enableLegacyTemplate !== false,
7994
genFilePreamble: PREAMBLE,
8095
});

packages/compiler-cli/src/ngtools_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface NgTools_InternalApi_NG2_CodeGen_Options {
3535
i18nFormat?: string;
3636
i18nFile?: string;
3737
locale?: string;
38+
missingTranslation?: string;
3839

3940
readResource: (fileName: string) => Promise<string>;
4041

@@ -95,6 +96,7 @@ export class NgTools_InternalApi_NG_2 {
9596
i18nFormat: options.i18nFormat !,
9697
i18nFile: options.i18nFile !,
9798
locale: options.locale !,
99+
missingTranslation: options.missingTranslation !,
98100
basePath: options.basePath
99101
};
100102

packages/compiler/src/aot/compiler_factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCom
5151
StaticAndDynamicReflectionCapabilities.install(staticReflector);
5252
const console = new Console();
5353
const htmlParser = new I18NHtmlParser(
54-
new HtmlParser(), translations, options.i18nFormat, MissingTranslationStrategy.Warning,
55-
console);
54+
new HtmlParser(), translations, options.i18nFormat, options.missingTranslation, console);
5655
const config = new CompilerConfig({
5756
defaultEncapsulation: ViewEncapsulation.Emulated,
5857
useJit: false,
5958
enableLegacyTemplate: options.enableLegacyTemplate !== false,
59+
missingTranslation: options.missingTranslation,
6060
});
6161
const normalizer = new DirectiveNormalizer(
6262
{get: (url: string) => compilerHost.loadResource(url)}, urlResolver, htmlParser, config);

packages/compiler/src/aot/compiler_options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {MissingTranslationStrategy} from '@angular/core';
10+
911
export interface AotCompilerOptions {
1012
locale?: string;
1113
i18nFormat?: string;
1214
translations?: string;
15+
missingTranslation?: MissingTranslationStrategy;
1316
enableLegacyTemplate?: boolean;
1417
/** preamble for all generated source files */
1518
genFilePreamble?: string;

tools/@angular/tsc-wrapped/src/cli_options.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ export class NgcCliOptions extends CliOptions {
3131
public i18nFormat: string;
3232
public i18nFile: string;
3333
public locale: string;
34+
public missingTranslation: string;
3435

35-
constructor({i18nFormat = null, i18nFile = null, locale = null, basePath = null}:
36-
{i18nFormat?: string, i18nFile?: string, locale?: string, basePath?: string}) {
36+
constructor({i18nFormat = null, i18nFile = null, locale = null, missingTranslation = null,
37+
basePath = null}: {
38+
i18nFormat?: string,
39+
i18nFile?: string,
40+
locale?: string,
41+
missingTranslation?: string,
42+
basePath?: string
43+
}) {
3744
super({basePath: basePath});
3845
this.i18nFormat = i18nFormat;
3946
this.i18nFile = i18nFile;
4047
this.locale = locale;
48+
this.missingTranslation = missingTranslation;
4149
}
4250
}

0 commit comments

Comments
 (0)