@@ -9,14 +9,18 @@ import {
99 getDecoratorMetadata ,
1010 getProjectFromWorkspace ,
1111 getProjectMainFile ,
12+ getAppModulePath ,
1213 insertAfterLastOccurrence ,
1314 insertImport ,
15+ isStandaloneApp ,
1416 parseSourceFile
1517} from '@angular/cdk/schematics' ;
1618
17- import { Rule , Tree } from '@angular-devkit/schematics' ;
19+ import { Rule , Tree , chain } from '@angular-devkit/schematics' ;
20+ import { addRootProvider } from '@schematics/angular/utility' ;
1821import { 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' ;
2024import { getWorkspace } from '@schematics/angular/utility/workspace' ;
2125import { blue , cyan , yellow } from 'chalk' ;
2226import * as ts from 'typescript' ;
@@ -27,15 +31,24 @@ export function registerLocale(options: Schema): Rule {
2731 return async ( host : Tree ) => {
2832 const workspace = await getWorkspace ( host ) ;
2933 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 ) ;
3146 const moduleSource = parseSourceFile ( host , appModulePath ) ;
3247
3348 const locale = options . locale || 'en_US' ;
3449 const localePrefix = locale . split ( '_' ) [ 0 ] ;
3550
36- const recorder = host . beginUpdate ( appModulePath ) ;
37-
38- const changes = [
51+ applyChangesToFile ( host , appModulePath , [
3952 insertImport ( moduleSource , appModulePath , 'NZ_I18N' ,
4053 'ng-zorro-antd/i18n' ) ,
4154 insertImport ( moduleSource , appModulePath , locale ,
@@ -46,18 +59,32 @@ export function registerLocale(options: Schema): Rule {
4659 `@angular/common/locales/${ localePrefix } ` , true ) ,
4760 registerLocaleData ( moduleSource , appModulePath , localePrefix ) ,
4861 ...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+ }
5865
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+ ] ) ;
6188}
6289
6390function registerLocaleData ( moduleSource : ts . SourceFile , modulePath : string , locale : string ) : Change {
@@ -74,9 +101,9 @@ function registerLocaleData(moduleSource: ts.SourceFile, modulePath: string, loc
74101 modulePath , 0 ) as InsertChange ;
75102 } else {
76103 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 ) } ).` +
78105 `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:` ) ) ;
80107 console . log ( cyan ( `registerLocaleData(${ locale } );` ) ) ;
81108 return new NoopChange ( ) ;
82109 }
@@ -127,7 +154,7 @@ function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string,
127154 return addProvide ;
128155 } else {
129156 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 ) } ).` +
131158 `because there is already a locale token in provides.` ) ) ;
132159 console . log ( yellow ( `Please manually add the following code to your provides:` ) ) ;
133160 console . log ( cyan ( `{ provide: NZ_I18N, useValue: ${ locale } }` ) ) ;
@@ -139,3 +166,15 @@ function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string,
139166 }
140167
141168}
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