Skip to content

Commit 1371265

Browse files
authored
feat(module:i18n): enhance provideNzI18n to support factory function (#9393)
1 parent 86f5876 commit 1371265

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

‎components/i18n/nz-i18n.service.spec.ts‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
*/
55

66
import { Component, inject, OnDestroy } from '@angular/core';
7-
import { ComponentFixture, TestBed, inject as testingInject } from '@angular/core/testing';
7+
import { ComponentFixture, TestBed } from '@angular/core/testing';
88
import { Subscription } from 'rxjs';
99

1010
import { NZ_I18N, provideNzI18n } from 'ng-zorro-antd/i18n/nz-i18n.token';
1111

12-
import cs_CZ from './languages/cs_CZ';
13-
import de_DE from './languages/de_DE';
1412
import en_US from './languages/en_US';
15-
import ka_GE from './languages/ka_GE';
1613
import zh_CN from './languages/zh_CN';
1714
import { NzI18nInterface } from './nz-i18n.interface';
1815
import { NzI18nService } from './nz-i18n.service';
@@ -23,30 +20,15 @@ describe('i18n service', () => {
2320
let testComponent: NzI18nTestComponent;
2421
const DEFAULT_LAN = zh_CN;
2522

26-
beforeEach(() => {
27-
TestBed.configureTestingModule({
28-
providers: [provideNzI18n(DEFAULT_LAN)]
29-
});
30-
});
31-
3223
describe('#setLocale', () => {
3324
beforeEach(() => {
25+
TestBed.configureTestingModule({
26+
providers: [provideNzI18n(DEFAULT_LAN)]
27+
});
28+
3429
fixture = TestBed.createComponent(NzI18nTestComponent);
3530
testComponent = fixture.debugElement.componentInstance;
36-
});
37-
38-
beforeEach(
39-
testingInject([NzI18nService], (s: NzI18nService) => {
40-
srv = s;
41-
})
42-
);
43-
44-
it('should interface be right', () => {
45-
const i18nEN: NzI18nInterface = en_US;
46-
const i18nDE: NzI18nInterface = de_DE;
47-
const i18nCS: NzI18nInterface = cs_CZ;
48-
const i18nKA: NzI18nInterface = ka_GE;
49-
console.log(i18nEN, i18nDE, i18nCS, i18nKA);
31+
srv = TestBed.inject(NzI18nService);
5032
});
5133

5234
it('should be provide interface be right', () => {
@@ -85,6 +67,22 @@ https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/CONTRIBUTING.md`
8567
);
8668
});
8769
});
70+
71+
describe('provideNzI18n', () => {
72+
beforeEach(() => {
73+
TestBed.configureTestingModule({
74+
providers: [provideNzI18n(() => en_US)]
75+
});
76+
77+
fixture = TestBed.createComponent(NzI18nTestComponent);
78+
testComponent = fixture.debugElement.componentInstance;
79+
srv = TestBed.inject(NzI18nService);
80+
});
81+
82+
it('should factory work', () => {
83+
expect(testComponent.locale.locale).toBe(en_US.locale);
84+
});
85+
});
8886
});
8987

9088
@Component({

‎components/i18n/nz-i18n.token.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ import { DateLocale, NzI18nInterface } from './nz-i18n.interface';
99

1010
export const NZ_I18N = new InjectionToken<NzI18nInterface>('nz-i18n');
1111

12-
export function provideNzI18n(config: NzI18nInterface): EnvironmentProviders {
13-
return makeEnvironmentProviders([{ provide: NZ_I18N, useValue: config }]);
12+
type FactoryLike<T> = T | (() => T);
13+
14+
/**
15+
* Set the locale globally.
16+
*/
17+
export function provideNzI18n(config: NzI18nInterface): EnvironmentProviders;
18+
export function provideNzI18n(factory: () => NzI18nInterface): EnvironmentProviders;
19+
export function provideNzI18n(config: FactoryLike<NzI18nInterface>): EnvironmentProviders {
20+
return makeEnvironmentProviders([
21+
typeof config === 'function' ? { provide: NZ_I18N, useFactory: config } : { provide: NZ_I18N, useValue: config }
22+
]);
1423
}
1524

1625
/** Locale for date operations, should import from date-fns, see example: https://github.com/date-fns/date-fns/blob/v1.30.1/src/locale/zh_cn/index.js */

0 commit comments

Comments
 (0)