Skip to content

Commit 357dda3

Browse files
committed
Internationalization support
1 parent cb1e176 commit 357dda3

File tree

13 files changed

+166
-30
lines changed

13 files changed

+166
-30
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ import VueLoadmore from 'vuejs-loadmore';
3434
Vue.use(VueLoadmore);
3535
```
3636

37+
## Internationalization support
38+
39+
Support Chinese zh-CN and English en-US, the default is zh-CN.
40+
41+
```js
42+
import VueLoadmore from 'vuejs-loadmore';
43+
44+
Vue.use(VueLoadmore, {
45+
lang: 'en-US'
46+
})
47+
```
48+
49+
You can also use `locale.use()` to specify the language.
50+
51+
```js
52+
import VueLoadmore, { locale } from 'vuejs-loadmore';
53+
54+
Vue.use(VueLoadmore);
55+
locale.use('en-US');
56+
```
57+
3758
## Usage
3859

3960
### Basic Usage

README.zh-CN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ import VueLoadmore from 'vuejs-loadmore';
3434
Vue.use(VueLoadmore);
3535
```
3636

37+
## 国际化支持
38+
39+
支持中文 zh-CN 和英文 en-US, 默认为 zh-CN。
40+
41+
```js
42+
import VueLoadmore from 'vuejs-loadmore';
43+
44+
Vue.use(VueLoadmore, {
45+
lang: 'en-US'
46+
})
47+
```
48+
49+
你也可以使用 `locale.use()` 指定语言。
50+
51+
```js
52+
import VueLoadmore, { locale } from 'vuejs-loadmore';
53+
54+
Vue.use(VueLoadmore);
55+
locale.use('en-US');
56+
```
57+
3758
## 用法
3859

3960
### 基础用法

example/src/App.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<div>
33
<div class="loadmore-head">
4-
<div class="text">{{ language === 'Chinese' ? '下拉刷新上拉加载' : 'pull up and pull down' }}</div>
5-
<div class="lan" @click="changeLanguage">{{ language === 'Chinese' ? 'English' : 'Chinese' }}</div>
4+
<div class="text">{{ language === 'zh-CN' ? '下拉刷新上拉加载' : 'pull up and pull down' }}</div>
5+
<div class="lan" @click="changeLanguage">{{ language === 'zh-CN' ? 'English' : 'Chinese' }}</div>
66
</div>
77
<div class="wrap">
88
<vue-loadmore
@@ -13,7 +13,7 @@
1313
ref="loadmoreRef"
1414
>
1515
<ul class="list-ul">
16-
<li class="list-li" v-for="(item, index) of list" :key="item">{{ language === 'Chinese' ? '测试数据' : 'This is data' }} {{ index + 1 }}</li>
16+
<li class="list-li" v-for="(item, index) of list" :key="item">{{ language === 'zh-CN' ? '测试数据' : 'This is data' }} {{ index + 1 }}</li>
1717
</ul>
1818
</vue-loadmore>
1919
</div>
@@ -22,7 +22,7 @@
2222

2323
<script>
2424
// import VueLoadmore from '../../packages/index';
25-
25+
import { locale } from '../../packages/index'
2626
export default {
2727
name: 'app',
2828
// components: {
@@ -34,7 +34,7 @@ export default {
3434
page: 1,
3535
finished: false,
3636
error: false,
37-
language: 'Chinese'
37+
language: 'en-US'
3838
};
3939
},
4040
mounted() {
@@ -80,7 +80,10 @@ export default {
8080
},
8181
8282
changeLanguage() {
83-
this.language = this.language === 'Chinese' ? 'English' : 'Chinese';
83+
this.language = this.language === 'zh-CN' ? 'en-US' : 'zh-CN';
84+
85+
// 切换语言
86+
locale.use(this.language);
8487
}
8588
},
8689
destroyed() {

example/src/main.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import Vue from 'vue'
2-
import VueLoadmore from '../../packages/index'
2+
import VueLoadmore, { locale } from '../../packages/index'
33
import App from './App.vue'
44

55
Vue.config.productionTip = false
6-
Vue.use(VueLoadmore)
6+
7+
Vue.use(VueLoadmore, {
8+
lang: 'en-US'
9+
})
10+
// Vue.use(VueLoadmore);
11+
// locale.use('en-US');
712

813
new Vue({
914
render: h => h(App),

lib/index.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.module.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuejs-loadmore",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A pull-down refresh and pull-up loadmore scroll component for Vue.js",
55
"entry": "packages/index.js",
66
"main": "lib/index.js",

packages/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import VueLoadmore from './vuejs-loadmore/index';
22
import './vuejs-loadmore/index.scss';
3+
import locale from './locale/index';
34

45
export default {
5-
install (Vue) {
6+
install (Vue, options = {}) {
67
Vue.component('vue-loadmore', VueLoadmore);
8+
9+
locale.use(options.lang);
710
}
811
};
12+
13+
export {
14+
locale
15+
}

packages/locale/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Vue from 'vue';
2+
import { getDeepVal } from '../utils/getDeepValByKey';
3+
import zhCN from './lang/zh-CN';
4+
import enUS from './lang/en-US';
5+
6+
const langLibrary = {
7+
'zh-CN': zhCN,
8+
'en-US': enUS,
9+
};
10+
11+
const proto = Vue.prototype;
12+
const { defineReactive } = Vue.util;
13+
// 将proto.lang定义成响应式数据
14+
defineReactive(proto, 'lang', 'zh-CN');
15+
16+
const getLangLibrary = () => langLibrary[proto.lang];
17+
18+
export default {
19+
// 获取当前语言库的值
20+
t(path) {
21+
const library = getLangLibrary();
22+
return getDeepVal(library, path);
23+
},
24+
25+
// 使用某个语言库(zhCN/enUS)
26+
use(lang) {
27+
if (langLibrary[lang]) {
28+
proto.lang = lang;
29+
}
30+
}
31+
};

packages/locale/lang/en-US.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
refresh: {
3+
pulling: 'Pull down to refresh',
4+
loosing: 'Loosing to refresh',
5+
refresh: 'Refreshing',
6+
success: 'Refresh success'
7+
},
8+
loadmore: {
9+
loading: 'Loading',
10+
finished: 'No more data',
11+
error: 'Request failed, click to reload'
12+
},
13+
}

0 commit comments

Comments
 (0)