Skip to content

Commit 2faaa9f

Browse files
authored
fix: let defaultLocale works (#639)
1 parent bde0274 commit 2faaa9f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/i18n/localeService.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface ILocaleService {
8484
}
8585

8686
export const STORE_KEY = `${APP_PREFIX}.localeId`;
87+
export const DEFAULT_LOCALE_ID = `${APP_PREFIX}.defaultLocaleId`;
8788

8889
@singleton()
8990
export class LocaleService extends Component implements ILocaleService {

src/provider/molecule.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { container } from 'tsyringe';
33
import React, { createContext, Component } from 'react';
44

55
import { defaultExtensions } from 'mo/extensions';
6-
import { BuiltInDefault } from 'mo/extensions/locales-defaults';
6+
import { BuiltInId } from 'mo/extensions/locales-defaults';
77
import { IExtension } from 'mo/model/extension';
88
import {
99
ExtensionService,
1010
IExtensionService,
1111
} from 'mo/services/extensionService';
1212
import { LocaleService, ILocaleService } from 'mo/i18n';
13-
import { STORE_KEY } from 'mo/i18n/localeService';
13+
import { STORE_KEY, DEFAULT_LOCALE_ID } from 'mo/i18n/localeService';
1414
import { IMonacoService, MonacoService } from 'mo/monaco/monacoService';
1515
import { ILayoutService, LayoutService } from 'mo/services';
1616
import * as controllers from 'mo/controller';
@@ -81,14 +81,22 @@ export class MoleculeProvider extends Component<IMoleculeProps> {
8181
}
8282

8383
initLocaleExts(languages: IExtension[]) {
84-
const { defaultLocale = BuiltInDefault } = this.props;
84+
const { defaultLocale = BuiltInId } = this.props;
8585

8686
this.extensionService.load(languages);
8787

8888
// And Molecule should set the correct locale before loading normal extensions in case of
8989
// the localize method returns incorrect international text caused by incorrect current locale in the normal extensions
9090
const currentLocale = localStorage.getItem(STORE_KEY) || defaultLocale;
91-
this.localeService.setCurrentLocale(currentLocale);
91+
const preDefaultLocale = localStorage.getItem(DEFAULT_LOCALE_ID);
92+
let finalLocale = currentLocale;
93+
94+
if (defaultLocale !== preDefaultLocale) {
95+
finalLocale = defaultLocale;
96+
}
97+
98+
this.localeService.setCurrentLocale(finalLocale);
99+
localStorage.setItem(DEFAULT_LOCALE_ID, defaultLocale);
92100
}
93101

94102
/**

0 commit comments

Comments
 (0)