Skip to content

Commit c98df53

Browse files
authored
fix: update the LocaleService initial logic (#397)
* fix: get the default language from localStorage * test: unit test for reset method * refactor: simplify the code
1 parent 55efbb7 commit c98df53

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

src/i18n/__tests__/localeService.test.ts

+33-14
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,77 @@ import { LocaleService } from '..';
44
import { BuiltInLocales, BuiltInZhCN, ILocale } from '../localization';
55

66
describe('The Locale Service', () => {
7-
const localeService = container.resolve(LocaleService);
8-
97
const TestLocale = {
108
id: 'test',
119
source: new Map(),
1210
name: 'test',
1311
};
1412

1513
afterEach(() => {
14+
localStorage.clear();
15+
});
16+
17+
test('Instance the LocaleService by IOC', () => {
18+
const localeService = container.resolve(LocaleService);
19+
expect(localeService).not.toBeUndefined();
20+
});
21+
22+
test('Reset the LocaleService', () => {
23+
const localeService = new LocaleService();
24+
expect(localeService.getCurrentLocale()!.id).toBe(BuiltInZhCN.id);
1625
localeService.reset();
26+
expect(localeService.getCurrentLocale()).toBeUndefined();
1727
});
1828

1929
test('Get default Locale', () => {
30+
const localeService = new LocaleService();
2031
const defaultLocale = localeService.getDefaultLocale();
2132
expect(defaultLocale).toEqual(BuiltInZhCN);
2233
});
2334

2435
test('Get default Locales', () => {
36+
const localeService = new LocaleService();
2537
const defaultLocale = localeService.getDefaultLocales();
2638
expect(defaultLocale).toEqual(BuiltInLocales);
2739
});
2840

2941
test('The size of Built-in Locales should be 2', () => {
42+
const localeService = new LocaleService();
3043
const locales = localeService.getLocales();
3144
expect(locales.length).toBe(2);
3245
});
3346

3447
test('Initialize the locales', () => {
48+
const localeService = new LocaleService();
3549
localeService.initialize([TestLocale]);
36-
expect(localeService.getCurrentLocale().id).toEqual(
50+
expect(localeService.getCurrentLocale()!.id).toEqual(
3751
localeService.getDefaultLocale().id
3852
);
3953
expect(localeService.getLocales().length).toBe(3);
4054
localeService.initialize([], 'test');
41-
expect(localeService.getCurrentLocale().id).toEqual(BuiltInZhCN.id);
55+
expect(localeService.getCurrentLocale()!.id).toEqual(BuiltInZhCN.id);
4256
// Clear the cached locale value
4357
localStorage.clear();
4458
localeService.initialize([], 'test');
45-
expect(localeService.getCurrentLocale().id).toEqual('test');
59+
expect(localeService.getCurrentLocale()!.id).toEqual('test');
4660
localeService.initialize([]);
4761
// Get from the localStorage cache
48-
expect(localeService.getCurrentLocale().id).toEqual('test');
62+
expect(localeService.getCurrentLocale()!.id).toEqual('test');
4963
});
5064

5165
test('Get/Set current locale', () => {
52-
(localeService as any)._current = null;
53-
expect(localeService.getCurrentLocale()).toEqual(
54-
localeService.getDefaultLocale()
55-
);
66+
const localeService = new LocaleService();
67+
(localeService as any)._current = undefined;
68+
expect(localeService.getCurrentLocale()).toBeUndefined();
5669
localeService.addLocales([TestLocale]);
5770
localeService.setCurrentLocale(TestLocale.id);
58-
expect(localeService.getCurrentLocale().id).toEqual(TestLocale.id);
71+
expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id);
5972

6073
expect(localeService.setCurrentLocale('unknown')).toEqual(false);
6174
});
6275

6376
test('Add locales', () => {
77+
const localeService = new LocaleService();
6478
expect(localeService.getLocales().length).toBe(2);
6579
localeService.addLocales([TestLocale]);
6680
expect(localeService.getLocales().length).toBe(3);
@@ -72,6 +86,7 @@ describe('The Locale Service', () => {
7286
});
7387

7488
test('Add an locale inherit the en', () => {
89+
const localeService = new LocaleService();
7590
expect(TestLocale.source.size).toBe(0);
7691
(TestLocale as ILocale).inherit = 'en';
7792
localeService.addLocales([TestLocale]);
@@ -85,6 +100,7 @@ describe('The Locale Service', () => {
85100
});
86101

87102
test('Get a specific locale', () => {
103+
const localeService = new LocaleService();
88104
localeService.addLocales([TestLocale]);
89105
expect(localeService.getLocale(TestLocale.id)).not.toBeNull();
90106
expect(localeService.getLocale(TestLocale.id)?.id).toEqual(
@@ -93,6 +109,7 @@ describe('The Locale Service', () => {
93109
});
94110

95111
test('Remove a locale', () => {
112+
const localeService = new LocaleService();
96113
localeService.addLocales([TestLocale]);
97114
expect(localeService.getLocale(TestLocale.id)?.id).toEqual(
98115
TestLocale.id
@@ -103,9 +120,9 @@ describe('The Locale Service', () => {
103120
localeService.setCurrentLocale(TestLocale.id);
104121

105122
//Remove the current locale
106-
expect(localeService.getCurrentLocale().id).toEqual(TestLocale.id);
123+
expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id);
107124
localeService.removeLocale(TestLocale.id);
108-
expect(localeService.getCurrentLocale().id).toEqual(
125+
expect(localeService.getCurrentLocale()!.id).toEqual(
109126
localeService.getDefaultLocale().id
110127
);
111128

@@ -114,14 +131,16 @@ describe('The Locale Service', () => {
114131
});
115132

116133
test('Listen to the current locale change event', () => {
134+
const localeService = new LocaleService();
117135
const fn = jest.fn();
118136
localeService.onChange(fn);
119137
localeService.setCurrentLocale('en');
120138
expect(fn).toBeCalledTimes(1);
121-
expect(localeService.getCurrentLocale().id).toEqual('en');
139+
expect(localeService.getCurrentLocale()!.id).toEqual('en');
122140
});
123141

124142
test('Localize the source key', () => {
143+
const localeService = new LocaleService();
125144
let res = localeService.localize('test');
126145
expect(res).toEqual('');
127146

src/i18n/localeService.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface ILocaleService {
2525
/**
2626
* Get the current locale language
2727
*/
28-
getCurrentLocale(): ILocale;
28+
getCurrentLocale(): ILocale | undefined;
2929
/**
3030
* Get All locale languages
3131
*/
@@ -93,14 +93,13 @@ export class LocaleService extends Component implements ILocaleService {
9393

9494
constructor() {
9595
super();
96-
this.reset();
96+
this.initialize(BuiltInLocales);
9797
}
9898

9999
public reset(): void {
100100
localStorage.removeItem(LocaleService.STORE_KEY);
101101
this._current = undefined;
102102
this._locales.clear();
103-
this.initialize(BuiltInLocales);
104103
}
105104

106105
public getDefaultLocale(): ILocale {
@@ -128,8 +127,8 @@ export class LocaleService extends Component implements ILocaleService {
128127
this.setCurrentLocale(finalLocale);
129128
}
130129

131-
public getCurrentLocale(): ILocale {
132-
return Object.assign({}, this._current || this.getDefaultLocale());
130+
public getCurrentLocale(): ILocale | undefined {
131+
return this._current && Object.assign({}, this._current);
133132
}
134133

135134
public getLocale(id: string): ILocale | undefined {

src/services/settingsService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class SettingsService extends GlobalEvent implements ISettingsService {
107107
const theme = this.colorThemeService.getColorTheme();
108108
const locale = this.localeService.getCurrentLocale();
109109

110-
return new SettingsModel(theme.id, editorOptions!, locale.id);
110+
return new SettingsModel(theme.id, editorOptions!, locale!.id);
111111
}
112112

113113
public getDefaultSettingsTab(): BuiltInSettingsTabType {

0 commit comments

Comments
 (0)