mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat: locale插件暴漏onLocaleChange事件
This commit is contained in:
parent
9b072f828a
commit
2e75e59eeb
@ -69,11 +69,9 @@ export default {
|
|||||||
|
|
||||||
想了解更多语言信息配置、匹配规则,请参考 [Vue I18n](https://vue-i18n.intlify.dev/guide/essentials/syntax.html) 文档。
|
想了解更多语言信息配置、匹配规则,请参考 [Vue I18n](https://vue-i18n.intlify.dev/guide/essentials/syntax.html) 文档。
|
||||||
|
|
||||||
|
|
||||||
### 多层配置
|
### 多层配置
|
||||||
如果国际化内容较多,希望模块化配置,则可以这样:
|
|
||||||
|
|
||||||
```
|
如果国际化内容较多,希望模块化配置,则可以这样:
|
||||||
src
|
src
|
||||||
├── locales
|
├── locales
|
||||||
│ ├── zh-CN.js
|
│ ├── zh-CN.js
|
||||||
@ -84,6 +82,7 @@ src
|
|||||||
└── pages
|
└── pages
|
||||||
│ └── index.vue
|
│ └── index.vue
|
||||||
└── app.js
|
└── app.js
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
插件会把相同语言的配置合并在一起!
|
插件会把相同语言的配置合并在一起!
|
||||||
@ -145,7 +144,19 @@ export default {
|
|||||||
|
|
||||||
### 运行时配置
|
### 运行时配置
|
||||||
|
|
||||||
暂无。
|
## onLocaleChange
|
||||||
|
|
||||||
|
当语言环境发生变化时,会触发此函数。
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { defineRuntimeConfig } from '@fesjs/fes';
|
||||||
|
|
||||||
|
export default defineRuntimeConfig({
|
||||||
|
onLocaleChange: ({ t, locale }) => {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
@ -24,6 +24,8 @@ export default (api) => {
|
|||||||
|
|
||||||
api.addRuntimePluginKey(() => 'locale');
|
api.addRuntimePluginKey(() => 'locale');
|
||||||
|
|
||||||
|
api.addRuntimePluginKey(() => 'onLocaleChange');
|
||||||
|
|
||||||
const absoluteFilePath = join(namespace, 'core.js');
|
const absoluteFilePath = join(namespace, 'core.js');
|
||||||
|
|
||||||
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import { isRef, unref } from 'vue';
|
import { isRef, unref } from 'vue';
|
||||||
import { createI18n, useI18n } from '{{{ VUE_I18N_PATH }}}';
|
import { createI18n, useI18n } from '{{{ VUE_I18N_PATH }}}';
|
||||||
import locales from './locales'
|
import locales from './locales'
|
||||||
|
import { plugin, ApplyPluginsType } from '@@/core/coreExports';
|
||||||
|
|
||||||
const defaultOptions = {{{REPLACE_DEFAULT_OPTIONS}}};
|
const defaultOptions = {{{REPLACE_DEFAULT_OPTIONS}}};
|
||||||
|
|
||||||
@ -51,6 +51,8 @@ const i18n = createI18n({
|
|||||||
messages,
|
messages,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const t = i18n.global.t;
|
||||||
|
|
||||||
window.localStorage.setItem("fes_locale", unref(i18n.global.locale));
|
window.localStorage.setItem("fes_locale", unref(i18n.global.locale));
|
||||||
const setLocale = ({ locale }) => {
|
const setLocale = ({ locale }) => {
|
||||||
if (isRef(i18n.global.locale)) {
|
if (isRef(i18n.global.locale)) {
|
||||||
@ -59,6 +61,11 @@ const setLocale = ({ locale }) => {
|
|||||||
i18n.global.locale = locale;
|
i18n.global.locale = locale;
|
||||||
}
|
}
|
||||||
window.localStorage.setItem("fes_locale", locale);
|
window.localStorage.setItem("fes_locale", locale);
|
||||||
|
plugin.applyPlugins({
|
||||||
|
key: 'onLocaleChange',
|
||||||
|
type: ApplyPluginsType.event,
|
||||||
|
args: { i18n, t, locale: unref(i18n.global.locale) },
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLocale = () => {
|
const getLocale = () => {
|
||||||
@ -82,10 +89,13 @@ const getAllLocales = () => {
|
|||||||
|
|
||||||
const install = (app) => {
|
const install = (app) => {
|
||||||
app.use(i18n);
|
app.use(i18n);
|
||||||
|
plugin.applyPlugins({
|
||||||
|
key: 'onLocaleChange',
|
||||||
|
type: ApplyPluginsType.event,
|
||||||
|
args: { i18n, t, locale: unref(i18n.global.locale) },
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const t = i18n.global.t;
|
|
||||||
|
|
||||||
const locale = {
|
const locale = {
|
||||||
setLocale,
|
setLocale,
|
||||||
getLocale,
|
getLocale,
|
||||||
|
3
packages/fes-plugin-locale/types.d.ts
vendored
3
packages/fes-plugin-locale/types.d.ts
vendored
@ -21,4 +21,7 @@ declare module '@fesjs/fes' {
|
|||||||
}
|
}
|
||||||
| false;
|
| false;
|
||||||
}
|
}
|
||||||
|
interface PluginRuntimeConfig {
|
||||||
|
onLocaleChange: (params: { t: VueI18n['t']; locale: string }) => void;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,11 @@ import { ref, watch } from 'vue';
|
|||||||
import PageLoading from '@/components/pageLoading.vue';
|
import PageLoading from '@/components/pageLoading.vue';
|
||||||
import UserCenter from '@/components/userCenter.vue';
|
import UserCenter from '@/components/userCenter.vue';
|
||||||
|
|
||||||
|
export function onLocaleChange({ locale }) {
|
||||||
|
// 切换语言
|
||||||
|
console.log(locale);
|
||||||
|
}
|
||||||
|
|
||||||
export const beforeRender = {
|
export const beforeRender = {
|
||||||
loading: <PageLoading />,
|
loading: <PageLoading />,
|
||||||
action() {
|
action() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user