From a6c30e510626c922f6f8e81eb370a9481cdb5120 Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Wed, 1 Mar 2017 21:47:33 +0100 Subject: [PATCH] feat(@angular/cli): add new xi18n parameters --locale and --outFile Fixes #5145 --- packages/@angular/cli/commands/xi18n.ts | 16 +++++++++++++- .../cli/models/webpack-configs/xi18n.ts | 8 +++++-- .../cli/models/webpack-xi18n-config.ts | 6 ++++- packages/@angular/cli/tasks/extract-i18n.ts | 2 ++ .../webpack/src/extract_i18n_plugin.ts | 22 ++++++++++++++++++- tests/e2e/tests/i18n/extract-locale.ts | 21 ++++++++++++++++++ tests/e2e/tests/i18n/extract-outfile.ts | 21 ++++++++++++++++++ 7 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 tests/e2e/tests/i18n/extract-locale.ts create mode 100644 tests/e2e/tests/i18n/extract-outfile.ts diff --git a/packages/@angular/cli/commands/xi18n.ts b/packages/@angular/cli/commands/xi18n.ts index 5cce122fdbd4..afaea034de80 100644 --- a/packages/@angular/cli/commands/xi18n.ts +++ b/packages/@angular/cli/commands/xi18n.ts @@ -4,6 +4,8 @@ export interface Xi18nOptions { outputPath?: string; verbose?: boolean; i18nFormat?: string; + locale?: string; + outFile?: string; } const Xi18nCommand = Command.extend({ @@ -42,7 +44,19 @@ const Xi18nCommand = Command.extend({ type: String, aliases: ['a'], description: 'Specifies app name to use.' - } + }, + { + name: 'locale', + type: String, + aliases: ['l'], + description: 'Specifies the source language of the application.' + }, + { + name: 'out-file', + type: String, + aliases: ['of'], + description: 'Name of the file to output.' + }, ], run: function (commandOptions: any) { const {Extracti18nTask} = require('../tasks/extract-i18n'); diff --git a/packages/@angular/cli/models/webpack-configs/xi18n.ts b/packages/@angular/cli/models/webpack-configs/xi18n.ts index 8130fcbaeb2b..651e018d96c4 100644 --- a/packages/@angular/cli/models/webpack-configs/xi18n.ts +++ b/packages/@angular/cli/models/webpack-configs/xi18n.ts @@ -5,7 +5,9 @@ export const getWebpackExtractI18nConfig = function( projectRoot: string, appConfig: any, genDir: string, - i18nFormat: string): any { + i18nFormat: string, + locale: string, + outFile: string): any { let exclude: string[] = []; if (appConfig.test) { @@ -18,7 +20,9 @@ export const getWebpackExtractI18nConfig = function( tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig), exclude: exclude, genDir: genDir, - i18nFormat: i18nFormat + i18nFormat: i18nFormat, + locale: locale, + outFile: outFile, }) ] }; diff --git a/packages/@angular/cli/models/webpack-xi18n-config.ts b/packages/@angular/cli/models/webpack-xi18n-config.ts index 667cbcee178a..06e86ffc73a4 100644 --- a/packages/@angular/cli/models/webpack-xi18n-config.ts +++ b/packages/@angular/cli/models/webpack-xi18n-config.ts @@ -9,6 +9,8 @@ export interface XI18WebpackOptions { genDir?: string; buildDir?: string; i18nFormat?: string; + locale?: string; + outFile?: string; verbose?: boolean; progress?: boolean; app?: string; @@ -35,7 +37,9 @@ export class XI18nWebpackConfig extends NgCliWebpackConfig { getWebpackExtractI18nConfig(projectRoot, this.appConfig, this.extractOptions.genDir, - this.extractOptions.i18nFormat); + this.extractOptions.i18nFormat, + this.extractOptions.locale, + this.extractOptions.outFile); this.config = webpackMerge([this.config, extractI18nConfig]); return this.config; diff --git a/packages/@angular/cli/tasks/extract-i18n.ts b/packages/@angular/cli/tasks/extract-i18n.ts index fb8c52d477c5..278223162a35 100644 --- a/packages/@angular/cli/tasks/extract-i18n.ts +++ b/packages/@angular/cli/tasks/extract-i18n.ts @@ -21,6 +21,8 @@ export const Extracti18nTask = Task.extend({ genDir, buildDir, i18nFormat: runTaskOptions.i18nFormat, + locale: runTaskOptions.locale, + outFile: runTaskOptions.outFile, verbose: runTaskOptions.verbose, progress: runTaskOptions.progress, app: runTaskOptions.app, diff --git a/packages/@ngtools/webpack/src/extract_i18n_plugin.ts b/packages/@ngtools/webpack/src/extract_i18n_plugin.ts index 7b4d9ed0a778..10a087343abd 100644 --- a/packages/@ngtools/webpack/src/extract_i18n_plugin.ts +++ b/packages/@ngtools/webpack/src/extract_i18n_plugin.ts @@ -2,7 +2,7 @@ import * as ts from 'typescript'; import * as path from 'path'; import * as fs from 'fs'; -import {__NGTOOLS_PRIVATE_API_2} from '@angular/compiler-cli'; +import {__NGTOOLS_PRIVATE_API_2, VERSION} from '@angular/compiler-cli'; import {Tapable} from './webpack'; import {WebpackResourceLoader} from './resource_loader'; @@ -12,6 +12,8 @@ export interface ExtractI18nPluginOptions { basePath?: string; genDir?: string; i18nFormat?: string; + locale?: string; + outFile?: string; exclude?: string[]; } @@ -33,6 +35,8 @@ export class ExtractI18nPlugin implements Tapable { private _program: ts.Program; private _i18nFormat: string; + private _locale: string; + private _outFile: string; constructor(options: ExtractI18nPluginOptions) { this._setupOptions(options); @@ -117,6 +121,20 @@ export class ExtractI18nPlugin implements Tapable { if (options.hasOwnProperty('i18nFormat')) { this._i18nFormat = options.i18nFormat; } + if (options.hasOwnProperty('locale')) { + if (VERSION.major === '2') { + console.warn("The option '--locale' is only available on the xi18n command" + + ' starting from Angular v4, please update to a newer version.', '\n\n'); + } + this._locale = options.locale; + } + if (options.hasOwnProperty('outFile')) { + if (VERSION.major === '2') { + console.warn("The option '--out-file' is only available on the xi18n command" + + ' starting from Angular v4, please update to a newer version.', '\n\n'); + } + this._outFile = options.outFile; + } } apply(compiler: any) { @@ -156,6 +174,8 @@ export class ExtractI18nPlugin implements Tapable { host: this._compilerHost, angularCompilerOptions: this._angularCompilerOptions, i18nFormat: this._i18nFormat, + locale: this._locale, + outFile: this._outFile, readResource: (path: string) => this._resourceLoader.get(path) }); diff --git a/tests/e2e/tests/i18n/extract-locale.ts b/tests/e2e/tests/i18n/extract-locale.ts new file mode 100644 index 000000000000..dce9bda56930 --- /dev/null +++ b/tests/e2e/tests/i18n/extract-locale.ts @@ -0,0 +1,21 @@ +import { join } from 'path'; +import { ng } from '../../utils/process'; +import { + expectFileToExist, writeFile, + expectFileToMatch +} from '../../utils/fs'; + + +export default function() { + return ng('generate', 'component', 'i18n-test') + .then(() => writeFile( + join('src/app/i18n-test', 'i18n-test.component.html'), + '

