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: when has runtimeLocale.setLocale setLocale not work #543

Merged
merged 1 commit into from
Feb 26, 2021
Merged
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
48 changes: 27 additions & 21 deletions packages/plugin-locale/src/templates/localeExports.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,40 @@ export const setLocale = (lang: string, realReload: boolean = true) => {
type: ApplyPluginsType.modify,
initialValue: {},
});

const updater = () => {
if (lang !== undefined && !localeExp.test(lang)) {
// for reset when lang === undefined
throw new Error('setLocale lang format error');
}
if (getLocale() !== lang) {
if (typeof window.localStorage !== 'undefined' && useLocalStorage) {
window.localStorage.setItem('umi_locale', lang || '');
}
setIntl(lang);
if (realReload) {
window.location.reload();
} else {
event.emit(LANG_CHANGE_EVENT, lang);
// chrome 不支持这个事件。所以人肉触发一下
if (window.dispatchEvent) {
const event = new Event('languagechange');
window.dispatchEvent(event);
}
}
}
}

if (typeof runtimeLocale?.setLocale === 'function') {
runtimeLocale.setLocale({
lang,
realReload,
updater: (updateLang = lang) => event.emit(LANG_CHANGE_EVENT, updateLang),
updater: updater,
});
return;
}
if (lang !== undefined && !localeExp.test(lang)) {
// for reset when lang === undefined
throw new Error('setLocale lang format error');
}
if (getLocale() !== lang) {
if (typeof window.localStorage !== 'undefined' && useLocalStorage) {
window.localStorage.setItem('umi_locale', lang || '');
}
setIntl(lang);
if (realReload) {
window.location.reload();
} else {
event.emit(LANG_CHANGE_EVENT, lang);
// chrome 不支持这个事件。所以人肉触发一下
if (window.dispatchEvent) {
const event = new Event('languagechange');
window.dispatchEvent(event);
}
}
}

updater();
};

let firstWaring = true;
Expand Down