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

Commit 4cac8b9

Browse files
committed
⭐ new: add globally locale checking
1 parent 65925eb commit 4cac8b9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

gitbook/api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@
126126
- **Usage:**
127127
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-).
128128

129+
### Vue.te(keypath, [lang])
130+
131+
- **Arguments:**
132+
- `{String} keypath`
133+
- `{String} [lang]`
134+
135+
- **Return:**
136+
Whether keypath exists, boolean value
137+
138+
- **Usage:**
139+
Check whether key path exists in global locale. If you specified `lang`, check the locale of `lang`.
140+
129141
## Constructor Options
130142

131143
### locales

src/extend.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ export default function (Vue) {
162162
return fetchChoice(Vue.t(key, ...args), choice)
163163
}
164164

165+
/**
166+
* Vue.te
167+
*
168+
* @param {String} key
169+
* @param {Array} ...args
170+
* @return {Boolean}
171+
*/
172+
173+
Vue.te = (key, ...args) => {
174+
const { lang } = parseArgs(...args)
175+
return exist(getAssetLocale(lang), key)
176+
}
177+
165178
/**
166179
* $t
167180
*

test/specs/i18n.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,24 @@ describe('i18n', () => {
270270
})
271271
})
272272

273+
describe('Vue.te', () => {
274+
describe('existing key', () => {
275+
it('should return true', () => {
276+
assert(Vue.te('message.hello') === true)
277+
})
278+
279+
it('should return true with language', () => {
280+
assert(Vue.te('message.hello', 'ja') === true)
281+
})
282+
})
283+
284+
describe('not existing key', () => {
285+
it('should return false', () => {
286+
assert(Vue.te('message.hallo') === false)
287+
})
288+
})
289+
})
290+
273291
describe('$t', () => {
274292
describe('en language locale', () => {
275293
it('should translate an english', () => {

0 commit comments

Comments
 (0)