@sweetui/sweet-mobile
Advanced tools
Comparing version 2.2.2 to 2.2.3
@@ -16,2 +16,3 @@ /** | ||
const loadedLanguages = [] | ||
const onlineDate = {} | ||
@@ -38,4 +39,9 @@ /** | ||
if (!loadedLanguages.includes(lang)) { | ||
let fileLane = {} | ||
return import(`@/lang/${lang}`).then(msgs => { | ||
i18n.setLocaleMessage(lang, msgs.default) | ||
fileLane = msgs.default | ||
}).catch(err => { | ||
console.log('err', err) | ||
}).finally(() => { | ||
i18n.setLocaleMessage(lang, Object.assign(fileLane, onlineDate[lang] || {})) | ||
loadedLanguages.push(lang) | ||
@@ -55,3 +61,3 @@ return setI18n(lang) | ||
*/ | ||
function init() { | ||
function next() { | ||
const navLang = navigator.language || navigator.browserLanguage || 'zh' | ||
@@ -62,13 +68,14 @@ const lang = localStorage.getItem('SWEETLANG') || appConfig.language | ||
export default { | ||
i18n, | ||
/** | ||
* install钩子 | ||
* @param {Vue} Vue Vue | ||
* @param {String} lang 默认语言 | ||
*/ | ||
install(Vue) { | ||
init() | ||
Vue.prototype.$i18nLanguage = $i18nLanguage | ||
}, | ||
export default async function init(cb = () => Promise.resolve()) { | ||
await cb().then((data = {}) => { | ||
Object.assign(onlineDate, data) | ||
next() | ||
}) | ||
Vue.prototype.$i18nLanguage = $i18nLanguage | ||
return i18n | ||
} | ||
{ | ||
"name": "sweet-plugin-i18n", | ||
"version": "2.2.0", | ||
"version": "2.2.3", | ||
"description": "sweet i18n国际化方案", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,11 +7,54 @@ # sweet-i18n 国际化 | ||
// 国际化 | ||
Vue.use(sweetI18n) | ||
sweetI18n().then(i18n => { | ||
// 初始化 App | ||
const SweetApp = new Vue({ | ||
el: '#app', | ||
render: c => c('app'), | ||
... | ||
// 注入i18n | ||
i18n, | ||
}); | ||
}) | ||
const SweetApp = new Vue({ | ||
... | ||
// 注入i18n | ||
i18n: sweetI18n.i18n, | ||
}); | ||
``` | ||
语言包分成3种情况 | ||
* 设置在项目工程 src/lang 目录下 | ||
* 走网络接口请求获取 | ||
* 俩者都有,且如有重复,接口数据会覆盖项目.js文件的字段值 | ||
所以程序初始化要写在sweetI18n().then(i18n => ...)函数里面 | ||
sweetI18n方法可提供一个参数,必须是Promise函数。Demo如下,用于从接口获取语言包 | ||
```js | ||
/* 模拟ajax请求 */ | ||
sweetI18n(() => { | ||
return new Promise((resolve, reject) => { | ||
const onlineDate = { | ||
zh: { o: '哦哦哦' }, | ||
en: { o: 'OOO' }, | ||
} | ||
setTimeout(() => { | ||
resolve(onlineDate) | ||
}, 100) | ||
}) | ||
}).then(...) | ||
// 真实ajax | ||
sweetI18n(() =>{ | ||
return new Promise((resolve, reject) => { | ||
this.SWTOOL.get('url').then(data => { | ||
resolve(data) | ||
}).catch(() => reject) | ||
}) | ||
}).then(...) | ||
``` | ||
<strong>注: 返回的data数据格式必须严格</strong> | ||
```js | ||
const data ={ | ||
zh: { o: '哦哦哦' }, // zh => 语言lang 和 src/lang目录下的zh.js文件名保持一致 | ||
en: { o: 'OOO' }, | ||
} | ||
``` | ||
* 项目`app.config.js`文件 可配置language 设置初始语言。 | ||
@@ -21,13 +64,5 @@ > 本地缓存SWEETLANG字段 > appConfig.language值 > navigator.language | ||
* `setI18nLanguage`方法,用于切换lang语言包的方法,已注册到全局。 | ||
```js | ||
/** | ||
* 设置语言包 | ||
* @param {String} lang 语言文件名称 en-US | ||
*/ | ||
function $i18nLanguage(lang) { | ||
.... | ||
} | ||
``` | ||
```vue | ||
this.$i18nLanguage(lang) | ||
this.$i18nLanguage(lang) // 调用 | ||
``` | ||
@@ -34,0 +69,0 @@ * <strong>lang必须与@/lang/*目录下的文件名相同</strong> |
{ | ||
"name": "@sweetui/sweet-mobile", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "Sweet Mobile Core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
{ | ||
"name": "sweet-plugin-theme", | ||
"version": "2.2.0", | ||
"version": "2.2.3", | ||
"description": "sweet 换肤配置", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -112,3 +112,3 @@ /* | ||
}) | ||
$el.className = classes.join(' ') | ||
$el.className = classes.join(' ').trim() | ||
} | ||
@@ -130,3 +130,3 @@ }, | ||
}) | ||
$el.className = classes.join(' ') | ||
$el.className = classes.join(' ').trim() | ||
} | ||
@@ -133,0 +133,0 @@ }, |
{ | ||
"name": "sweet-plugin-tool", | ||
"version": "2.2.0", | ||
"version": "2.2.3", | ||
"description": "sweet 前端工具类库", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
{ | ||
"name": "sweet-plugin-xhr", | ||
"version": "2.1.0", | ||
"version": "2.2.3", | ||
"description": "基于axios第三方ajax组件封装的sweet 2.x 版本的组件", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
128553
407
196