Skip to content

Commit ad9d73d

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
fix(@angular-devkit/build-angular): provide locale data discovery fallbacks
This synchronizes the behavior with the FW's wherein the language code will be used if the data for the full locale is not found. The user will still be notified in the event this occurs.
1 parent e414d9b commit ad9d73d

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

‎packages/angular_devkit/build_angular/src/utils/i18n-options.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,18 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
206206
continue;
207207
}
208208

209-
const localeDataPath = findLocaleDataPath(locale, localeDataBasePath);
209+
let localeDataPath = findLocaleDataPath(locale, localeDataBasePath);
210+
if (!localeDataPath) {
211+
const [first] = locale.split('-');
212+
if (first) {
213+
localeDataPath = findLocaleDataPath(first.toLowerCase(), localeDataBasePath);
214+
if (localeDataPath) {
215+
context.logger.warn(
216+
`Locale data for '${locale}' cannot be found. Using locale data for '${first}'.`,
217+
);
218+
}
219+
}
220+
}
210221
if (!localeDataPath) {
211222
context.logger.warn(
212223
`Locale data for '${locale}' cannot be found. No locale data will be included for this locale.`,
@@ -330,6 +341,11 @@ function findLocaleDataPath(locale: string, basePath: string): string | null {
330341
const localeDataPath = path.join(basePath, locale + '.js');
331342

332343
if (!fs.existsSync(localeDataPath)) {
344+
if (locale === 'en-US') {
345+
// fallback to known existing en-US locale data as of 9.0
346+
return findLocaleDataPath('en-US-POSIX', basePath);
347+
}
348+
333349
return null;
334350
}
335351

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { ng } from '../../utils/process';
9+
import { updateJsonFile } from '../../utils/project';
10+
import { setupI18nConfig } from './legacy';
11+
12+
export default async function() {
13+
// Setup i18n tests and config.
14+
await setupI18nConfig(true);
15+
16+
// Update angular.json
17+
await updateJsonFile('angular.json', workspaceJson => {
18+
const appProject = workspaceJson.projects['test-project'];
19+
// tslint:disable-next-line: no-any
20+
const i18n: Record<string, any> = appProject.i18n;
21+
22+
i18n.sourceLocale = 'fr-Abcd';
23+
appProject.architect['build'].options.localize = ['fr-Abcd'];
24+
});
25+
26+
const { stderr: err1 } = await ng('build');
27+
if (!err1.includes(`Locale data for 'fr-Abcd' cannot be found. Using locale data for 'fr'.`)) {
28+
throw new Error('locale data fallback warning not shown');
29+
}
30+
31+
// Update angular.json
32+
await updateJsonFile('angular.json', workspaceJson => {
33+
const appProject = workspaceJson.projects['test-project'];
34+
// tslint:disable-next-line: no-any
35+
const i18n: Record<string, any> = appProject.i18n;
36+
37+
i18n.sourceLocale = 'en-US';
38+
appProject.architect['build'].options.localize = ['en-US'];
39+
});
40+
41+
const { stderr: err2 } = await ng('build');
42+
if (err2.includes(`Locale data for 'en-US' cannot be found. No locale data will be included for this locale.`)) {
43+
throw new Error('locale data not found warning shown');
44+
}
45+
46+
}

0 commit comments

Comments
 (0)