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: create instanceService #733

Merged
merged 14 commits into from
Jun 7, 2022
Merged
Prev Previous commit
Next Next commit
refactor: improve the localeService
  • Loading branch information
mortalYoung committed May 11, 2022
commit e43d4d682c1beb3e151b8a3c2a4487a8f36cc676
61 changes: 19 additions & 42 deletions src/i18n/localeService.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { APP_PREFIX } from 'mo/common/const';
import {
ILocale,
LocalizationEvent,
BuiltInLocales,
BuiltInDefault,
} from 'mo/i18n/localization';
import logger from 'mo/common/logger';
import { ILocale, LocalizationEvent } from 'mo/i18n/localization';

import { Component } from 'mo/react';
import { singleton } from 'tsyringe';

export interface ILocaleService {
/**
* Initialize the locales data, and the default current locale language,
* this method first uses the cached `locale` in localStorage, then use the
* localeId argument, if both the values are null, finally apply the built-in BuiltInZhCN
* Initialize the locales data, and the current locale language,
* @param locales
* @param localeId
*/
initialize(locales: ILocale[], localeId?: string): void;
initialize(locales: ILocale[], localeId: string): void;
/**
* Set the current locale language by id
* @param id
Expand All @@ -36,14 +30,6 @@ export interface ILocaleService {
* @param id
*/
getLocale(id: string): ILocale | undefined;
/**
* Get the default locale
*/
getDefaultLocale(): ILocale;
/**
* Get the default locales;
*/
getDefaultLocales(): ILocale[];
/**
* Add multiple local languages
* @param locales
Expand Down Expand Up @@ -91,7 +77,7 @@ export class LocaleService extends Component implements ILocaleService {
state = {};
private static LOCALIZE_REPLACED_WORD = '${i}';

private _locales: Map<string, ILocale> = new Map();
private _locales = new Map<string, ILocale>();
private _current: ILocale | undefined;

constructor() {
Expand All @@ -104,37 +90,22 @@ export class LocaleService extends Component implements ILocaleService {
this._locales.clear();
}

public getDefaultLocale(): ILocale {
return Object.assign({}, BuiltInDefault);
}

public getDefaultLocales(): ILocale[] {
return BuiltInLocales.concat();
}

public getLocales(): ILocale[] {
return Array.from(this._locales.values());
}

public initialize(locales: ILocale[], localeId?: string) {
public initialize(locales: ILocale[], localeId: string) {
this.addLocales(locales);
let finalLocale = BuiltInDefault!.id;
if (localeId) {
finalLocale = localeId;
}
const cachedLocaleId = localStorage.getItem(STORE_KEY);
if (cachedLocaleId) {
finalLocale = cachedLocaleId;
if (this._locales.get(localeId)) {
this._current = this._locales.get(localeId);
} else {
logger.warn(`Cannot initialize the locale with ${localeId}`);
this._current = this._locales.values().next().value;
}

this.setCurrentLocale(finalLocale);
}

public getCurrentLocale(): ILocale | undefined {
return (
(this._current && Object.assign({}, this._current)) ||
BuiltInDefault
);
return this._current && Object.assign({}, this._current);
}

public getLocale(id: string | null): ILocale | undefined {
Expand All @@ -145,8 +116,14 @@ export class LocaleService extends Component implements ILocaleService {
public removeLocale(id: string): ILocale | undefined {
const locale = this._locales.get(id);
if (locale !== undefined) {
if (this._locales.size === 1) {
logger.error(
"You can't remove this Locale because there must have one locale at least"
);
return undefined;
}
if (this._current && this._current.id === locale.id) {
this._current = this.getDefaultLocale();
this._current = this._locales.values().next().value;
}
this._locales.delete(id);
return locale;
Expand Down
4 changes: 2 additions & 2 deletions src/model/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export interface ISettings {
export class SettingsModel implements ISettings {
colorTheme: string;
editor: IEditorOptions;
locale: string;
locale?: string;

constructor(colorTheme: string, editor: IEditorOptions, locale: string) {
constructor(colorTheme: string, editor: IEditorOptions, locale?: string) {
this.colorTheme = colorTheme;
this.editor = editor;
this.locale = locale;
Expand Down
10 changes: 7 additions & 3 deletions src/services/instanceService.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ReactNode } from 'react';
import { ILocale } from 'mo/i18n';
import { container } from 'tsyringe';
import * as controllers from 'mo/controller';
import type { Controller } from 'mo/react';
import { defaultExtensions } from 'mo/extensions';
import { GlobalEvent } from 'mo/common/event';
import { IConfigProps, IServiceCollection } from 'mo/provider/create';
import { ReactNode } from 'react';
import { IExtension } from 'mo/model';
import { STORE_KEY } from 'mo/i18n/localeService';

interface IInstanceServiceProps {
render: (dom: any) => void;
Expand Down Expand Up @@ -47,8 +48,11 @@ export default class InstanceService
const languages = cur.contributes?.languages || [];
return pre.concat(languages);
}, [] as ILocale[]);
this._services.i18n.initialize(locales);
this._services.i18n.setCurrentLocale(this._config.defaultLocale);

this._services.i18n.initialize(
locales,
localStorage.getItem(STORE_KEY) || this._config.defaultLocale
);
};

public render = (workbench: ReactNode) => {
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 editorOptions = this.editorService.getState().editorOptions;
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