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

expose ngc i18n options #3098

Merged
merged 8 commits into from Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 7 additions & 1 deletion packages/angular-cli/commands/build.ts
Expand Up @@ -11,6 +11,9 @@ export interface BuildOptions {
supressSizes: boolean;
baseHref?: string;
aot?: boolean;
i18nFile?: string;
i18nFormat?: string;
locale?: string;
}

const BuildCommand = Command.extend({
Expand All @@ -31,7 +34,10 @@ const BuildCommand = Command.extend({
{ name: 'watcher', type: String },
{ name: 'suppress-sizes', type: Boolean, default: false },
{ name: 'base-href', type: String, default: null, aliases: ['bh'] },
{ name: 'aot', type: Boolean, default: false }
{ name: 'aot', type: Boolean, default: false },
{ name: 'i18n-file', type: String, default: null },
{ name: 'i18n-format', type: String, default: null },
{ name: 'locale', type: String, default: null }
],

run: function (commandOptions: BuildOptions) {
Expand Down
6 changes: 6 additions & 0 deletions packages/angular-cli/commands/serve.ts
Expand Up @@ -27,6 +27,9 @@ export interface ServeTaskOptions {
sslCert?: string;
aot?: boolean;
open?: boolean;
i18nFile?: string;
i18nFormat?: string;
locale?: string;
}

const ServeCommand = Command.extend({
Expand Down Expand Up @@ -88,6 +91,9 @@ const ServeCommand = Command.extend({
aliases: ['o'],
description: 'Opens the url in default browser',
},
{ name: 'i18n-file', type: String, default: null },
{ name: 'i18n-format', type: String, default: null },
{ name: 'locale', type: String, default: null }
],

run: function(commandOptions: ServeTaskOptions) {
Expand Down
8 changes: 6 additions & 2 deletions packages/angular-cli/models/webpack-build-typescript.ts
Expand Up @@ -47,7 +47,8 @@ export const getWebpackNonAotConfigPartial = function(projectRoot: string, appCo
};
};

export const getWebpackAotConfigPartial = function(projectRoot: string, appConfig: any) {
export const getWebpackAotConfigPartial = function(projectRoot: string, appConfig: any,
i18nFile: string, i18nFormat: string, locale: string) {
return {
module: {
rules: [
Expand All @@ -61,7 +62,10 @@ export const getWebpackAotConfigPartial = function(projectRoot: string, appConfi
plugins: [
new AotPlugin({
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
mainPath: path.join(projectRoot, appConfig.root, appConfig.main)
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: i18nFile,
i18nFormat: i18nFormat,
locale: locale
}),
]
};
Expand Down
7 changes: 5 additions & 2 deletions packages/angular-cli/models/webpack-config.ts
Expand Up @@ -23,7 +23,10 @@ export class NgCliWebpackConfig {
public environment: string,
outputDir?: string,
baseHref?: string,
isAoT = false
isAoT = false,
i18nFile?: string,
i18nFormat?: string,
locale?: string
) {
const config: CliConfig = CliConfig.fromProject();
const appConfig = config.config.apps[0];
Expand All @@ -38,7 +41,7 @@ export class NgCliWebpackConfig {
);
let targetConfigPartial = this.getTargetConfig(this.ngCliProject.root, appConfig);
const typescriptConfigPartial = isAoT
? getWebpackAotConfigPartial(this.ngCliProject.root, appConfig)
? getWebpackAotConfigPartial(this.ngCliProject.root, appConfig, i18nFile, i18nFormat, locale)
: getWebpackNonAotConfigPartial(this.ngCliProject.root, appConfig);

if (appConfig.mobile) {
Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/tasks/build-webpack-watch.ts
Expand Up @@ -24,7 +24,10 @@ export default Task.extend({
runTaskOptions.environment,
outputDir,
runTaskOptions.baseHref,
runTaskOptions.aot
runTaskOptions.aot,
runTaskOptions.i18nFile,
runTaskOptions.i18nFormat,
runTaskOptions.locale
).config;
const webpackCompiler: any = webpack(config);

Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/tasks/build-webpack.ts
Expand Up @@ -23,7 +23,10 @@ export default <any>Task.extend({
runTaskOptions.environment,
outputDir,
runTaskOptions.baseHref,
runTaskOptions.aot
runTaskOptions.aot,
runTaskOptions.i18nFile,
runTaskOptions.i18nFormat,
runTaskOptions.locale
).config;

const webpackCompiler: any = webpack(config);
Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/tasks/serve-webpack.ts
Expand Up @@ -26,7 +26,10 @@ export default Task.extend({
commandOptions.environment,
undefined,
undefined,
commandOptions.aot
commandOptions.aot,
commandOptions.i18nFile,
commandOptions.i18nFormat,
commandOptions.locale
).config;

// This allows for live reload of page when changes are made to repo.
Expand Down
4 changes: 2 additions & 2 deletions packages/angular-cli/utilities/completion.sh
Expand Up @@ -8,13 +8,13 @@

ng_opts='b build completion doc e2e g generate get github-pages:deploy gh-pages:deploy h help i init install lint make-this-awesome new s serve server set t test v version -h --help'

build_opts='--aot --base-href --environment --output-path --suppress-sizes --target --watch --watcher -bh -dev -e -o -prod -t -w'
build_opts='--aot --base-href --environment --i18n-file --i18n-format --locale --output-path --suppress-sizes --target --watch --watcher -bh -dev -e -o -prod -t -w'
generate_opts='class component directive enum module pipe route service c cl d e m p r s --help'
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page -bh -e -t'
help_opts='--json --verbose -v'
init_opts='--dry-run inline-style inline-template --link-cli --mobile --name --prefix --routing --skip-bower --skip-npm --source-dir --style --verbose -d -is -it -lc -n -p -sb -sd -sn -v'
new_opts='--directory --dry-run inline-style inline-template --link-cli --mobile --prefix --routing --skip-bower --skip-git --skip-npm --source-dir --style --verbose -d -dir -is -it -lc -p -sb -sd -sg -sn -v'
serve_opts='--aot --environment --host --live-reload --live-reload-base-url --live-reload-host --live-reload-live-css --live-reload-port --open --port --proxy-config --ssl --ssl-cert --ssl-key --target --watcher -H -e -lr -lrbu -lrh -lrp -o -p -pc -t -w'
serve_opts='--aot --environment --host --i18n-file --i18n-format --live-reload --live-reload-base-url --live-reload-host --live-reload-live-css --live-reload-port --locale --open --port --proxy-config --ssl --ssl-cert --ssl-key --target --watcher -H -e -lr -lrbu -lrh -lrp -o -p -pc -t -w'
set_opts='--global -g'
test_opts='--browsers --build --code-coverage --colors --lint --log-level --port --reporters --watch -cc -l -w'

Expand Down
25 changes: 22 additions & 3 deletions packages/webpack/src/plugin.ts
Expand Up @@ -23,6 +23,9 @@ export interface AotPluginOptions {
entryModule?: string;
mainPath?: string;
typeChecking?: boolean;
i18nFile?: string;
i18nFormat?: string;
locale?: string;
}


Expand Down Expand Up @@ -72,6 +75,9 @@ export class AotPlugin {
private _basePath: string;
private _genDir: string;

private _i18nFile: string;
private _i18nFormat: string;
private _locale: string;

constructor(options: AotPluginOptions) {
this._setupOptions(options);
Expand All @@ -85,6 +91,9 @@ export class AotPlugin {
get genDir() { return this._genDir; }
get program() { return this._program; }
get typeCheck() { return this._typeCheck; }
get i18nFile() { return this._i18nFile; }
get i18nFormat() { return this._i18nFormat; }
get locale() { return this._locale; }

private _setupOptions(options: AotPluginOptions) {
// Fill in the missing options.
Expand Down Expand Up @@ -139,6 +148,16 @@ export class AotPlugin {
this._reflectorHost = new ngCompiler.ReflectorHost(
this._program, this._compilerHost, this._angularCompilerOptions);
this._reflector = new ngCompiler.StaticReflector(this._reflectorHost);

if (options.hasOwnProperty('i18nFile')) {
this._i18nFile = options.i18nFile;
}
if (options.hasOwnProperty('i18nFormat')) {
this._i18nFormat = options.i18nFormat;
}
if (options.hasOwnProperty('locale')) {
this._locale = options.locale;
}
}

// registration hook for webpack plugin
Expand Down Expand Up @@ -202,9 +221,9 @@ export class AotPlugin {
this._resourceLoader = new WebpackResourceLoader(compilation);

const i18nOptions: ngCompiler.NgcCliOptions = {
i18nFile: undefined,
i18nFormat: undefined,
locale: undefined,
i18nFile: this.i18nFile,
i18nFormat: this.i18nFormat,
locale: this.locale,
basePath: this.basePath
};

Expand Down
29 changes: 29 additions & 0 deletions tests/e2e/tests/build/aot-i18n.ts
@@ -0,0 +1,29 @@
import {ng} from '../../utils/process';
import {expectFileToMatch, writeFile, createDir, appendToFile} from '../../utils/fs';
import {expectToFail} from '../../utils/utils';

export default function() {
return Promise.resolve()
.then(() => createDir('src/locale'))
.then(() => writeFile('src/locale/messages.fr.xlf', `
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="8def8481e91291a52f9baa31cbdb313e6a6ca02b" datatype="html">
<source>Hello i18n!</source>
<target>Bonjour i18n!</target>
<note priority="1" from="description">An introduction header for this sample</note>
</trans-unit>
</body>
</file>
</xliff>`))
.then(() => appendToFile('src/app/app.component.html',
'<h1 i18n="An introduction header for this sample">Hello i18n!</h1>'))
.then(() => ng('build', '--aot', '--i18n-file', 'src/locale/messages.fr.xlf', '--i18n-format',
'xlf', '--locale', 'fr'))
.then(() => expectFileToMatch('dist/main.bundle.js', /Bonjour i18n!/))
.then(() => ng('build', '--aot'))
.then(() => expectToFail(() => expectFileToMatch('dist/main.bundle.js', /Bonjour i18n!/)))
.then(() => expectFileToMatch('dist/main.bundle.js', /Hello i18n!/));
}