@@ -9,14 +9,18 @@ import {
9
9
getDecoratorMetadata ,
10
10
getProjectFromWorkspace ,
11
11
getProjectMainFile ,
12
+ getAppModulePath ,
12
13
insertAfterLastOccurrence ,
13
14
insertImport ,
15
+ isStandaloneApp ,
14
16
parseSourceFile
15
17
} from '@angular/cdk/schematics' ;
16
18
17
- import { Rule , Tree } from '@angular-devkit/schematics' ;
19
+ import { Rule , Tree , chain } from '@angular-devkit/schematics' ;
20
+ import { addRootProvider } from '@schematics/angular/utility' ;
18
21
import { Change , InsertChange , NoopChange } from '@schematics/angular/utility/change' ;
19
- import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils' ;
22
+ import { findAppConfig } from '@schematics/angular/utility/standalone/app_config' ;
23
+ import { findBootstrapApplicationCall } from '@schematics/angular/utility/standalone/util' ;
20
24
import { getWorkspace } from '@schematics/angular/utility/workspace' ;
21
25
import { blue , cyan , yellow } from 'chalk' ;
22
26
import * as ts from 'typescript' ;
@@ -27,15 +31,24 @@ export function registerLocale(options: Schema): Rule {
27
31
return async ( host : Tree ) => {
28
32
const workspace = await getWorkspace ( host ) ;
29
33
const project = getProjectFromWorkspace ( workspace , options . project ) ;
30
- const appModulePath = getAppModulePath ( host , getProjectMainFile ( project ) ) ;
34
+ const mainFile = getProjectMainFile ( project ) ;
35
+ if ( isStandaloneApp ( host , mainFile ) ) {
36
+ return registerLocaleInStandaloneApp ( mainFile , options ) ;
37
+ } else {
38
+ return registerLocaleInAppModule ( mainFile , options ) ;
39
+ }
40
+ } ;
41
+ }
42
+
43
+ function registerLocaleInAppModule ( mainFile : string , options : Schema ) : Rule {
44
+ return async ( host : Tree ) => {
45
+ const appModulePath = getAppModulePath ( host , mainFile ) ;
31
46
const moduleSource = parseSourceFile ( host , appModulePath ) ;
32
47
33
48
const locale = options . locale || 'en_US' ;
34
49
const localePrefix = locale . split ( '_' ) [ 0 ] ;
35
50
36
- const recorder = host . beginUpdate ( appModulePath ) ;
37
-
38
- const changes = [
51
+ applyChangesToFile ( host , appModulePath , [
39
52
insertImport ( moduleSource , appModulePath , 'NZ_I18N' ,
40
53
'ng-zorro-antd/i18n' ) ,
41
54
insertImport ( moduleSource , appModulePath , locale ,
@@ -46,18 +59,32 @@ export function registerLocale(options: Schema): Rule {
46
59
`@angular/common/locales/${ localePrefix } ` , true ) ,
47
60
registerLocaleData ( moduleSource , appModulePath , localePrefix ) ,
48
61
...insertI18nTokenProvide ( moduleSource , appModulePath , locale )
49
- ] ;
50
-
51
- changes . forEach ( ( change ) => {
52
- if ( change instanceof InsertChange ) {
53
- recorder . insertLeft ( change . pos , change . toAdd ) ;
54
- }
55
- } ) ;
56
-
57
- host . commitUpdate ( recorder ) ;
62
+ ] ) ;
63
+ }
64
+ }
58
65
59
- return ;
60
- } ;
66
+ function registerLocaleInStandaloneApp ( mainFile : string , options : Schema ) : Rule {
67
+ const locale = options . locale || 'en_US' ;
68
+
69
+ return chain ( [
70
+ async ( host : Tree ) => {
71
+ const bootstrapCall = findBootstrapApplicationCall ( host , mainFile ) ;
72
+ const appConfig = findAppConfig ( bootstrapCall , host , mainFile ) ;
73
+ const appConfigFile = appConfig . filePath ;
74
+ const appConfigSource = parseSourceFile ( host , appConfig . filePath ) ;
75
+ const localePrefix = locale . split ( '_' ) [ 0 ] ;
76
+
77
+ applyChangesToFile ( host , appConfigFile , [
78
+ insertImport ( appConfigSource , appConfigFile , locale , 'ng-zorro-antd/i18n' ) ,
79
+ insertImport ( appConfigSource , appConfigFile , 'registerLocaleData' , '@angular/common' ) ,
80
+ insertImport ( appConfigSource , appConfigFile , localePrefix , `@angular/common/locales/${ localePrefix } ` , true ) ,
81
+ registerLocaleData ( appConfigSource , appConfigFile , localePrefix )
82
+ ] ) ;
83
+ } ,
84
+ addRootProvider ( options . project , ( { code, external} ) => {
85
+ return code `${ external ( 'provideNzI18n' , 'ng-zorro-antd/i18n' ) } (${ locale } )` ;
86
+ } )
87
+ ] ) ;
61
88
}
62
89
63
90
function registerLocaleData ( moduleSource : ts . SourceFile , modulePath : string , locale : string ) : Change {
@@ -74,9 +101,9 @@ function registerLocaleData(moduleSource: ts.SourceFile, modulePath: string, loc
74
101
modulePath , 0 ) as InsertChange ;
75
102
} else {
76
103
console . log ( ) ;
77
- console . log ( yellow ( `Could not add the registerLocaleData to your app.module file (${ blue ( modulePath ) } ).` +
104
+ console . log ( yellow ( `Could not add the registerLocaleData to file (${ blue ( modulePath ) } ).` +
78
105
`because there is already a registerLocaleData function.` ) ) ;
79
- console . log ( yellow ( `Please manually add the following code to your app.module :` ) ) ;
106
+ console . log ( yellow ( `Please manually add the following code:` ) ) ;
80
107
console . log ( cyan ( `registerLocaleData(${ locale } );` ) ) ;
81
108
return new NoopChange ( ) ;
82
109
}
@@ -127,7 +154,7 @@ function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string,
127
154
return addProvide ;
128
155
} else {
129
156
console . log ( ) ;
130
- console . log ( yellow ( `Could not provide the locale token to your app.module file (${ blue ( modulePath ) } ).` +
157
+ console . log ( yellow ( `Could not provide the locale token to file (${ blue ( modulePath ) } ).` +
131
158
`because there is already a locale token in provides.` ) ) ;
132
159
console . log ( yellow ( `Please manually add the following code to your provides:` ) ) ;
133
160
console . log ( cyan ( `{ provide: NZ_I18N, useValue: ${ locale } }` ) ) ;
@@ -139,3 +166,15 @@ function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string,
139
166
}
140
167
141
168
}
169
+
170
+ function applyChangesToFile ( host : Tree , filePath : string , changes : Change [ ] ) : void {
171
+ const recorder = host . beginUpdate ( filePath ) ;
172
+
173
+ changes . forEach ( ( change ) => {
174
+ if ( change instanceof InsertChange ) {
175
+ recorder . insertLeft ( change . pos , change . toAdd ) ;
176
+ }
177
+ } ) ;
178
+
179
+ host . commitUpdate ( recorder ) ;
180
+ }
0 commit comments