Skip to content

Commit

Permalink
⭐ new: add globally locale checking
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 17, 2016
1 parent 65925eb commit 4cac8b9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gitbook/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@
- **Usage:**
This is the same as the `$tc` method. This is translate pluralization function for global locale only. more detail see [$tc](#tc-keypath-choice-arguments-).

### Vue.te(keypath, [lang])

- **Arguments:**
- `{String} keypath`
- `{String} [lang]`

- **Return:**
Whether keypath exists, boolean value

- **Usage:**
Check whether key path exists in global locale. If you specified `lang`, check the locale of `lang`.

## Constructor Options

### locales
Expand Down
13 changes: 13 additions & 0 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ export default function (Vue) {
return fetchChoice(Vue.t(key, ...args), choice)
}

/**
* Vue.te
*
* @param {String} key
* @param {Array} ...args
* @return {Boolean}
*/

Vue.te = (key, ...args) => {
const { lang } = parseArgs(...args)
return exist(getAssetLocale(lang), key)
}

/**
* $t
*
Expand Down
18 changes: 18 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ describe('i18n', () => {
})
})

describe('Vue.te', () => {
describe('existing key', () => {
it('should return true', () => {
assert(Vue.te('message.hello') === true)
})

it('should return true with language', () => {
assert(Vue.te('message.hello', 'ja') === true)
})
})

describe('not existing key', () => {
it('should return false', () => {
assert(Vue.te('message.hallo') === false)
})
})
})

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

0 comments on commit 4cac8b9

Please sign in to comment.