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

Commit 8e6d154

Browse files
QingWei-Likazupon
authored andcommitted
improvement(extend): support translate empty string (#86) by @QingWei-Li
1 parent a79020a commit 8e6d154

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

src/asset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function (Vue, langVM) {
88
* @param {Object | Function | Promise} definition
99
* @param {Function} cb
1010
*/
11-
11+
1212
Vue.locale = (id, definition, cb) => {
1313
if (definition === undefined) { // gettter
1414
return langVM.locales[id]

src/extend.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import warn from './warn'
22
import Format from './format'
33
import Path from './path'
4-
4+
import { isNil } from './util'
55

66
/**
77
* extend
@@ -40,8 +40,9 @@ export default function (Vue) {
4040
function interpolate (locale, key, args) {
4141
if (!locale) { return null }
4242

43-
let val = getValue(locale, key) || locale[key]
44-
if (!val) { return null }
43+
let val = getValue(locale, key)
44+
if (isNil(val)) val = locale[key]
45+
if (isNil(val)) { return null }
4546

4647
// Check for the existance of links within the translated string
4748
if (val.indexOf('@:') >= 0) {
@@ -70,10 +71,10 @@ export default function (Vue) {
7071
function translate (getter, lang, fallback, key, params) {
7172
let res = null
7273
res = interpolate(getter(lang), key, params)
73-
if (res) { return res }
74+
if (!isNil(res)) { return res }
7475

7576
res = interpolate(getter(fallback), key, params)
76-
if (res) {
77+
if (!isNil(res)) {
7778
if (process.env.NODE_ENV !== 'production') {
7879
warn('Fall back to translate the keypath "' + key + '" with "'
7980
+ fallback + '" language.')
@@ -85,7 +86,8 @@ export default function (Vue) {
8586
}
8687

8788

88-
function warnDefault (lang, key, vm) {
89+
function warnDefault (lang, key, vm, result) {
90+
if (!isNil(result)) { return result }
8991
if (process.env.NODE_ENV !== 'production') {
9092
warn('Cannot translate the value of keypath "' + key + '". '
9193
+ 'Use the value of keypath as default')
@@ -134,8 +136,7 @@ export default function (Vue) {
134136
Vue.t = (key, ...args) => {
135137
if (!key) { return '' }
136138
const { lang, fallback, params } = parseArgs(...args)
137-
return translate(getAssetLocale, lang, fallback, key, params)
138-
|| warnDefault(lang, key, null)
139+
return warnDefault(lang, key, null, translate(getAssetLocale, lang, fallback, key, params))
139140
}
140141

141142
/**
@@ -169,8 +170,7 @@ export default function (Vue) {
169170
)
170171
if (res) { return res }
171172
}
172-
return translate(getAssetLocale, lang, fallback, key, params)
173-
|| warnDefault(lang, key, this)
173+
return warnDefault(lang, key, this, translate(getAssetLocale, lang, fallback, key, params))
174174
}
175175

176176
/**

src/format.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { isNil } from './util'
2+
13
/**
24
* String format template
3-
* - Inspired:
5+
* - Inspired:
46
* https://github.com/Matt-Esch/string-template/index.js
57
*/
68

@@ -12,7 +14,7 @@ export default function (Vue) {
1214

1315
/**
1416
* template
15-
*
17+
*
1618
* @param {String} string
1719
* @param {Array} ...args
1820
* @return {String}
@@ -37,7 +39,7 @@ export default function (Vue) {
3739
return i
3840
} else {
3941
result = hasOwn(args, i) ? args[i] : match
40-
if (result === null || result === undefined) {
42+
if (isNil(result)) {
4143
return ''
4244
}
4345

src/util.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isNil (val) {
2+
return val === null || val === undefined
3+
}

test/specs/fixture/locales.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export default {
33
message: {
44
hello: 'the world',
55
hoge: 'hoge',
6+
empty: '',
67
format: {
78
named: 'Hello {name}, how are you?',
89
list: 'Hello {0}, how are you?'
@@ -41,6 +42,7 @@ export default {
4142
message: {
4243
hello: 'ザ・ワールド',
4344
hoge: 'ほげ',
45+
empty: '',
4446
format: {
4547
named: 'こんにちは {name}, ごきげんいかが?',
4648
list: 'こんにちは {0}, ごきげんいかが?'

test/specs/i18n.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ describe('i18n', () => {
2121
})
2222
})
2323

24+
describe('empty string', () => {
25+
it('should support empty string', () => {
26+
assert.equal(Vue.t('message.empty'), locales.en.message.empty)
27+
})
28+
})
29+
2430
describe('linked translation', () => {
2531
it('should translate simple link', () => {
2632
assert.equal(Vue.t('message.link'), locales.en.message.hello)

0 commit comments

Comments
 (0)