Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 12fe695

Browse files
committed
⭐ new: component locales
Closes #29
1 parent d87b59b commit 12fe695

3 files changed

Lines changed: 86 additions & 28 deletions

File tree

src/extend.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,33 @@ import { getValue } from './path'
1212
export default function (Vue) {
1313
const { isArray, isObject } = Vue.util
1414

15-
function getVal (key, lang, args) {
16-
let value = key
17-
try {
18-
let locale = Vue.locale(lang)
19-
let val = getValue(locale, key) || locale[key]
20-
value = (args ? format(val, args) : val) || key
21-
} catch (e) {
22-
value = key
15+
function parseArgs (...args) {
16+
let lang = Vue.config.lang
17+
if (args.length === 1) {
18+
if (isObject(args[0]) || isArray(args[0])) {
19+
args = args[0]
20+
} else if (typeof args[0] === 'string') {
21+
lang = args[0]
22+
}
23+
} else if (args.length === 2) {
24+
if (typeof args[0] === 'string') {
25+
lang = args[0]
26+
}
27+
if (isObject(args[1]) || isArray(args[1])) {
28+
args = args[1]
29+
}
2330
}
24-
return value
31+
32+
return { lang, params: args }
33+
}
34+
35+
function translate (locale, key, args) {
36+
if (!locale) { return null }
37+
38+
let val = getValue(locale, key) || locale[key]
39+
if (!val) { return null }
40+
41+
return args ? format(val, args) : val
2542
}
2643

2744

@@ -36,23 +53,8 @@ export default function (Vue) {
3653
Vue.t = (key, ...args) => {
3754
if (!key) { return '' }
3855

39-
let language = Vue.config.lang
40-
if (args.length === 1) {
41-
if (isObject(args[0]) || isArray(args[0])) {
42-
args = args[0]
43-
} else if (typeof args[0] === 'string') {
44-
language = args[0]
45-
}
46-
} else if (args.length === 2) {
47-
if (typeof args[0] === 'string') {
48-
language = args[0]
49-
}
50-
if (isObject(args[1]) || isArray(args[1])) {
51-
args = args[1]
52-
}
53-
}
54-
55-
return getVal(key, language, args)
56+
const { lang, params } = parseArgs(...args)
57+
return translate(Vue.locale(lang), key, params) || key
5658
}
5759

5860

@@ -64,8 +66,13 @@ export default function (Vue) {
6466
* @return {String}
6567
*/
6668

67-
Vue.prototype.$t = (key, ...args) => {
68-
return Vue.t(key, ...args)
69+
Vue.prototype.$t = function (key, ...args) {
70+
if (!key) { return '' }
71+
72+
const { lang, params } = parseArgs(...args)
73+
return translate(this.$options.locales && this.$options.locales[lang], key, params)
74+
|| translate(Vue.locale(lang), key, params)
75+
|| key
6976
}
7077

7178
return Vue

test/specs/component.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import assert from 'power-assert'
2+
import Vue from 'vue'
3+
import locales from './fixture/locales'
4+
5+
6+
describe('component locales', () => {
7+
before((done) => {
8+
Object.keys(locales).forEach((lang) => {
9+
Vue.locale(lang, locales[lang])
10+
})
11+
Vue.config.lang = 'en'
12+
Vue.nextTick(done)
13+
})
14+
15+
let vm
16+
beforeEach((done) => {
17+
vm = new Vue({
18+
el: document.createElement('div'),
19+
template: '<div><component1></component1></div>',
20+
components: {
21+
component1: {
22+
locales: {
23+
en: {
24+
foo: {
25+
bar: {
26+
buz: 'hello world'
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
})
34+
vm.$nextTick(done)
35+
})
36+
37+
describe('local', () => {
38+
it('should be translated', () => {
39+
const comp1 = vm.$children[0] // component1
40+
assert(comp1.$t('foo.bar.buz') === 'hello world')
41+
})
42+
})
43+
44+
describe('global', () => {
45+
it('should be translated', () => {
46+
const comp1 = vm.$children[0] // component1
47+
assert(comp1.$t('message.hello') === 'the world')
48+
})
49+
})
50+
})

test/specs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Vue.use(plugin)
1010

1111
require('./i18n')
1212
require('./asset')
13+
require('./component')

0 commit comments

Comments
 (0)