Skip to content

Commit 72d1bf3

Browse files
clydinvikerman
authored andcommitted
refactor(@angular-devkit/build-angular): support reading i18n project options
1 parent 1cefbc6 commit 72d1bf3

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
normalizeOptimization,
6565
normalizeSourceMaps,
6666
} from '../utils';
67+
import { I18nOptions, createI18nOptions } from '../utils/i18n-options';
6768
import { manglingDisabled } from '../utils/mangle-options';
6869
import {
6970
CacheKey,
@@ -167,13 +168,23 @@ async function initialize(
167168
context: BuilderContext,
168169
host: virtualFs.Host<fs.Stats>,
169170
webpackConfigurationTransform?: ExecutionTransformer<webpack.Configuration>,
170-
): Promise<{ config: webpack.Configuration[]; projectRoot: string; projectSourceRoot?: string }> {
171+
): Promise<{
172+
config: webpack.Configuration[];
173+
projectRoot: string;
174+
projectSourceRoot?: string;
175+
i18n: I18nOptions;
176+
}> {
171177
const { config, projectRoot, projectSourceRoot } = await buildBrowserWebpackConfigFromContext(
172178
options,
173179
context,
174180
host,
175181
);
176182

183+
// target is verified in the above call
184+
// tslint:disable-next-line: no-non-null-assertion
185+
const metadata = await context.getProjectMetadata(context.target!);
186+
const i18n = createI18nOptions(metadata);
187+
177188
let transformedConfig;
178189
if (webpackConfigurationTransform) {
179190
transformedConfig = [];
@@ -190,7 +201,7 @@ async function initialize(
190201
).toPromise();
191202
}
192203

193-
return { config: transformedConfig || config, projectRoot, projectSourceRoot };
204+
return { config: transformedConfig || config, projectRoot, projectSourceRoot, i18n };
194205
}
195206

196207
// tslint:disable-next-line: no-big-function
@@ -211,7 +222,7 @@ export function buildWebpackBrowser(
211222

212223
return from(initialize(options, context, host, transforms.webpackConfiguration)).pipe(
213224
// tslint:disable-next-line: no-big-function
214-
switchMap(({ config: configs, projectRoot }) => {
225+
switchMap(({ config: configs, projectRoot, i18n }) => {
215226
const tsConfig = readTsconfig(options.tsConfig, context.workspaceRoot);
216227
const target = tsConfig.options.target || ScriptTarget.ES5;
217228
const buildBrowserFeatures = new BuildBrowserFeatures(projectRoot, target);
@@ -591,7 +602,7 @@ export function buildWebpackBrowser(
591602
? workerFile
592603
: require.resolve('../utils/process-bundle-bootstrap'),
593604
'process',
594-
{ cachePath: cacheDownlevelPath },
605+
{ cachePath: cacheDownlevelPath, i18n },
595606
);
596607

597608
try {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { json } from '@angular-devkit/core';
9+
10+
export interface I18nOptions {
11+
sourceLocale?: string;
12+
locales: Record<string, { file: string; translation: unknown }>;
13+
}
14+
15+
export function createI18nOptions(metadata: json.JsonObject): I18nOptions {
16+
const i18n: I18nOptions = { locales: {} };
17+
18+
if (metadata.sourceLocale !== undefined && typeof metadata.sourceLocale !== 'string') {
19+
throw new Error('Project i18n sourceLocale field is malformed. Expected a string.');
20+
}
21+
22+
// en-US is the default locale added to Angular applications (https://angular.io/guide/i18n#i18n-pipes)
23+
i18n.sourceLocale = metadata.sourceLocale || 'en-US';
24+
25+
if (
26+
metadata.locales !== undefined &&
27+
(!metadata.locales || typeof metadata.locales !== 'object' || Array.isArray(metadata.locales))
28+
) {
29+
throw new Error('Project i18n locales field is malformed. Expected an object.');
30+
} else if (metadata.locales) {
31+
for (const [locale, translationFile] of Object.entries(metadata.locales)) {
32+
if (typeof translationFile !== 'string') {
33+
throw new Error(
34+
`Project i18n locales field value for '${locale}' is malformed. Expected a string.`,
35+
);
36+
}
37+
38+
// TODO: Integrate translation file parsing from FW when available
39+
i18n.locales[locale] = {
40+
file: translationFile,
41+
translation: {},
42+
};
43+
}
44+
}
45+
46+
return i18n;
47+
}

0 commit comments

Comments
 (0)