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: local plugin custom localStorage key #12718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/docs/docs/max/i18n.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ export default {
default: 'zh-CN',
title: false,
useLocalStorage: true,
localStorageKey: 'umi_locale',
},
};
```
Expand All @@ -428,6 +429,7 @@ The detailed introduction to the configuration is as follows:
| `default` | `String` | `zh-CN` | **Default language** of the project. When a specific language is not detected, use the default language set by `default`. |
| `title` | `Boolean` | `false` | Enable [**title internationalization**](#title-internationalization). |
| `useLocalStorage` | `Boolean` | `true` | Automatically using `localStorage` to save the currently used language. |
| `localStorageKey` | `String` | `umi_locale` | Customize the key for saving language in `localStorage` |

### Title Internationalization

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/docs/max/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export default {
default: 'zh-CN',
title: false,
useLocalStorage: true,
localStorageKey: 'umi_locale',
},
};
```
Expand All @@ -427,6 +428,7 @@ export default {
| `default` | `String` | `zh-CN` | 项目**默认语言**。当检测不到具体语言时,使用 `default` 设置的默认语言。 |
| `title` | `Boolean` | `false` | 开启[**标题国际化**](#标题国际化)。 |
| `useLocalStorage` | `Boolean` | `true` | 自动使用 `localStorage` 保存当前使用的语言。 |
| `localStorageKey` | `String` | `umi_locale` | 自定义 `localStorage` 保存语言时使用的 key。 |

### 标题国际化

Expand Down
12 changes: 11 additions & 1 deletion packages/plugins/src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default (api: IApi) => {
useLocalStorage: true,
baseSeparator: '-',
antd: hasAntd,
localStorageKey: 'umi_locale',
};

api.describe({
Expand All @@ -57,6 +58,7 @@ export default (api: IApi) => {
title: zod.boolean(),
antd: zod.boolean(),
baseSeparator: zod.string(),
localStorageKey: zod.string(),
})
.partial();
},
Expand Down Expand Up @@ -120,7 +122,14 @@ export default (api: IApi) => {
dirname(require.resolve('event-emitter/package')),
);

const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = {
const {
baseSeparator,
baseNavigator,
antd,
title,
useLocalStorage,
localStorageKey,
} = {
...defaultConfig,
...(api.config.locale as ILocaleConfig),
};
Expand Down Expand Up @@ -207,6 +216,7 @@ export default (api: IApi) => {
DefaultLocale: JSON.stringify(defaultLocale),
warningPkgPath: winPath(dirname(require.resolve('warning/package'))),
reactIntlPkgPath,
localStorageKey: JSON.stringify(localStorageKey),
}),
});
// runtime.tsx
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/templates/locale/localeExports.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const getLocale = () => {
// because changing will break the app
const lang =
navigator.cookieEnabled && typeof localStorage !== 'undefined' && useLocalStorage
? window.localStorage.getItem('umi_locale')
? window.localStorage.getItem({{{ localStorageKey }}})
: '';
// support baseNavigator, default true
let browserLang;
Expand Down Expand Up @@ -241,7 +241,7 @@ export const setLocale = (lang: string, realReload: boolean = true) => {
const updater = () => {
if (getLocale() !== lang) {
if (navigator.cookieEnabled && typeof window.localStorage !== 'undefined' && useLocalStorage) {
window.localStorage.setItem('umi_locale', lang || '');
window.localStorage.setItem({{{ localStorageKey }}}, lang || '');
}
setIntl(lang);
if (realReload) {
Expand Down
Loading