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

fix: update the LocaleService initial logic #397

Merged
merged 3 commits into from
Sep 7, 2021
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
47 changes: 33 additions & 14 deletions src/i18n/__tests__/localeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,77 @@ 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(),
name: 'test',
};

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', () => {
(localeService as any)._current = null;
expect(localeService.getCurrentLocale()).toEqual(
localeService.getDefaultLocale()
);
const localeService = new LocaleService();
(localeService as any)._current = undefined;
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);
Expand All @@ -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]);
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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
);

Expand All @@ -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('');

Expand Down
9 changes: 4 additions & 5 deletions src/i18n/localeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ILocaleService {
/**
* Get the current locale language
*/
getCurrentLocale(): ILocale;
getCurrentLocale(): ILocale | undefined;
/**
* Get All locale languages
*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

public getLocale(id: string): ILocale | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/services/settingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down