Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
Hello,world!
Browse files Browse the repository at this point in the history
  • Loading branch information
YFengFly committed Sep 19, 2018
1 parent a9140a0 commit 5d3d741
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# vue-language-switcher

Small,simple,easy to use.

简单,小巧,易使用。

## 本插件特点 | Why use it

### 1. 小巧 | Small

\> 1.5 KiB

### 2. 参数、语言列表使用数组承载,不需要将同一个意思写两遍

在官方的 awesome-vue (https://github.com/vuejs/awesome-vue) 中,有许多不错的i18n插件,不过大多用起来不是太简单。

大多数i18n等多语言切换的使用格式为:

<code>
{
zh: {
hello: '您好'
},
en: {
hello: 'Hello'
}
}
</code>

也有小部分这样的:

<code>
{
hello: {
​ zh: '您好',
​ en: 'Hello'
}
}
</code>

我偏向于能省则省,

`['您好','Hello']`

当然,是会有顺序设置的,默认为:

`['zh','en']`

设置语言则仅需要在Vue中 `$lSet('en')` 即可切换到英文。

所以,如果你要求不高,只是一个非常小的项目,要在IE9这样的不支持ES6的浏览器,尤其是不考虑用Webpack等东东进行打包...

可以试试鸭~

---

## 安装 | Install

`Vue.use(VueLangSwitcher,参数 | Parameters)`


## 使用 | How to use

`$l(Data)`

将 Data 挂载于 Vue 中

使用时建议:将数据(Data)与展示(View)根据情况做分离

## Data格式 | Format

`{key:['语言1','2nd language']}`

## 参数 | Parameters

locale:语言;默认:`'zh'`

index:语言列表。默认:`['zh', 'en']`

title:`{title:['中文标题','English title']}`(需要与Data一致);默认为title标签内容

## 方法 | Methods

方法均在Vue的原型链中。

`$lSet(val)` | val:语言,默认zh,默认可选en

`$lReset()` | 无参数,清除`localStorage(vlsLang)`,恢复语言为默认值
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "vue-language-switcher",
"version": "0.1.0",
"description": "Small,simple,easy to use.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/YFengFly/vue-language-switcher.git"
},
"author": "YFeng",
"license": "MIT",
"bugs": {
"url": "https://github.com/YFengFly/vue-language-switcher/issues"
},
"homepage": "https://github.com/YFengFly/vue-language-switcher#readme"
}
56 changes: 56 additions & 0 deletions src/vueLanguageSwitcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var VueLangSwitcher = {
install: function (Vue, _options) {
// Initial
var vlsThis = this
if (_options === void 0) _options = {}
var locale = localStorage.getItem('vlsLang') || _options.locale || 'zh'
var index = _options.index || ['zh', 'en']
var title = _options.title || document.title
var indexJSON = {}
index.forEach(function (val, i) {
indexJSON[val] = i
})
this.opt = { //opt:options
locale: locale,
index: indexJSON,
title: title
}

setTitle()

// Methods

function setTitle() {
var opt = vlsThis.opt
if (Array.isArray(opt.title)) {
document.title = opt.title[opt.index[opt.locale]]
}
}

Vue.prototype.$l = function (val) {
if (Array.isArray(val)) {
var opt = vlsThis.opt
return val[opt.index[opt.locale]]
} else if (typeof val === 'string') {
return val
} else {
console.error('语言格式异常:\n', val)
return '【** 无法获取信息,请联系管理员 Message got wrong,please connect site manager. **】'
}
}

Vue.prototype.$lSet = function (val) {
vlsThis.opt.locale = val
localStorage.setItem('vlsLang', val)
this.$forceUpdate()
setTitle()
}

Vue.prototype.$lReset = function () {
vlsThis.opt.locale = 'zh'
localStorage.clear('vlsLang')
this.$forceUpdate()
setTitle()
}
}
}

0 comments on commit 5d3d741

Please sign in to comment.