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(@angular/cli): add new xi18n parameters --locale and --outFile #5154

Merged
merged 1 commit into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/@angular/cli/commands/xi18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface Xi18nOptions {
outputPath?: string;
verbose?: boolean;
i18nFormat?: string;
locale?: string;
outFile?: string;
}

const Xi18nCommand = Command.extend({
Expand Down Expand Up @@ -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');
Expand Down
8 changes: 6 additions & 2 deletions packages/@angular/cli/models/webpack-configs/xi18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
})
]
};
Expand Down
6 changes: 5 additions & 1 deletion packages/@angular/cli/models/webpack-xi18n-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface XI18WebpackOptions {
genDir?: string;
buildDir?: string;
i18nFormat?: string;
locale?: string;
outFile?: string;
verbose?: boolean;
progress?: boolean;
app?: string;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/tasks/extract-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion packages/@ngtools/webpack/src/extract_i18n_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -12,6 +12,8 @@ export interface ExtractI18nPluginOptions {
basePath?: string;
genDir?: string;
i18nFormat?: string;
locale?: string;
outFile?: string;
exclude?: string[];
}

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
});
Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/tests/i18n/extract-locale.ts
Original file line number Diff line number Diff line change
@@ -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'),
'<p i18n>Hello world</p>'))
.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"/)
}
});
}
21 changes: 21 additions & 0 deletions tests/e2e/tests/i18n/extract-outfile.ts
Original file line number Diff line number Diff line change
@@ -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'),
'<p i18n>Hello world</p>'))
.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/);
}
});
}