6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { BuilderContext } from '@angular-devkit/architect' ;
9
- import { json , virtualFs } from '@angular-devkit/core' ;
9
+ import { json } from '@angular-devkit/core' ;
10
10
import * as fs from 'fs' ;
11
11
import * as os from 'os' ;
12
12
import * as path from 'path' ;
@@ -19,7 +19,10 @@ import { createTranslationLoader } from './load-translations';
19
19
export interface I18nOptions {
20
20
inlineLocales : Set < string > ;
21
21
sourceLocale : string ;
22
- locales : Record < string , { file : string ; format ?: string ; translation ?: unknown } > ;
22
+ locales : Record <
23
+ string ,
24
+ { file : string ; format ?: string ; translation ?: unknown ; localeDataPath ?: string }
25
+ > ;
23
26
flatOutput ?: boolean ;
24
27
readonly shouldInline : boolean ;
25
28
}
@@ -127,6 +130,13 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
127
130
}
128
131
129
132
if ( i18n . inlineLocales . size > 0 ) {
133
+ const localeDataBasePath = findLocaleBaseDataPath ( ) ;
134
+ if ( ! localeDataBasePath ) {
135
+ throw new Error (
136
+ `Unable to find locale data within '@angular/common'. Please ensure '@angular/common' is installed.` ,
137
+ ) ;
138
+ }
139
+
130
140
// Load locales
131
141
const loader = await createTranslationLoader ( ) ;
132
142
const projectRoot = path . join ( context . workspaceRoot , ( metadata . root as string ) || '' ) ;
@@ -145,6 +155,15 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
145
155
146
156
desc . format = result . format ;
147
157
desc . translation = result . translation ;
158
+
159
+ const localeDataPath = findLocaleDataPath ( locale , localeDataBasePath ) ;
160
+ if ( ! localeDataPath ) {
161
+ context . logger . warn (
162
+ `Locale data for '${ locale } ' cannot be found. No locale data will be included for this locale.` ,
163
+ ) ;
164
+ } else {
165
+ desc . localeDataPath = localeDataPath ;
166
+ }
148
167
}
149
168
}
150
169
@@ -197,3 +216,28 @@ function mergeDeprecatedI18nOptions(
197
216
198
217
return i18n ;
199
218
}
219
+
220
+ function findLocaleBaseDataPath ( ) : string | null {
221
+ try {
222
+ const commonPath = path . dirname ( require . resolve ( '@angular/common/package.json' ) ) ;
223
+ const localesPath = path . join ( commonPath , 'locales/global' ) ;
224
+
225
+ if ( ! fs . existsSync ( localesPath ) ) {
226
+ return null ;
227
+ }
228
+
229
+ return localesPath ;
230
+ } catch {
231
+ return null ;
232
+ }
233
+ }
234
+
235
+ function findLocaleDataPath ( locale : string , basePath : string ) : string | null {
236
+ const localeDataPath = path . join ( basePath , locale ) ;
237
+
238
+ if ( ! fs . existsSync ( localeDataPath ) ) {
239
+ return null ;
240
+ }
241
+
242
+ return localeDataPath ;
243
+ }
0 commit comments