Skip to content

Commit

Permalink
feat: local plugin custom localStorage key
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni committed Sep 23, 2024
1 parent 79499d9 commit 247533b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
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

0 comments on commit 247533b

Please sign in to comment.