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

Commit 0bc0a6b

Browse files
long-long-floatkazupon
authored andcommitted
⭐ new: locale checking (#98) by @long-long-float
* add $texist * add document for $texist
1 parent d4e942c commit 0bc0a6b

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

gitbook/api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,15 @@
167167
- **Usage:**
168168

169169
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).
170+
171+
### $texist(keypath, [arguments])
172+
173+
- **Arguments:**
174+
- `{String} keypath`
175+
- `{Array | Object} [arguments]`
176+
177+
- **Return:**
178+
Whether keypath exists (Boolean)
179+
180+
- **Usage:**
181+
Return whether key path exists in Boolean.

src/extend.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,29 @@ export default function (Vue) {
193193
return fetchChoice(this.$t(key, ...args), choice)
194194
}
195195

196+
/**
197+
* $texist
198+
*
199+
* @param {String} key
200+
* @param {Array} ...args
201+
* @return {Boolean}
202+
*
203+
*/
204+
205+
Vue.prototype.$texist = function (key, ...args) {
206+
if (!key) { return false }
207+
const { lang, fallback, params } = parseArgs(...args)
208+
209+
let locale
210+
if (this.$options.locales) {
211+
locale = bind(getComponentLocale, this)
212+
} else {
213+
locale = getAssetLocale
214+
}
215+
216+
const result = translate(locale, lang, fallback, key, params)
217+
return !isNil(result)
218+
}
219+
196220
return Vue
197221
}

test/specs/i18n.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,26 @@ describe('i18n', () => {
487487
})
488488
})
489489

490+
describe('$texist', () => {
491+
describe('existing key', () => {
492+
it('should return true', () => {
493+
const vm = new Vue()
494+
assert.equal(vm.$texist('message.hello'), true)
495+
})
496+
497+
it('should return true with language', () => {
498+
const vm = new Vue()
499+
assert.equal(vm.$texist('message.hello', 'ja'), true)
500+
})
501+
})
502+
503+
describe('not existing key', () => {
504+
it('should return false', () => {
505+
const vm = new Vue()
506+
assert.equal(vm.$texist('message.hallo'), false)
507+
})
508+
})
509+
})
490510

491511
describe('reactive translation', () => {
492512
let el

0 commit comments

Comments
 (0)