@sweetui/sweet-mobile
Advanced tools
Comparing version 2.3.4 to 2.3.5
@@ -31,2 +31,13 @@ /** | ||
/** | ||
* finallyI18n 始终执行 | ||
* @param lang | ||
* @param fileLang | ||
*/ | ||
function finallyI18n(lang, fileLang) { | ||
i18n.setLocaleMessage(lang, Object.assign(fileLang, onlineDate[lang] || {})) | ||
loadedLanguages.push(lang) | ||
return setI18n(lang) | ||
} | ||
/** | ||
* 设置语言包 | ||
@@ -39,11 +50,7 @@ * @param {String} lang 语言文件名称 en-US | ||
if (!loadedLanguages.includes(lang)) { | ||
let fileLane = {} | ||
return import(`@/lang/${lang}`).then(msgs => { | ||
fileLane = msgs.default | ||
finallyI18n(lang, msgs.default || {}) | ||
}).catch(err => { | ||
console.log('err', err) | ||
}).finally(() => { | ||
i18n.setLocaleMessage(lang, Object.assign(fileLane, onlineDate[lang] || {})) | ||
loadedLanguages.push(lang) | ||
return setI18n(lang) | ||
finallyI18n(lang, {}) | ||
}) | ||
@@ -77,4 +84,2 @@ } | ||
return i18n | ||
} | ||
} |
{ | ||
"name": "@sweetui/sweet-mobile", | ||
"version": "2.3.4", | ||
"version": "2.3.5", | ||
"description": "Sweet Mobile Core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
/** | ||
* i18n国际化 | ||
* 语言包路径 @/lang/* | ||
* Created by zdliu on 2018/4/9. | ||
*/ | ||
import Vue from 'vue' | ||
import VueI18n from 'vue-i18n' | ||
import axios from 'axios' | ||
import tool from './../tool' | ||
import appConfig from '@cwd/app.config' | ||
Vue.use(VueI18n) | ||
const { key } = appConfig.themeConfig | ||
const i18n = new VueI18n() | ||
const loadedLanguages = [] | ||
const onlineDate = {} | ||
/** | ||
* 更新语言设置 | ||
* @param lang | ||
* 肤色初始化 | ||
* 取项目app.config.js的themeConfig配置 | ||
* @param {String} key 值存储在localStorage里,key根据项目变, | ||
* @param {String} name 项目肤色名称 localStorage > name | ||
* @param {String} f7name framework7肤色名称 localStorage > f7name | ||
*/ | ||
function setI18n(lang) { | ||
i18n.locale = lang | ||
axios.defaults.headers.common['Accept-Language'] = lang | ||
document.querySelector('html').setAttribute('lang', lang) | ||
localStorage.setItem('SWEETLANG', lang) | ||
return lang | ||
function init() { | ||
const $body = document.body | ||
const localConfig = JSON.parse(localStorage.getItem(key)) || '' | ||
let { name, f7name } = localConfig || appConfig.themeConfig | ||
name && tool.SWTOOL.addClass($body, `sweet-theme-${name}`) | ||
f7name && tool.SWTOOL.addClass($body, `color-theme-${f7name}`) | ||
} | ||
/** | ||
* finallyI18n 始终执行 | ||
* @param lang | ||
* @param fileLang | ||
*/ | ||
function finallyI18n(lang, fileLang) { | ||
i18n.setLocaleMessage(lang, Object.assign(fileLang, onlineDate[lang] || {})) | ||
loadedLanguages.push(lang) | ||
return setI18n(lang) | ||
const theme = { | ||
/** | ||
* 切换肤色 | ||
* @param {String} name 项目肤色名称 | ||
* @param {String} f7name framework7肤色名称 为空,则和name 同值 | ||
*/ | ||
$cutTheme(name, f7name = name) { | ||
const $body = document.body | ||
const classes = $body.className.split(' ').filter(vl => vl.indexOf('theme') === -1) | ||
classes.push(`sweet-theme-${name}`) | ||
classes.push(`color-theme-${f7name}`) | ||
$body.className = classes.join(' ') | ||
localStorage.setItem(key, JSON.stringify({ name, f7name })) | ||
} | ||
} | ||
/** | ||
* 设置语言包 | ||
* @param {String} lang 语言文件名称 en-US | ||
* @return {Promise} | ||
*/ | ||
function $i18nLanguage(lang) { | ||
if (i18n.locale !== lang) { | ||
if (!loadedLanguages.includes(lang)) { | ||
return import(`@/lang/${lang}`).then(msgs => { | ||
finallyI18n(lang, msgs.default || {}) | ||
}).catch(err => { | ||
console.log('err', err) | ||
finallyI18n(lang, {}) | ||
}) | ||
} | ||
return Promise.resolve(setI18n(lang)) | ||
} | ||
return Promise.resolve(lang) | ||
export default { | ||
theme, | ||
/** | ||
* install钩子 | ||
* @param {Vue} Vue Vue | ||
* @param {String} lang 默认语言 | ||
*/ | ||
install(Vue) { | ||
init() | ||
Vue.prototype.$cutTheme = theme.$cutTheme | ||
}, | ||
} | ||
/** | ||
* 初始化方法 本地缓存SWEETLANG字段 > appConfig.language值 > navigator.language | ||
* appConfig.language值为空时取当前浏览器navigator.language | ||
* @return {Promise} | ||
*/ | ||
function next() { | ||
const navLang = navigator.language || navigator.browserLanguage || 'zh' | ||
const lang = localStorage.getItem('SWEETLANG') || appConfig.language | ||
return $i18nLanguage(lang || navLang.substring(0, 2)) | ||
} | ||
export default async function init(cb = () => Promise.resolve()) { | ||
await cb().then((data = {}) => { | ||
Object.assign(onlineDate, data) | ||
next() | ||
}) | ||
Vue.prototype.$i18nLanguage = $i18nLanguage | ||
return i18n | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155943
547