Hello world

')) + .then(() => ng('xi18n', '--locale', 'fr')) + .then((output) => { + if (!output.stdout.match(/starting from Angular v4/)) { + expectFileToExist(join('src', 'messages.xlf')); + expectFileToMatch(join('src', 'messages.xlf'), /source-language="fr"/) + } + }); +} diff --git a/tests/e2e/tests/i18n/extract-outfile.ts b/tests/e2e/tests/i18n/extract-outfile.ts new file mode 100644 index 000000000000..444c0e1c70fc --- /dev/null +++ b/tests/e2e/tests/i18n/extract-outfile.ts @@ -0,0 +1,21 @@ +import {join} from 'path'; +import {ng} from '../../utils/process'; +import { + expectFileToExist, writeFile, + expectFileToMatch +} from '../../utils/fs'; + + +export default function() { + return ng('generate', 'component', 'i18n-test') + .then(() => writeFile( + join('src/app/i18n-test', 'i18n-test.component.html'), + '

Hello world

')) + .then(() => ng('xi18n', '--out-file', 'messages.fr.xlf')) + .then((output) => { + if (!output.stdout.match(/starting from Angular v4/)) { + expectFileToExist(join('src', 'messages.fr.xlf')); + expectFileToMatch(join('src', 'messages.fr.xlf'), /Hello world/); + } + }); +}