diff --git a/packages/fes-plugin-access/src/index.js b/packages/fes-plugin-access/src/index.js index b01fb9e8..ce27de0b 100644 --- a/packages/fes-plugin-access/src/index.js +++ b/packages/fes-plugin-access/src/index.js @@ -23,7 +23,6 @@ export default (api) => { const absRuntimeFilePath = join(namespace, 'runtime.js'); - const generatedOnce = false; api.onGenerateFiles(() => { // 文件写出 const { roles = {} } = api.config.access || {}; diff --git a/packages/fes-plugin-locale/src/index.js b/packages/fes-plugin-locale/src/index.js index 1cc450b8..d20ab6ac 100644 --- a/packages/fes-plugin-locale/src/index.js +++ b/packages/fes-plugin-locale/src/index.js @@ -10,14 +10,18 @@ export default (api) => { } = api; api.describe({ + key: 'locale', config: { schema(joi) { return joi.object(); }, - default: {} + default: {}, + onChange: api.ConfigChangeType.regenerateTmpFiles } }); + api.addRuntimePluginKey(() => 'locale'); + const absoluteFilePath = join(namespace, 'core.js'); const absRuntimeFilePath = join(namespace, 'runtime.js'); @@ -30,12 +34,18 @@ export default (api) => { api.addTmpGenerateWatcherPaths(getLocaleFileBasePath); api.onGenerateFiles(() => { + // .fes配置 + const userConfig = { + locale: 'zh-CN', // default locale + fallbackLocale: 'zh-CN', // set fallback locale + legacy: true, + baseNavigator: true, // 开启浏览器语言检测 + share: true, // 用户是否需要手动改变语言 + ...api.config.locale + }; + const loacleConfigFileBasePath = getLocaleFileBasePath(); - // 文件写出 - const defaultOptions = api.config.locale || {}; - - const locales = getLocalesJSON(loacleConfigFileBasePath); api.writeTmpFile({ @@ -44,19 +54,17 @@ export default (api) => { readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'), { REPLACE_LOCALES: locales, - REPLACE_DEFAULT_OPTIONS: JSON.stringify(defaultOptions) + REPLACE_DEFAULT_OPTIONS: JSON.stringify({ + locale: userConfig.locale, + fallbackLocale: userConfig.fallbackLocale, + legacy: userConfig.legacy + }, null, 2), + BASE_NAVIGATOR: userConfig.baseNavigator, + SHARE: userConfig.share } ) }); - api.writeTmpFile({ - path: absRuntimeFilePath, - content: readFileSync( - join(__dirname, 'runtime/runtime.tpl'), - 'utf-8' - ) - }); - api.copyTmpFiles({ namespace, path: join(__dirname, 'runtime'), @@ -66,12 +74,10 @@ export default (api) => { api.addPluginExports(() => [ { - specifiers: ['useI18n', 'setLocale'], + specifiers: ['useI18n', 'locale'], source: absoluteFilePath } ]); - api.addRuntimePluginKey(() => 'onLocaleReady'); - api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`); }; diff --git a/packages/fes-plugin-locale/src/runtime/core.tpl b/packages/fes-plugin-locale/src/runtime/core.tpl index 7f27e0c7..b5f40e9c 100644 --- a/packages/fes-plugin-locale/src/runtime/core.tpl +++ b/packages/fes-plugin-locale/src/runtime/core.tpl @@ -5,13 +5,38 @@ // locales目录下以语言简称为子文件下存放配置信息 // 其他插件可以运行时修改配置 // 所有插件使用一个语言和配置 -import { createI18n, useI18n } from 'vue-i18n/dist/vue-i18n.esm-bundler.js'; +import { isRef } from 'vue'; +import { createI18n, useI18n } from 'vue-i18n/dist/vue-i18n.esm-bundler.js'; import { plugin, ApplyPluginsType } from "@@/core/coreExports"; -import SelectLang from "./views/SelectLang" +import SelectLang from "./views/SelectLang"; + +{{ #SHARE }} +// 共享出去 +plugin.share("locale", { SelectLang }); +{{ /SHARE }} const locales = {{{REPLACE_LOCALES}}}; -const defaultOptions = {{{REPLACE_DEFAULT_OPTIONS}}} +const defaultOptions = {{{REPLACE_DEFAULT_OPTIONS}}}; + +const BASE_NAVIGATOR = {{{BASE_NAVIGATOR}}}; + +const getDefaultLocale = () => { + const fes_locale = window.localStorage.getItem("fes_locale"); + if (fes_locale) { + return { + locale: fes_locale, + fallbackLocale: fes_locale, + }; + } + if (BASE_NAVIGATOR) { + return { + locale: window.navigator.language, + fallbackLocale: window.navigator.language, + }; + } + return {}; +}; const messages = {}; if (Array.isArray(locales)) { @@ -20,21 +45,53 @@ if (Array.isArray(locales)) { }); } -const i18n = createI18n({ ...defaultOptions, messages }); +const i18n = createI18n({ + ...defaultOptions, + ...getDefaultLocale(), + messages, +}); -// 共享出去 -plugin.share("locale", { i18n, SelectLang }); - -const setLocale = (locale)=>{ - i18n.global.locale = locale +window.localStorage.setItem("fes_locale", i18n.global.locale); +const setLocale = ({ lang }) => { + if (isRef(i18n.global.locale)) { + i18n.global.locale.value = lang; + } else { + i18n.global.locale = lang; + } + window.localStorage.setItem("fes_locale", lang); }; -const addLocale = (locale, messages)=>{}; +const addLocale = ({ lang, messages }) => { + messages[lang] = messages; + if (isRef(i18n.global.messages)) { + i18n.global.messages.value[lang] = messages; + } else { + i18n.global.messages[lang] = messages; + } +}; -const getAllLocales = ()=>{}; +const getAllLocales = () => { + return Object.keys( + isRef(i18n.global.messages) + ? i18n.global.messages.value + : i18n.global.messages + ).sort(); +}; -const install = (app)=>{ +const install = (app) => { + const runtimeConfig = plugin.applyPlugins({ + key: "locale", + type: ApplyPluginsType.modify, + initialValue: {}, + }); app.use(i18n); -} +}; -export { useI18n, setLocale, install } \ No newline at end of file +const locale = { + setLocale, + addLocale, + getAllLocales, + messages, +}; + +export { useI18n, locale, install }; diff --git a/packages/fes-plugin-locale/src/runtime/runtime.js b/packages/fes-plugin-locale/src/runtime/runtime.js new file mode 100644 index 00000000..c9a39e3c --- /dev/null +++ b/packages/fes-plugin-locale/src/runtime/runtime.js @@ -0,0 +1,5 @@ +import { install } from './core'; + +export function onAppCreated({ app }) { + install(app); +} diff --git a/packages/fes-plugin-locale/src/runtime/runtime.tpl b/packages/fes-plugin-locale/src/runtime/runtime.tpl deleted file mode 100644 index 41c090a7..00000000 --- a/packages/fes-plugin-locale/src/runtime/runtime.tpl +++ /dev/null @@ -1,5 +0,0 @@ -import { install } from "./core"; - -export function onAppCreated({ app }) { - install(app) -} diff --git a/packages/fes-plugin-locale/src/runtime/views/SelectLang.vue b/packages/fes-plugin-locale/src/runtime/views/SelectLang.vue index 751f6381..83b182c3 100644 --- a/packages/fes-plugin-locale/src/runtime/views/SelectLang.vue +++ b/packages/fes-plugin-locale/src/runtime/views/SelectLang.vue @@ -4,7 +4,7 @@