Skip to content

Commit d55fc62

Browse files
alan-agius4filipesilva
authored andcommittedDec 6, 2021
fix(@angular-devkit/build-angular): fallback to use language ID to set the dir attribute
In some cases we don't ship certain locales, or they map to files which are named only the language IDs. Example `en-US`, which it's locale data is available from `@angular/common/locales/en.mjs` Closes #22285
1 parent b06dd03 commit d55fc62

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed
 

‎packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,40 @@ function isString(value: unknown): value is string {
222222
return typeof value === 'string';
223223
}
224224

225-
async function getLanguageDirection(lang: string, warnings: string[]): Promise<string | undefined> {
225+
async function getLanguageDirection(
226+
locale: string,
227+
warnings: string[],
228+
): Promise<string | undefined> {
229+
const dir = await getLanguageDirectionFromLocales(locale);
230+
231+
if (!dir) {
232+
warnings.push(
233+
`Locale data for '${locale}' cannot be found. 'dir' attribute will not be set for this locale.`,
234+
);
235+
}
236+
237+
return dir;
238+
}
239+
240+
async function getLanguageDirectionFromLocales(locale: string): Promise<string | undefined> {
226241
try {
227242
const localeData = (
228243
await loadEsmModule<typeof import('@angular/common/locales/en')>(
229-
`@angular/common/locales/${lang}`,
244+
`@angular/common/locales/${locale}`,
230245
)
231246
).default;
232247

233248
const dir = localeData[localeData.length - 2];
234249

235250
return isString(dir) ? dir : undefined;
236251
} catch {
237-
warnings.push(
238-
`Locale data for '${lang}' cannot be found. 'dir' attribute will not be set for this locale.`,
239-
);
252+
// In some cases certain locales might map to files which are named only with language id.
253+
// Example: `en-US` -> `en`.
254+
const [languageId] = locale.split('-', 1);
255+
if (languageId !== locale) {
256+
return getLanguageDirectionFromLocales(languageId);
257+
}
240258
}
259+
260+
return undefined;
241261
}

‎packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ describe('augment-index-html', () => {
104104
`);
105105
});
106106

107+
it(`should fallback to use language ID to set the dir attribute (en-US)`, async () => {
108+
const { content, warnings } = await augmentIndexHtml({
109+
...indexGeneratorOptions,
110+
lang: 'en-US',
111+
});
112+
113+
expect(warnings).toHaveSize(0);
114+
expect(content).toEqual(oneLineHtml`
115+
<html lang="en-US" dir="ltr">
116+
<head>
117+
<base href="/">
118+
</head>
119+
<body>
120+
</body>
121+
</html>
122+
`);
123+
});
124+
107125
it(`should work when lang (locale) is not provided by '@angular/common'`, async () => {
108126
const { content, warnings } = await augmentIndexHtml({
109127
...indexGeneratorOptions,

0 commit comments

Comments
 (0)
Please sign in to comment.