Skip to content

Commit

Permalink
⭐ new: locale checking (#98) by @long-long-float
Browse files Browse the repository at this point in the history
* add $texist

* add document for $texist
  • Loading branch information
long-long-float authored and kazupon committed Dec 16, 2016
1 parent d4e942c commit 0bc0a6b
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -167,3 +167,15 @@
- **Usage:**

Translate the locale of `keypath` with pluralization. Translate in preferentially component locale than global locale. If not specified component locale, translate with global locale. If you will specify String value to `arguments`, translate the locale of value. If you wll specify Array or Object value to `arguments`, you must specify with `arguments` of [$t](#tkeypath-lang-arguments).

### $texist(keypath, [arguments])

- **Arguments:**
- `{String} keypath`
- `{Array | Object} [arguments]`

- **Return:**
Whether keypath exists (Boolean)

- **Usage:**
Return whether key path exists in Boolean.
24 changes: 24 additions & 0 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,29 @@ export default function (Vue) {
return fetchChoice(this.$t(key, ...args), choice)
}

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

Vue.prototype.$texist = function (key, ...args) {
if (!key) { return false }
const { lang, fallback, params } = parseArgs(...args)

let locale
if (this.$options.locales) {
locale = bind(getComponentLocale, this)
} else {
locale = getAssetLocale
}

const result = translate(locale, lang, fallback, key, params)
return !isNil(result)
}

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

describe('$texist', () => {
describe('existing key', () => {
it('should return true', () => {
const vm = new Vue()
assert.equal(vm.$texist('message.hello'), true)
})

it('should return true with language', () => {
const vm = new Vue()
assert.equal(vm.$texist('message.hello', 'ja'), true)
})
})

describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue()
assert.equal(vm.$texist('message.hallo'), false)
})
})
})

describe('reactive translation', () => {
let el
Expand Down

0 comments on commit 0bc0a6b

Please sign in to comment.