Skip to content

Commit

Permalink
fix: i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
yandadaFreedom committed Jul 14, 2023
1 parent 67b611a commit 4f0aedf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
25 changes: 19 additions & 6 deletions packages/webpack-plugin/lib/runtime/optionProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function getWxsMixin (wxsModules) {
}
}

function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, tabBarMap }) {
function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, tabBarMap, i18n, Mpx, context = {} }) {
const option = {}
// 对于app中的组件需要全局注册
for (const componentName in componentsMap) {
Expand Down Expand Up @@ -307,7 +307,15 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
// 注入pinia
option.pinia = global.__mpxPinia
}

if (i18n) {
const { createI18n, i18nCfg, VueI18n } = i18n
if (!isBrowser && context?.lang) {
i18nCfg.locale = context.lang
}
const i18nObj = createI18n(i18nCfg, VueI18n)
Vue.use(i18nObj)
Mpx.i18n = i18nObj
}
const app = new Vue({
...option,
render: (h) => h(App)
Expand All @@ -318,17 +326,20 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
}
}

export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap, webConfig }) {
export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap, webConfig, i18n, Mpx }) {
if (!isBrowser) {
return context => {
const { app, router, pinia = {} } = createApp({
const { app, router, pinia } = createApp({
App,
componentsMap,
Vue,
pagesMap,
firstPage,
VueRouter,
tabBarMap
tabBarMap,
i18n,
Mpx,
context
})
if (App.onSSRAppCreated) {
return App.onSSRAppCreated({ pinia, router, app, context })
Expand All @@ -352,7 +363,9 @@ export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue
pagesMap,
firstPage,
VueRouter,
tabBarMap
tabBarMap,
i18n,
Mpx
})
if (window.__INITIAL_STATE__) {
pinia.state.value = JSON.parse(window.__INITIAL_STATE__)
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack-plugin/lib/web/processMainScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module.exports = function (script, {
if (i18n) {
output += buildI18n({ i18n, loaderContext })
}

output += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, jsonConfig, webConfig, isMain: true, globalTabBar })
output += `export default processAppOption({
App,
Expand All @@ -47,7 +46,9 @@ module.exports = function (script, {
componentsMap: ${shallowStringify(componentsMap)},
Vue,
VueRouter,
webConfig: ${JSON.stringify(webConfig)}
webConfig: ${JSON.stringify(webConfig)},
i18n: i18nPageConfig,
Mpx
})`

callback(null, {
Expand Down
10 changes: 7 additions & 3 deletions packages/webpack-plugin/lib/web/script-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,19 @@ function buildI18n ({ i18n, loaderContext }) {
delete i18nObj[`${key}Path`]
}
})
i18nContent += ` const i18nPageConfig = {}\n`
i18nContent += ` const i18nCfg = ${JSON.stringify(i18nObj)}\n`
Object.keys(requestObj).forEach((key) => {
i18nContent += ` i18nCfg.${key} = require(${requestObj[key]})\n`
})
i18nContent += ' i18nCfg.legacy = false\n'
i18nContent += ` const i18n = createI18n(i18nCfg, VueI18n)
Vue.use(i18n)
Mpx.i18n = i18n\n`
i18nContent += ' i18nPageConfig.i18nCfg = i18nCfg\n'
i18nContent += ' i18nPageConfig.createI18n = createI18n\n'
i18nContent += ' i18nPageConfig.VueI18n = VueI18n\n'
return i18nContent
// i18nContent += ` const i18n = createI18n(i18nCfg, VueI18n)
// Vue.use(i18n)
// Mpx.i18n = i18n\n`
}

module.exports = {
Expand Down

0 comments on commit 4f0aedf

Please sign in to comment.