diff --git a/gitbook/api.md b/gitbook/api.md index 0f7249bc7..f220cdd67 100644 --- a/gitbook/api.md +++ b/gitbook/api.md @@ -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 diff --git a/src/extend.js b/src/extend.js index c8459d13d..1df036676 100644 --- a/src/extend.js +++ b/src/extend.js @@ -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 * diff --git a/test/specs/i18n.js b/test/specs/i18n.js index 0496b20b3..299153ba9 100644 --- a/test/specs/i18n.js +++ b/test/specs/i18n.js @@ -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', () => {