Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-cli): add i18n parameters to tsconfig #16365

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 23 additions & 11 deletions packages/compiler-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,22 @@ export class CodeGenerator {
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
new CompilerHost(program, options, context);
}
let transContent: string = '';
if (cliOptions.i18nFile) {
if (!cliOptions.locale) {

const transFile = cliOptions.i18nFile || options.i18nFile;
const locale = cliOptions.locale || options.translationLocale;
let translations: string = '';
if (transFile) {
if (!locale) {
throw new Error(
`The translation file (${cliOptions.i18nFile}) locale must be provided. Use the --locale option.`);
`The translation file (${transFile}) locale must be provided. Use the --locale option.`);
}
transContent = readFileSync(cliOptions.i18nFile, 'utf8');
translations = readFileSync(transFile, 'utf8');
}

const optMissingTranslation = cliOptions.missingTranslation || options.missingTranslation;
let missingTranslation = MissingTranslationStrategy.Warning;
if (cliOptions.missingTranslation) {
switch (cliOptions.missingTranslation) {
if (optMissingTranslation) {
switch (optMissingTranslation) {
case 'error':
missingTranslation = MissingTranslationStrategy.Error;
break;
Expand All @@ -83,13 +88,20 @@ export class CodeGenerator {
break;
default:
throw new Error(
`Unknown option for missingTranslation (${cliOptions.missingTranslation}). Use either error, warning or ignore.`);
`Unknown option for missingTranslation (${optMissingTranslation}). Use either error, warning or ignore.`);
}
}

let i18nFormat = cliOptions.i18nFormat || options.i18nTranslationFormat || options.i18nFormat;
if (i18nFormat && i18nFormat.toLowerCase() === 'xmb') {
i18nFormat = 'xtb';
}

const {compiler: aotCompiler} = compiler.createAotCompiler(ngCompilerHost, {
translations: transContent,
i18nFormat: cliOptions.i18nFormat,
locale: cliOptions.locale, missingTranslation,
translations,
i18nFormat,
locale,
missingTranslation,
enableLegacyTemplate: options.enableLegacyTemplate !== false,
genFilePreamble: PREAMBLE,
});
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler-cli/src/extract_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {Extractor} from './extractor';
function extract(
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
program: ts.Program, host: ts.CompilerHost): Promise<void> {
return Extractor.create(ngOptions, program, host, cliOptions.locale)
.extract(cliOptions.i18nFormat !, cliOptions.outFile);
const format = cliOptions.i18nFormat || ngOptions.i18nFormat || 'xlf';
const outFile = cliOptions.outFile || ngOptions.outFile || null;
return Extractor.create(ngOptions, program, host, cliOptions.locale).extract(format, outFile);
}

// Entry point
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler-cli/src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Extractor {
}

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

switch (format) {
Expand Down Expand Up @@ -98,7 +98,8 @@ export class Extractor {
new CompilerHost(program, options, context);
}

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

return new Extractor(options, ngExtractor, tsCompilerHost, ngCompilerHost, program);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-cli/src/ngtools_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ export class NgTools_InternalApi_NG_2 {
const extractor = Extractor.create(
options.angularCompilerOptions, options.program, options.host, locale, hostContext);

return extractor.extract(options.i18nFormat !, options.outFile || null);
return extractor.extract(options.i18nFormat || 'xlf', options.outFile || null);
}
}
21 changes: 21 additions & 0 deletions tools/@angular/tsc-wrapped/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ interface Options extends ts.CompilerOptions {

// Whether to enable support for <template> and the template attribute (true by default)
enableLegacyTemplate?: boolean;

// format for extracted messages file (xlf, xlf2, xmb)
i18nFormat?: string;

// locale of your application for extracted messages file
locale?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting multiple locales to appear here. Users shouldn't have N tsconfig files (and note that the tsconfig "extends" behavior doesn't cover unknown config properties like "angularCompilerOptions")

Copy link
Contributor Author

@ocombe ocombe May 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the locale used for extracted messages, which is the main locale of your application. There is only one here since you only do one extraction.
You are referring to translationLocale?: string; which is the locale used when you compile the application. I had a comment there that @vicb told me remove which was something like "only use this if you have one tsconfig file / locale".
I agree that it would be better if we could compile all locales in one command, but that is not possible yet. There's a bit refactor coming for the compiler and it will probably change at that point, but for now we can only compile for one locale.


// path & name for the extracted messages file
outFile?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably confusing.

"compilerOptions": {
  "outDir": "foo",
  "out": "bar"
},
"angularCompilerOptions": {
  "outFile": "baz"
}

I think either these options need to have i18n in the name, or be nested:

"angularCompilerOptions": {
  "i18n_msgs": {
    "extract": {
      "outFile": "msgs.xtb"
    },
    "merge": {
      "i18nFile": "msgs.xtb"
    }
  }
}

WDYT?

Copy link
Contributor Author

@ocombe ocombe May 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used the names of the current cli options (when possible). We can nest them though, what do you think @vicb ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not possible to nest the options:
type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't MapLike<string[]> the way to nest options? TypeScript does nest them, eg. compilerOptions.paths


// format for imported translation files (xlf, xlf2, xtb)
i18nTranslationFormat?: string;

// locale for imported translation files
translationLocale?: string;

// path & name of your translations file
i18nFile?: string;

// strategy to use for missing translations (error, warning or ignore)
missingTranslation?: string;
}

export default Options;