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

Commit 68935e3

Browse files
committed
feat(i18n): add Vue.t function
Closes #17
1 parent 4f08656 commit 68935e3

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ Output the following:
152152

153153
Translate the locale of `keypath`. If you specified `lang`, translate the locale of `lang`. If you specified `keypath` of list / named formatting local, you must specify `arguments` too. For `arguments` more details see [Formatting](https://github.com/kazupon/vue-i18n#formatting).
154154

155+
## Vue.t(keypath, [lang], [arguments])
156+
- keypath: `String` **required**
157+
- lang: `String` **optional**
158+
- arguments: `Array | Object` **optional**
159+
160+
This is the same as the `$t` method. This is translate function for global.
161+
155162

156163
# Options
157164

src/extend.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ export default function (Vue, locales) {
2727
return value
2828
}
2929

30+
3031
/**
31-
* $t
32+
* Vue.t
3233
*
3334
* @param {String} key
3435
* @param {Array} ...args
3536
* @return {String}
3637
*/
3738

38-
Vue.prototype.$t = (key, ...args) => {
39+
Vue.t = (key, ...args) => {
3940
if (!key) { return '' }
4041

4142
let language = Vue.config.lang
@@ -57,5 +58,18 @@ export default function (Vue, locales) {
5758
return getVal(key, language, args)
5859
}
5960

61+
62+
/**
63+
* $t
64+
*
65+
* @param {String} key
66+
* @param {Array} ...args
67+
* @return {String}
68+
*/
69+
70+
Vue.prototype.$t = (key, ...args) => {
71+
return Vue.t(key, ...args)
72+
}
73+
6074
return Vue
6175
}

test/specs/i18n.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,93 @@ describe('i18n', () => {
3737
})
3838

3939

40+
describe('Vue.t', () => {
41+
describe('en language locale', () => {
42+
it('should translate an english', () => {
43+
assert(Vue.t('message.hello') === locales.en.message.hello)
44+
})
45+
})
46+
47+
describe('ja language locale', () => {
48+
it('should translate a japanese', () => {
49+
assert(Vue.t('message.hello', 'ja') === locales.ja.message.hello)
50+
})
51+
})
52+
53+
describe('key argument', () => {
54+
describe('not specify', () => {
55+
it('should return empty string', () => {
56+
assert(Vue.t() === '')
57+
})
58+
})
59+
60+
describe('empty string', () => {
61+
it('should return empty string', () => {
62+
assert(Vue.t('') === '')
63+
})
64+
})
65+
66+
describe('not regist key', () => {
67+
it('should return key string', () => {
68+
assert(Vue.t('foo.bar') === 'foo.bar')
69+
})
70+
})
71+
72+
describe('sentence fragment', () => {
73+
it('should translate fragment', () => {
74+
assert(Vue.t('hello world') === 'Hello World')
75+
})
76+
77+
it('should return replaced string if available', () => {
78+
assert(
79+
Vue.t('Hello {0}', ['kazupon']),
80+
'Hello kazupon'
81+
)
82+
})
83+
84+
it('should return key if unavailable', () => {
85+
assert(Vue.t('Hello') === 'Hello')
86+
})
87+
})
88+
})
89+
90+
describe('format arguments', () => {
91+
context('named', () => {
92+
it('should return replaced string', () => {
93+
assert(
94+
Vue.t('message.format.named', { name: 'kazupon' }),
95+
'Hello kazupon, how are you?'
96+
)
97+
})
98+
})
99+
100+
context('list', () => {
101+
it('should return replaced string', () => {
102+
assert(
103+
Vue.t('message.format.list', ['kazupon']),
104+
'Hello kazupon, how are you?'
105+
)
106+
})
107+
})
108+
})
109+
110+
describe('language argument', () => {
111+
it('should return empty string', () => {
112+
assert(Vue.t('message.hello', 'ja') === locales.ja.message.hello)
113+
})
114+
})
115+
116+
describe('format & language arguments', () => {
117+
it('should return replaced string', () => {
118+
assert(
119+
Vue.t('message.format.list', 'ja', ['kazupon']),
120+
'こんにちは kazupon, ごきげんいかが?'
121+
)
122+
})
123+
})
124+
})
125+
126+
40127
describe('$t', () => {
41128
describe('en language locale', () => {
42129
it('should translate an english', () => {

0 commit comments

Comments
 (0)