From 247533b6eec5d284079fba1cb78d4b13e39dcde3 Mon Sep 17 00:00:00 2001 From: xiaohuoni Date: Mon, 23 Sep 2024 16:31:00 +0800 Subject: [PATCH] feat: local plugin custom localStorage key --- docs/docs/docs/max/i18n.en-US.md | 2 ++ docs/docs/docs/max/i18n.md | 2 ++ packages/plugins/src/locale.ts | 12 +++++++++++- packages/plugins/templates/locale/localeExports.tpl | 4 ++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/docs/docs/max/i18n.en-US.md b/docs/docs/docs/max/i18n.en-US.md index 979082ac6a6c..8b857ae8d743 100644 --- a/docs/docs/docs/max/i18n.en-US.md +++ b/docs/docs/docs/max/i18n.en-US.md @@ -414,6 +414,7 @@ export default { default: 'zh-CN', title: false, useLocalStorage: true, + localStorageKey: 'umi_locale', }, }; ``` @@ -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 diff --git a/docs/docs/docs/max/i18n.md b/docs/docs/docs/max/i18n.md index 59ce33439601..157a712c628a 100644 --- a/docs/docs/docs/max/i18n.md +++ b/docs/docs/docs/max/i18n.md @@ -413,6 +413,7 @@ export default { default: 'zh-CN', title: false, useLocalStorage: true, + localStorageKey: 'umi_locale', }, }; ``` @@ -427,6 +428,7 @@ export default { | `default` | `String` | `zh-CN` | 项目**默认语言**。当检测不到具体语言时,使用 `default` 设置的默认语言。 | | `title` | `Boolean` | `false` | 开启[**标题国际化**](#标题国际化)。 | | `useLocalStorage` | `Boolean` | `true` | 自动使用 `localStorage` 保存当前使用的语言。 | +| `localStorageKey` | `String` | `umi_locale` | 自定义 `localStorage` 保存语言时使用的 key。 | ### 标题国际化 diff --git a/packages/plugins/src/locale.ts b/packages/plugins/src/locale.ts index c048f1f2837a..507b68872d03 100644 --- a/packages/plugins/src/locale.ts +++ b/packages/plugins/src/locale.ts @@ -43,6 +43,7 @@ export default (api: IApi) => { useLocalStorage: true, baseSeparator: '-', antd: hasAntd, + localStorageKey: 'umi_locale', }; api.describe({ @@ -57,6 +58,7 @@ export default (api: IApi) => { title: zod.boolean(), antd: zod.boolean(), baseSeparator: zod.string(), + localStorageKey: zod.string(), }) .partial(); }, @@ -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), }; @@ -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 diff --git a/packages/plugins/templates/locale/localeExports.tpl b/packages/plugins/templates/locale/localeExports.tpl index 8224eb065b7f..bf55594f481a 100644 --- a/packages/plugins/templates/locale/localeExports.tpl +++ b/packages/plugins/templates/locale/localeExports.tpl @@ -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; @@ -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) {