From afdf7e92f89214ff1625d53408e752976bc5da0c Mon Sep 17 00:00:00 2001 From: xiaowei Date: Fri, 3 Sep 2021 15:57:56 +0800 Subject: [PATCH 1/3] fix: get the default language from localStorage --- src/i18n/localeService.ts | 9 ++++----- src/services/settingsService.ts | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/i18n/localeService.ts b/src/i18n/localeService.ts index 18536c3b1..aa0e15a5b 100644 --- a/src/i18n/localeService.ts +++ b/src/i18n/localeService.ts @@ -25,7 +25,7 @@ export interface ILocaleService { /** * Get the current locale language */ - getCurrentLocale(): ILocale; + getCurrentLocale(): ILocale | undefined; /** * Get All locale languages */ @@ -93,14 +93,13 @@ export class LocaleService extends Component implements ILocaleService { constructor() { super(); - this.reset(); + this.initialize(BuiltInLocales); } public reset(): void { localStorage.removeItem(LocaleService.STORE_KEY); this._current = undefined; this._locales.clear(); - this.initialize(BuiltInLocales); } public getDefaultLocale(): ILocale { @@ -128,8 +127,8 @@ export class LocaleService extends Component implements ILocaleService { this.setCurrentLocale(finalLocale); } - public getCurrentLocale(): ILocale { - return Object.assign({}, this._current || this.getDefaultLocale()); + public getCurrentLocale(): ILocale | undefined { + return this._current ? Object.assign({}, this._current) : undefined; } public getLocale(id: string): ILocale | undefined { diff --git a/src/services/settingsService.ts b/src/services/settingsService.ts index 903b5febd..daf2dc42e 100644 --- a/src/services/settingsService.ts +++ b/src/services/settingsService.ts @@ -107,7 +107,7 @@ export class SettingsService extends GlobalEvent implements ISettingsService { const theme = this.colorThemeService.getColorTheme(); const locale = this.localeService.getCurrentLocale(); - return new SettingsModel(theme.id, editorOptions!, locale.id); + return new SettingsModel(theme.id, editorOptions!, locale!.id); } public getDefaultSettingsTab(): BuiltInSettingsTabType { From a83009b96dc4391ab5fc98f06757f61a4fc9bfcb Mon Sep 17 00:00:00 2001 From: xiaowei Date: Fri, 3 Sep 2021 15:58:41 +0800 Subject: [PATCH 2/3] test: unit test for reset method --- src/i18n/__tests__/localeService.test.ts | 45 +++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/i18n/__tests__/localeService.test.ts b/src/i18n/__tests__/localeService.test.ts index cc67e3f13..c39fd7897 100644 --- a/src/i18n/__tests__/localeService.test.ts +++ b/src/i18n/__tests__/localeService.test.ts @@ -4,8 +4,6 @@ import { LocaleService } from '..'; import { BuiltInLocales, BuiltInZhCN, ILocale } from '../localization'; describe('The Locale Service', () => { - const localeService = container.resolve(LocaleService); - const TestLocale = { id: 'test', source: new Map(), @@ -13,54 +11,70 @@ describe('The Locale Service', () => { }; afterEach(() => { + localStorage.clear(); + }); + + test('Instance the LocaleService by IOC', () => { + const localeService = container.resolve(LocaleService); + expect(localeService).not.toBeUndefined(); + }); + + test('Reset the LocaleService', () => { + const localeService = new LocaleService(); + expect(localeService.getCurrentLocale()!.id).toBe(BuiltInZhCN.id); localeService.reset(); + expect(localeService.getCurrentLocale()).toBeUndefined(); }); test('Get default Locale', () => { + const localeService = new LocaleService(); const defaultLocale = localeService.getDefaultLocale(); expect(defaultLocale).toEqual(BuiltInZhCN); }); test('Get default Locales', () => { + const localeService = new LocaleService(); const defaultLocale = localeService.getDefaultLocales(); expect(defaultLocale).toEqual(BuiltInLocales); }); test('The size of Built-in Locales should be 2', () => { + const localeService = new LocaleService(); const locales = localeService.getLocales(); expect(locales.length).toBe(2); }); test('Initialize the locales', () => { + const localeService = new LocaleService(); localeService.initialize([TestLocale]); - expect(localeService.getCurrentLocale().id).toEqual( + expect(localeService.getCurrentLocale()!.id).toEqual( localeService.getDefaultLocale().id ); expect(localeService.getLocales().length).toBe(3); localeService.initialize([], 'test'); - expect(localeService.getCurrentLocale().id).toEqual(BuiltInZhCN.id); + expect(localeService.getCurrentLocale()!.id).toEqual(BuiltInZhCN.id); // Clear the cached locale value localStorage.clear(); localeService.initialize([], 'test'); - expect(localeService.getCurrentLocale().id).toEqual('test'); + expect(localeService.getCurrentLocale()!.id).toEqual('test'); localeService.initialize([]); // Get from the localStorage cache - expect(localeService.getCurrentLocale().id).toEqual('test'); + expect(localeService.getCurrentLocale()!.id).toEqual('test'); }); test('Get/Set current locale', () => { + const localeService = new LocaleService(); (localeService as any)._current = null; - expect(localeService.getCurrentLocale()).toEqual( - localeService.getDefaultLocale() - ); + expect(localeService.getCurrentLocale()).toBeUndefined(); localeService.addLocales([TestLocale]); localeService.setCurrentLocale(TestLocale.id); - expect(localeService.getCurrentLocale().id).toEqual(TestLocale.id); + expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id); expect(localeService.setCurrentLocale('unknown')).toEqual(false); }); test('Add locales', () => { + const localeService = new LocaleService(); expect(localeService.getLocales().length).toBe(2); localeService.addLocales([TestLocale]); expect(localeService.getLocales().length).toBe(3); @@ -72,6 +86,7 @@ describe('The Locale Service', () => { }); test('Add an locale inherit the en', () => { + const localeService = new LocaleService(); expect(TestLocale.source.size).toBe(0); (TestLocale as ILocale).inherit = 'en'; localeService.addLocales([TestLocale]); @@ -85,6 +100,7 @@ describe('The Locale Service', () => { }); test('Get a specific locale', () => { + const localeService = new LocaleService(); localeService.addLocales([TestLocale]); expect(localeService.getLocale(TestLocale.id)).not.toBeNull(); expect(localeService.getLocale(TestLocale.id)?.id).toEqual( @@ -93,6 +109,7 @@ describe('The Locale Service', () => { }); test('Remove a locale', () => { + const localeService = new LocaleService(); localeService.addLocales([TestLocale]); expect(localeService.getLocale(TestLocale.id)?.id).toEqual( TestLocale.id @@ -103,9 +120,9 @@ describe('The Locale Service', () => { localeService.setCurrentLocale(TestLocale.id); //Remove the current locale - expect(localeService.getCurrentLocale().id).toEqual(TestLocale.id); + expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id); localeService.removeLocale(TestLocale.id); - expect(localeService.getCurrentLocale().id).toEqual( + expect(localeService.getCurrentLocale()!.id).toEqual( localeService.getDefaultLocale().id ); @@ -114,14 +131,16 @@ describe('The Locale Service', () => { }); test('Listen to the current locale change event', () => { + const localeService = new LocaleService(); const fn = jest.fn(); localeService.onChange(fn); localeService.setCurrentLocale('en'); expect(fn).toBeCalledTimes(1); - expect(localeService.getCurrentLocale().id).toEqual('en'); + expect(localeService.getCurrentLocale()!.id).toEqual('en'); }); test('Localize the source key', () => { + const localeService = new LocaleService(); let res = localeService.localize('test'); expect(res).toEqual(''); From dc95957cf5b3bd00e814dbf7f8586f3eae9f65ce Mon Sep 17 00:00:00 2001 From: xiaowei Date: Tue, 7 Sep 2021 19:17:42 +0800 Subject: [PATCH 3/3] refactor: simplify the code --- src/i18n/__tests__/localeService.test.ts | 2 +- src/i18n/localeService.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/i18n/__tests__/localeService.test.ts b/src/i18n/__tests__/localeService.test.ts index c39fd7897..34480d957 100644 --- a/src/i18n/__tests__/localeService.test.ts +++ b/src/i18n/__tests__/localeService.test.ts @@ -64,7 +64,7 @@ describe('The Locale Service', () => { test('Get/Set current locale', () => { const localeService = new LocaleService(); - (localeService as any)._current = null; + (localeService as any)._current = undefined; expect(localeService.getCurrentLocale()).toBeUndefined(); localeService.addLocales([TestLocale]); localeService.setCurrentLocale(TestLocale.id); diff --git a/src/i18n/localeService.ts b/src/i18n/localeService.ts index aa0e15a5b..569468708 100644 --- a/src/i18n/localeService.ts +++ b/src/i18n/localeService.ts @@ -128,7 +128,7 @@ export class LocaleService extends Component implements ILocaleService { } public getCurrentLocale(): ILocale | undefined { - return this._current ? Object.assign({}, this._current) : undefined; + return this._current && Object.assign({}, this._current); } public getLocale(id: string): ILocale | undefined {