提交 0afa02f1 编写于 作者: fxy060608's avatar fxy060608

wip(i18n): getLocale,setLocale,onLocaleChange

上级 afe4d8e3
......@@ -184,7 +184,8 @@ const ui = [
'setLeftWindowStyle',
'setRightWindowStyle',
'getLocale',
'setLocale'
'setLocale',
'onLocaleChange'
]
const event = [
......@@ -267,4 +268,4 @@ const apis = [
...ad
]
module.exports = apis
module.exports = apis
......@@ -35,6 +35,10 @@
]
]
],
"interceptors": [
"/core/service/api/base/interceptor.js",
[]
],
"request": [
"/platforms/h5/service/api/network/request.js",
[
......@@ -837,6 +841,18 @@
"/platforms/h5/service/api/ui/windows.js",
[]
],
"getLocale": [
"/core/service/api/ui/locale.js",
[]
],
"setLocale": [
"/core/service/api/ui/locale.js",
[]
],
"onLocaleChange": [
"/core/service/api/ui/locale.js",
[]
],
"$emit": [
"/platforms/h5/service/api/base/event-bus.js",
[
......
......@@ -47,3 +47,22 @@ export const i18nMixin = i18n.mixin = {
}
export const setLocale = i18n.setLocale
export const getLocale = i18n.getLocale
export function initAppLocale (Vue, appVm) {
const state = Vue.observable({
locale: i18n.getLocale()
})
const localeWatchers = []
appVm.$watchLocale = (fn) => {
localeWatchers.push(fn)
}
Object.defineProperty(appVm, '$locale', {
get () {
return state.locale
},
set (v) {
state.locale = v
localeWatchers.forEach(watch => watch(v))
}
})
}
export {
getLocale,
setLocale
} from 'uni-core/helpers/i18n'
import {
invoke
} from 'uni-core/service/bridge'
import {
i18n
} from 'uni-helpers/i18n'
export function getLocale () {
// 优先使用 $locale
const app = getApp({
allowDefault: true
})
if (app && app.$vm) {
return app.$vm.$locale
}
return i18n.getLocale()
}
export function setLocale (locale) {
const oldLocale = getApp().$vm.$locale
if (oldLocale !== locale) {
getApp().$vm.$locale = locale
if (__PLATFORM__ === 'app-plus') {
const pages = getCurrentPages()
pages.forEach((page) => {
UniServiceJSBridge.publishHandler(
'setLocale',
locale,
page.$page.id
)
})
weex.requireModule('plus').setLanguage(locale)
}
callbacks.forEach(callbackId => {
invoke(callbackId, { locale })
})
return true
}
return false
}
const callbacks = []
export function onLocaleChange (callbackId) {
callbacks.push(callbackId)
}
import {
initAppLocale
} from 'uni-helpers/i18n'
import initRouterGuard from './router-guard'
let appVm = false
......@@ -23,7 +27,8 @@ export function getCurrentPages (isAll = false, ignoreError = false) {
childrenVm = layoutVm
}
childrenVm.$children.forEach(vm => {
if (tabBarVm !== vm && vm.$children.length && vm.$children[0].$options.name === 'Page' && vm.$children[0].$slots
if (tabBarVm !== vm && vm.$children.length && vm.$children[0].$options.name === 'Page' && vm.$children[0]
.$slots
.page) {
// vm.$children[0]=Page->PageBody->RealPage
const pageBody = vm.$children[0].$children.find(vm => vm.$options.name === 'PageBody')
......@@ -63,11 +68,11 @@ export function getCurrentPages (isAll = false, ignoreError = false) {
return pages
}
export default function createApp (vm, routes) {
export default function createApp (Vue, vm, routes) {
appVm = vm
appVm.$vm = vm
appVm.globalData = appVm.$options.globalData || {}
initAppLocale(Vue, appVm)
// initEvents(appVm)
initRouterGuard(appVm, routes)
}
......@@ -10,10 +10,10 @@ export {
}
from './create-app'
export function createAppMixin (routes, entryRoute) {
export function createAppMixin (Vue, routes, entryRoute) {
return {
created: function AppCreated () {
createApp(this, routes)
createApp(Vue, this, routes)
// TODO
if (!entryRoute.meta.name) { // PageNotFound
UniServiceJSBridge.emit('onPageNotFound', {
......
......@@ -147,7 +147,7 @@ export default {
keepAliveInclude
}
}
const appMixin = createAppMixin(routes, entryRoute)
const appMixin = createAppMixin(Vue, routes, entryRoute)
// mixin app hooks
Object.keys(appMixin).forEach(hook => {
options[hook] = options[hook] ? [].concat(appMixin[hook], options[hook]) : [
......@@ -207,4 +207,4 @@ export default {
Vue.use(VueRouter)
}
}
}
......@@ -21,4 +21,4 @@ export const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE'
export const WEBVIEW_INSERTED = 'webviewInserted'
export const WEBVIEW_REMOVED = 'webviewRemoved'
export const WEBVIEW_ID_PREFIX = 'webviewId'
export const SET_LOCALE = 'i18n.setLocale'
export const SET_LOCALE = 'setLocale'
import {
initAppLocale
} from 'uni-helpers/i18n'
import {
callAppHook
} from 'uni-core/service/plugins/util'
......@@ -207,12 +211,13 @@ export function clearTempFile () {
})
}
export function registerApp (appVm) {
export function registerApp (appVm, Vue) {
if (process.env.NODE_ENV !== 'production') {
console.log('[uni-app] registerApp')
}
appCtx = appVm
appCtx.$vm = appVm
initAppLocale(Vue, appVm)
Object.assign(appCtx, defaultApp) // 拷贝默认实现
......@@ -233,7 +238,7 @@ export function registerApp (appVm) {
initSubscribeHandlers()
initAppLaunch(appVm)
initAppLaunch(Vue, appVm)
// 10s后清理临时文件
setTimeout(clearTempFile, 10000)
......@@ -241,4 +246,4 @@ export function registerApp (appVm) {
__uniConfig.ready = true
process.env.NODE_ENV !== 'production' && perf('registerApp')
}
}
......@@ -72,12 +72,12 @@ export default {
console.log('[uni-app] launchApp')
}
plus.updateConfigInfo && plus.updateConfigInfo()
registerApp(this)
registerApp(this, Vue)
oldMount.call(this, el, hydrating)
})
return
}
registerApp(this)
registerApp(this, Vue)
}
return oldMount.call(this, el, hydrating)
}
......@@ -100,4 +100,4 @@ export default {
return callback ? undefined : result
}
}
}
}
......@@ -7,8 +7,7 @@ import {
WEBVIEW_READY,
WEB_INVOKE_APPSERVICE,
WEBVIEW_INSERTED,
WEBVIEW_REMOVED,
SET_LOCALE
WEBVIEW_REMOVED
} from '../../../constants'
import {
......@@ -28,10 +27,6 @@ import {
onWebviewRemoved
} from './on-webview-lifecycle'
import {
i18n
} from 'uni-core/helpers/i18n'
export function initSubscribeHandlers () {
const {
on,
......@@ -75,12 +70,4 @@ export function initSubscribeHandlers () {
subscribe(WEBVIEW_INSERTED, onWebviewInserted)
subscribe(WEBVIEW_REMOVED, onWebviewRemoved)
i18n.i18n.watchLocale(locale => {
const pages = getCurrentPages()
pages.forEach(page => {
publishHandler(SET_LOCALE, locale, page.$page.id)
})
weex.requireModule('plus').setLanguage(locale)
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册