Skip to content

Commit

Permalink
New i18n schema (#15760)
Browse files Browse the repository at this point in the history
* feat(@angular-devkit/core): update schema to support new i18n options

"projects": {
  "my-app": {
    "projectType": "application",
    "schematics": {},
    "root": "",
    "i18n": {
      "sourceLocale": "en-US",
      "locales": {
        "fr": "src/locale/messages.fr.xlf"
      }
    },
    "sourceRoot": "src",
    ...
  }
}

* feat(@angular-devkit/build-angular): add new i18n options to browser and server builders

With this change we add `translateLocales` as new options for i18n in browser and server builders.

We also deprecate the following options;
* i18nLocale
* i18nFormat
* i18nFile

* feat(@angular-devkit/build-angular): deprecate `i18nFormat` and `i18nLocale` options of `extract-i18n` builder

Option `i18nFormat` has been deprecated in favor of `format` and `i18nLocale` option has been deprecated in favor of the `sourceLocale` sub option of the `i18n` project level option.

* feat(@angular/cli): add alias of `i18n-extract` for `x18n` command

* refactor: rename `translateLocales` to `localize`
  • Loading branch information
alan-agius4 authored and mgechev committed Oct 10, 2019
1 parent 2dc8853 commit c3169ae
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 35 deletions.
6 changes: 6 additions & 0 deletions etc/api/angular_devkit/core/src/_golden-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ export declare class WorkspaceNotYetLoadedException extends BaseException {
export interface WorkspaceProject {
architect?: WorkspaceTool;
cli?: WorkspaceTool;
i18n?: WorkspaceProjectI18n;
prefix: string;
projectType: "application" | "library";
root: string;
Expand All @@ -1259,6 +1260,11 @@ export interface WorkspaceProject {
targets?: WorkspaceTool;
}

export interface WorkspaceProjectI18n {
locales: Record<string, string>;
sourceLocale?: string;
}

export interface WorkspaceSchema {
$schema?: string;
architect?: WorkspaceTool;
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/commands/xi18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Extracts i18n messages from source code.",
"$longDescription": "",

"$aliases": [],
"$aliases": ["i18n-extract"],
"$scope": "in",
"$type": "architect",
"$impl": "./xi18n-impl#Xi18nCommand",
Expand Down
101 changes: 88 additions & 13 deletions packages/angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@
"type": "string",
"description": "Root of the project files."
},
"i18n": {
"$ref": "#/definitions/project/definitions/i18n"
},
"sourceRoot": {
"type": "string",
"description": "The root of the source files, assets and index.html file structure."
Expand Down Expand Up @@ -398,6 +401,28 @@
"^[a-z]{1,3}-.*": {}
},
"definitions": {
"i18n": {
"description": "Project i18n options",
"type": "object",
"properties": {
"sourceLocale": {
"type": "string",
"description": "Specifies the source language of the application.",
"default": "en-US"
},
"locales": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[a-z]{2}(-[a-zA-Z]{2,})?$": {
"type": "string",
"description": "Localization file to use for i18n"
}
}
}
},
"additionalProperties": false
},
"target": {
"oneOf": [
{
Expand Down Expand Up @@ -756,21 +781,26 @@
"description": "Log progress to the console while building.",
"default": true
},
"localize": {
"$ref": "#/definitions/buildersOptions/localize"
},
"i18nMissingTranslation": {
"$ref": "#/definitions/buildersOptions/missingTranslation"
},
"i18nFile": {
"type": "string",
"description": "Localization file to use for i18n."
"description": "Localization file to use for i18n.",
"x-deprecated": "Deprecated since 9.0"
},
"i18nFormat": {
"type": "string",
"description": "Format of the localization file specified with --i18n-file."
"description": "Format of the localization file specified with --i18n-file.",
"x-deprecated": "Deprecated since 9.0"
},
"i18nLocale": {
"type": "string",
"description": "Locale to use for i18n."
},
"i18nMissingTranslation": {
"type": "string",
"description": "How to handle missing translations for i18n."
"description": "Locale to use for i18n.",
"x-deprecated": "Deprecated since 9.0"
},
"extractCss": {
"type": "boolean",
Expand Down Expand Up @@ -1281,10 +1311,24 @@
"type": "string",
"description": "Target to extract from."
},
"format": {
"type": "string",
"description": "Output format for the generated file.",
"default": "xlf",
"enum": [
"xmb",
"xlf",
"xlif",
"xliff",
"xlf2",
"xliff2"
]
},
"i18nFormat": {
"type": "string",
"description": "Output format for the generated file.",
"default": "xlf",
"x-deprecated": "Use 'format' option instead.",
"enum": [
"xmb",
"xlf",
Expand All @@ -1296,7 +1340,8 @@
},
"i18nLocale": {
"type": "string",
"description": "Specifies the source language of the application."
"description": "Specifies the source language of the application.",
"x-deprecated": "Use 'i18n' project level sub-option 'sourceLocale' instead."
},
"progress": {
"type": "boolean",
Expand Down Expand Up @@ -1761,21 +1806,26 @@
"description": "Log progress to the console while building.",
"default": true
},
"localize": {
"$ref": "#/definitions/buildersOptions/localize"
},
"i18nFile": {
"type": "string",
"description": "Localization file to use for i18n."
"description": "Localization file to use for i18n.",
"x-deprecated": "Deprecated since 9.0"
},
"i18nFormat": {
"type": "string",
"description": "Format of the localization file specified with --i18n-file."
"description": "Format of the localization file specified with --i18n-file.",
"x-deprecated": "Deprecated since 9.0"
},
"i18nLocale": {
"type": "string",
"description": "Locale to use for i18n."
"description": "Locale to use for i18n.",
"x-deprecated": "Deprecated since 9.0"
},
"i18nMissingTranslation": {
"type": "string",
"description": "How to handle missing translations for i18n."
"$ref": "#/definitions/buildersOptions/missingTranslation"
},
"outputHashing": {
"type": "string",
Expand Down Expand Up @@ -1965,6 +2015,31 @@
"additionalProperties": false
}
}
},
"buildersOptions": {
"missingTranslation": {
"type": "string",
"description": "How to handle missing translations for i18n.",
"enum": ["warning", "error", "ignore"],
"default": "warning"
},
"localize": {
"oneOf": [
{
"type": "boolean",
"description": "Translate all locales."
},
{
"type": "array",
"description": "List of locales ID's to translate.",
"minItems": 1,
"items": {
"type": "string",
"pattern": "^[a-z]{2}(-[a-zA-Z]{2,})?$"
}
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
AssetPatternClass,
Budget,
ExtraEntryPoint,
I18NMissingTranslation,
Localize,
OptimizationClass,
SourceMapClass,
} from '../../browser/schema';
Expand All @@ -27,26 +29,29 @@ export interface BuildOptions {
resourcesOutputPath?: string;
aot?: boolean;
sourceMap: SourceMapClass;
/** @deprecated use sourceMap instead */
/** @deprecated since version 8. use sourceMap instead. */
vendorSourceMap?: boolean;
/** @deprecated */
/** @deprecated since version 8 */
evalSourceMap?: boolean;
vendorChunk?: boolean;
commonChunk?: boolean;
baseHref?: string;
deployUrl?: string;
verbose?: boolean;
progress?: boolean;
/** @deprecated since version 9. Use 'locales' object in the project metadata instead.*/
i18nFile?: string;
/** @deprecated since version 9. No longer needed as the format will be determined automatically.*/
i18nFormat?: string;
/** @deprecated since version 9. Use 'localize' instead.*/
i18nLocale?: string;
i18nMissingTranslation?: string;
localize?: Localize;
i18nMissingTranslation?: I18NMissingTranslation;
extractCss?: boolean;
bundleDependencies?: 'none' | 'all';
watch?: boolean;
outputHashing?: string;
poll?: number;
app?: string;
deleteOutputPath?: boolean;
preserveSymlinks?: boolean;
extractLicenses?: boolean;
Expand All @@ -56,10 +61,12 @@ export interface BuildOptions {
subresourceIntegrity?: boolean;
serviceWorker?: boolean;
webWorkerTsConfig?: string;
/** @deprecated since version 8 **/
skipAppShell?: boolean;
statsJson: boolean;
forkTypeChecker: boolean;
profile?: boolean;
/** @deprecated since version 8 **/
es5BrowserSupport?: boolean;

main: string;
Expand Down
30 changes: 26 additions & 4 deletions packages/angular_devkit/build_angular/src/browser/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,41 @@
},
"i18nFile": {
"type": "string",
"description": "Localization file to use for i18n."
"description": "Localization file to use for i18n.",
"x-deprecated": "Use 'locales' object in the project metadata instead."
},
"i18nFormat": {
"type": "string",
"description": "Format of the localization file specified with --i18n-file."
"description": "Format of the localization file specified with --i18n-file.",
"x-deprecated": "No longer needed as the format will be determined automatically."
},
"i18nLocale": {
"type": "string",
"description": "Locale to use for i18n."
"description": "Locale to use for i18n.",
"x-deprecated": "Use 'localize' instead."
},
"i18nMissingTranslation": {
"type": "string",
"description": "How to handle missing translations for i18n."
"description": "How to handle missing translations for i18n.",
"enum": ["warning", "error", "ignore"],
"default": "warning"
},
"localize": {
"oneOf": [
{
"type": "boolean",
"description": "Translate all locales."
},
{
"type": "array",
"description": "List of locales ID's to translate.",
"minItems": 1,
"items": {
"type": "string",
"pattern": "^[a-z]{2}(-[a-zA-Z]{2,})?$"
}
}
]
},
"extractCss": {
"type": "boolean",
Expand Down
22 changes: 18 additions & 4 deletions packages/angular_devkit/build_angular/src/extract-i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import {
} from '../angular-cli-files/models/webpack-configs';
import { statsErrorsToString, statsWarningsToString } from '../angular-cli-files/utilities/stats';
import { Schema as BrowserBuilderOptions } from '../browser/schema';
import { createI18nOptions } from '../utils/i18n-options';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import { Schema as ExtractI18nBuilderOptions } from './schema';
import { Format, Schema as ExtractI18nBuilderOptions } from './schema';

function getI18nOutfile(format: string | undefined) {
switch (format) {
Expand Down Expand Up @@ -58,13 +59,26 @@ async function execute(options: ExtractI18nBuilderOptions, context: BuilderConte
await context.getBuilderNameForTarget(browserTarget),
);

if (options.i18nFormat !== Format.Xlf) {
options.format = options.i18nFormat;
}

// We need to determine the outFile name so that AngularCompiler can retrieve it.
let outFile = options.outFile || getI18nOutfile(options.i18nFormat);
let outFile = options.outFile || getI18nOutfile(options.format);
if (options.outputPath) {
// AngularCompilerPlugin doesn't support genDir so we have to adjust outFile instead.
outFile = path.join(options.outputPath, outFile);
}

const projectName = context.target && context.target.project;
if (!projectName) {
throw new Error('The builder requires a target.');
}
// target is verified in the above call
// tslint:disable-next-line: no-non-null-assertion
const metadata = await context.getProjectMetadata(context.target!);
const i18n = createI18nOptions(metadata);

const { config } = await generateBrowserWebpackConfigFromContext(
{
...browserOptions,
Expand All @@ -73,8 +87,8 @@ async function execute(options: ExtractI18nBuilderOptions, context: BuilderConte
styles: false,
},
buildOptimizer: false,
i18nLocale: options.i18nLocale,
i18nFormat: options.i18nFormat,
i18nLocale: options.i18nLocale || i18n.sourceLocale,
i18nFormat: options.format,
i18nFile: outFile,
aot: true,
progress: options.progress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@
"type": "string",
"description": "Target to extract from."
},
"format": {
"type": "string",
"description": "Output format for the generated file.",
"default": "xlf",
"enum": [
"xmb",
"xlf",
"xlif",
"xliff",
"xlf2",
"xliff2"
]
},
"i18nFormat": {
"type": "string",
"description": "Output format for the generated file.",
"default": "xlf",
"x-deprecated": "Use 'format' option instead.",
"enum": [
"xmb",
"xlf",
Expand All @@ -23,7 +37,8 @@
},
"i18nLocale": {
"type": "string",
"description": "Specifies the source language of the application."
"description": "Specifies the source language of the application.",
"x-deprecated": "Use 'i18n' project level sub-option 'sourceLocale' instead."
},
"progress": {
"type": "boolean",
Expand Down
Loading

0 comments on commit c3169ae

Please sign in to comment.