diff --git a/README.md b/README.md index db39de972..0ad678f83 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/extend.js b/src/extend.js index 4cbaf1918..b9ee0d306 100644 --- a/src/extend.js +++ b/src/extend.js @@ -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 @@ -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 } diff --git a/test/specs/i18n.js b/test/specs/i18n.js index 0918b3aec..eee4ee2d3 100644 --- a/test/specs/i18n.js +++ b/test/specs/i18n.js @@ -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', () => {