Skip to content

Commit

Permalink
feat(i18n): add Vue.t function
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
kazupon committed Feb 6, 2016
1 parent 4f08656 commit 68935e3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ Output the following:

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).

## Vue.t(keypath, [lang], [arguments])
- keypath: `String` **required**
- lang: `String` **optional**
- arguments: `Array | Object` **optional**

This is the same as the `$t` method. This is translate function for global.


# Options

Expand Down
18 changes: 16 additions & 2 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ export default function (Vue, locales) {
return value
}


/**
* $t
* Vue.t
*
* @param {String} key
* @param {Array} ...args
* @return {String}
*/

Vue.prototype.$t = (key, ...args) => {
Vue.t = (key, ...args) => {
if (!key) { return '' }

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


/**
* $t
*
* @param {String} key
* @param {Array} ...args
* @return {String}
*/

Vue.prototype.$t = (key, ...args) => {
return Vue.t(key, ...args)
}

return Vue
}
87 changes: 87 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,93 @@ describe('i18n', () => {
})


describe('Vue.t', () => {
describe('en language locale', () => {
it('should translate an english', () => {
assert(Vue.t('message.hello') === locales.en.message.hello)
})
})

describe('ja language locale', () => {
it('should translate a japanese', () => {
assert(Vue.t('message.hello', 'ja') === locales.ja.message.hello)
})
})

describe('key argument', () => {
describe('not specify', () => {
it('should return empty string', () => {
assert(Vue.t() === '')
})
})

describe('empty string', () => {
it('should return empty string', () => {
assert(Vue.t('') === '')
})
})

describe('not regist key', () => {
it('should return key string', () => {
assert(Vue.t('foo.bar') === 'foo.bar')
})
})

describe('sentence fragment', () => {
it('should translate fragment', () => {
assert(Vue.t('hello world') === 'Hello World')
})

it('should return replaced string if available', () => {
assert(
Vue.t('Hello {0}', ['kazupon']),
'Hello kazupon'
)
})

it('should return key if unavailable', () => {
assert(Vue.t('Hello') === 'Hello')
})
})
})

describe('format arguments', () => {
context('named', () => {
it('should return replaced string', () => {
assert(
Vue.t('message.format.named', { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})

context('list', () => {
it('should return replaced string', () => {
assert(
Vue.t('message.format.list', ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})

describe('language argument', () => {
it('should return empty string', () => {
assert(Vue.t('message.hello', 'ja') === locales.ja.message.hello)
})
})

describe('format & language arguments', () => {
it('should return replaced string', () => {
assert(
Vue.t('message.format.list', 'ja', ['kazupon']),
'こんにちは kazupon, ごきげんいかが?'
)
})
})
})


describe('$t', () => {
describe('en language locale', () => {
it('should translate an english', () => {
Expand Down

0 comments on commit 68935e3

Please sign in to comment.