Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add provide function #1654

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions docs/i18n.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ registerLocaleData(zh);
`ng-zorro-antd` internationalization defaults to the Chinese version, for example the default English version:

```ts
import { NZ_I18N, en_US } from 'ng-zorro-antd/i18n';
import { en_US, provideNzI18n } from 'ng-zorro-antd/i18n';
@NgModule({
...
imports : [ NgZorroAntdModule ],
providers : [ { provide: NZ_I18N, useValue: en_US } ]
providers: [provideNzI18n(en_US)]
})
export class AppModule { }
export class App1Module {}
```

Of course, you can also use runtime changes:
Expand Down Expand Up @@ -144,7 +142,7 @@ In order to make language uniformity, NG-ALAIN provides a simple unified configu
```ts
// #region i18n
import { default as ngLang } from '@angular/common/locales/zh';
import { NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { provideNzI18n, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
const LANG = {
abbr: 'zh',
Expand All @@ -157,7 +155,7 @@ import { registerLocaleData } from '@angular/common';
registerLocaleData(LANG.ng, LANG.abbr);
const LANG_PROVIDES = [
{ provide: LOCALE_ID, useValue: LANG.abbr },
{ provide: NZ_I18N, useValue: LANG.zorro },
provideNzI18n(LANG.zorro),
{ provide: DELON_LOCALE, useValue: LANG.delon },
];
// #endregion
Expand All @@ -173,7 +171,7 @@ export class AppModule {}
```ts
// #region i18n
import { default as ngLang } from '@angular/common/locales/en';
import { NZ_I18N, en_US as zorroLang } from 'ng-zorro-antd/i18n';
import { provideNzI18n, en_US as zorroLang } from 'ng-zorro-antd/i18n';
import { DELON_LOCALE, en_US as delonLang } from '@delon/theme';
const LANG = {
abbr: 'en',
Expand All @@ -186,7 +184,7 @@ import { registerLocaleData } from '@angular/common';
registerLocaleData(LANG.ng, LANG.abbr);
const LANG_PROVIDES = [
{ provide: LOCALE_ID, useValue: LANG.abbr },
{ provide: NZ_I18N, useValue: LANG.zorro },
provideNzI18n(LANG.zorro),
{ provide: DELON_LOCALE, useValue: LANG.delon },
];
// #endregion
Expand Down
16 changes: 7 additions & 9 deletions docs/i18n.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ registerLocaleData(zh);
NG-ZORRO 国际化默认是中文版,例如默认为英文版本:

```ts
import { NZ_I18N, en_US } from 'ng-zorro-antd/i18n';
import { en_US, provideNzI18n } from 'ng-zorro-antd/i18n';
@NgModule({
...
imports : [ NgZorroAntdModule ],
providers : [ { provide: NZ_I18N, useValue: en_US } ]
providers: [provideNzI18n(en_US)]
})
export class AppModule { }
export class App1Module {}
```

当然,也可以使用运行时更改:
Expand Down Expand Up @@ -144,7 +142,7 @@ export class I18nTestComponent {
```ts
// #region i18n
import { default as ngLang } from '@angular/common/locales/zh';
import { NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { provideNzI18n, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
const LANG = {
abbr: 'zh',
Expand All @@ -157,7 +155,7 @@ import { registerLocaleData } from '@angular/common';
registerLocaleData(LANG.ng, LANG.abbr);
const LANG_PROVIDES = [
{ provide: LOCALE_ID, useValue: LANG.abbr },
{ provide: NZ_I18N, useValue: LANG.zorro },
provideNzI18n(LANG.zorro),
{ provide: DELON_LOCALE, useValue: LANG.delon },
];
// #endregion
Expand All @@ -173,7 +171,7 @@ export class AppModule {}
```ts
// #region i18n
import { default as ngLang } from '@angular/common/locales/en';
import { NZ_I18N, en_US as zorroLang } from 'ng-zorro-antd/i18n';
import { provideNzI18n, en_US as zorroLang } from 'ng-zorro-antd/i18n';
import { DELON_LOCALE, en_US as delonLang } from '@delon/theme';
const LANG = {
abbr: 'en',
Expand All @@ -186,7 +184,7 @@ import { registerLocaleData } from '@angular/common';
registerLocaleData(LANG.ng, LANG.abbr);
const LANG_PROVIDES = [
{ provide: LOCALE_ID, useValue: LANG.abbr },
{ provide: NZ_I18N, useValue: LANG.zorro },
provideNzI18n(LANG.zorro),
{ provide: DELON_LOCALE, useValue: LANG.delon },
];
// #endregion
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"extend": "^3.0.2",
"file-saver": "^2.0.5",
"ng-github-button": "^16.0.0",
"ng-zorro-antd": "^16.1.0",
"ng-zorro-antd": "^16.2.0",
"ngx-color": "~9.0.0",
"ngx-countdown": "^16.0.0",
"ngx-highlight-js": "^16.1.0",
Expand Down
4 changes: 2 additions & 2 deletions schematics/application/files/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Observable } from 'rxjs';
import { default as ngLang } from '@angular/common/locales/zh';
import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
import { zhCN as dateLang } from 'date-fns/locale';
import { NZ_DATE_LOCALE, NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
import { NZ_DATE_LOCALE, provideNzI18n, zh_CN as zorroLang } from 'ng-zorro-antd/i18n';
const LANG = {
abbr: 'zh',
ng: ngLang,
Expand All @@ -26,7 +26,7 @@ import { registerLocaleData } from '@angular/common';
registerLocaleData(LANG.ng, LANG.abbr);
const LANG_PROVIDES = [
{ provide: LOCALE_ID, useValue: LANG.abbr },
{ provide: NZ_I18N, useValue: LANG.zorro },
provideNzI18n(LANG.zorro),
{ provide: NZ_DATE_LOCALE, useValue: LANG.date },
{ provide: DELON_LOCALE, useValue: LANG.delon },
];
Expand Down
4 changes: 2 additions & 2 deletions schematics/plugin/plugin.default-language.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ describe('NgAlainSchematic: plugin: default-language', () => {
it(`should working`, () => {
const content = tree.readContent(PATH);
expect(content).toContain(`@angular/common/locales/en`);
expect(content).toContain(`NZ_I18N, en_US`);
expect(content).toContain(`provideNzI18n, en_US`);
expect(content).toContain(`DELON_LOCALE, en_US`);
});

it('should be from en to zh', async () => {
await runner.runSchematic('plugin', { name: 'defaultLanguage', type: 'add', defaultLanguage: 'zh' }, tree);
const content = tree.readContent(PATH);
expect(content).toContain(`@angular/common/locales/zh`);
expect(content).toContain(`NZ_I18N, zh_CN`);
expect(content).toContain(`provideNzI18n, zh_CN`);
expect(content).toContain(`DELON_LOCALE, zh_CN`);
});
});
1 change: 1 addition & 0 deletions schematics/plugin/plugin.default-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function pluginDefaultLanguage(options: PluginOptions): Rule {
.replace(/abbr: '([^']+)/, `abbr: '${options.defaultLanguage}`);
// zorro
content = content.replace(/NZ_I18N, ([^ ]+)/, `NZ_I18N, ${targetLang.zorro}`);
content = content.replace(/provideNzI18n, ([^ ]+)/, `provideNzI18n, ${targetLang.zorro}`);
// delon
content = content.replace(/DELON_LOCALE, ([^ ]+)/, `DELON_LOCALE, ${targetLang.zorro}`);
tree.overwrite(modulePath, content);
Expand Down
5 changes: 3 additions & 2 deletions src/app/core/code/files/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RouterModule } from '@angular/router';

import { DemoNgZorroAntdModule } from './ng-zorro-antd.module';
import { NZ_ICONS } from 'ng-zorro-antd/icon';
import { NZ_I18N, en_US } from 'ng-zorro-antd/i18n';
import { provideNzI18n, en_US } from 'ng-zorro-antd/i18n';
import { IconDefinition } from '@ant-design/icons-angular';
import * as AllIcons from '@ant-design/icons-angular/icons';

Expand Down Expand Up @@ -62,7 +62,8 @@ providers: [
deps: [StartupService],
multi: true
},
{ provide: NZ_I18N, useValue: en_US }, { provide: NZ_ICONS, useValue: icons }
provideNzI18n(en_US),
{ provide: NZ_ICONS, useValue: icons }
],
declarations: [ ${componentName} ],
bootstrap: [ ${componentName} ]
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/code/files/global-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ alainProvides.push(AlainConfigService as any);
// Please refer to: https://ng.ant.design/docs/global-config/en#how-to-use
// #region NG-ZORRO Config

import { NzConfig, NZ_CONFIG } from 'ng-zorro-antd/core/config';
import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config';

const ngZorroConfig: NzConfig = {};

const zorroProvides = [{ provide: NZ_CONFIG, useValue: ngZorroConfig }];
const zorroProvides = [provideNzConfig(ngZorroConfig)];

// #endregion

Expand Down
4 changes: 2 additions & 2 deletions src/app/global-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }];
// Please refer to: https://ng.ant.design/docs/global-config/en#how-to-use
// #region NG-ZORRO Config

import { NzConfig, NZ_CONFIG } from 'ng-zorro-antd/core/config';
import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config';

const ngZorroConfig: NzConfig = {};

const zorroProvides = [{ provide: NZ_CONFIG, useValue: ngZorroConfig }];
const zorroProvides = [provideNzConfig(ngZorroConfig)];

// #endregion

Expand Down