File tree 2 files changed +43
-5
lines changed
packages/angular_devkit/build_angular/src/utils/index-file
2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change @@ -222,20 +222,40 @@ function isString(value: unknown): value is string {
222
222
return typeof value === 'string' ;
223
223
}
224
224
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 > {
226
241
try {
227
242
const localeData = (
228
243
await loadEsmModule < typeof import ( '@angular/common/locales/en' ) > (
229
- `@angular/common/locales/${ lang } ` ,
244
+ `@angular/common/locales/${ locale } ` ,
230
245
)
231
246
) . default ;
232
247
233
248
const dir = localeData [ localeData . length - 2 ] ;
234
249
235
250
return isString ( dir ) ? dir : undefined ;
236
251
} 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
+ }
240
258
}
259
+
260
+ return undefined ;
241
261
}
Original file line number Diff line number Diff line change @@ -104,6 +104,24 @@ describe('augment-index-html', () => {
104
104
` ) ;
105
105
} ) ;
106
106
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
+
107
125
it ( `should work when lang (locale) is not provided by '@angular/common'` , async ( ) => {
108
126
const { content, warnings } = await augmentIndexHtml ( {
109
127
...indexGeneratorOptions ,
You can’t perform that action at this time.
0 commit comments