Skip to content

Commit

Permalink
improvement(extend): support translate empty string (#86) by @QingWei-Li
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li authored and kazupon committed Nov 19, 2016
1 parent a79020a commit 8e6d154
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function (Vue, langVM) {
* @param {Object | Function | Promise} definition
* @param {Function} cb
*/

Vue.locale = (id, definition, cb) => {
if (definition === undefined) { // gettter
return langVM.locales[id]
Expand Down
20 changes: 10 additions & 10 deletions src/extend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warn from './warn'
import Format from './format'
import Path from './path'

import { isNil } from './util'

/**
* extend
Expand Down Expand Up @@ -40,8 +40,9 @@ export default function (Vue) {
function interpolate (locale, key, args) {
if (!locale) { return null }

let val = getValue(locale, key) || locale[key]
if (!val) { return null }
let val = getValue(locale, key)
if (isNil(val)) val = locale[key]
if (isNil(val)) { return null }

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

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


function warnDefault (lang, key, vm) {
function warnDefault (lang, key, vm, result) {
if (!isNil(result)) { return result }
if (process.env.NODE_ENV !== 'production') {
warn('Cannot translate the value of keypath "' + key + '". '
+ 'Use the value of keypath as default')
Expand Down Expand Up @@ -134,8 +136,7 @@ export default function (Vue) {
Vue.t = (key, ...args) => {
if (!key) { return '' }
const { lang, fallback, params } = parseArgs(...args)
return translate(getAssetLocale, lang, fallback, key, params)
|| warnDefault(lang, key, null)
return warnDefault(lang, key, null, translate(getAssetLocale, lang, fallback, key, params))
}

/**
Expand Down Expand Up @@ -169,8 +170,7 @@ export default function (Vue) {
)
if (res) { return res }
}
return translate(getAssetLocale, lang, fallback, key, params)
|| warnDefault(lang, key, this)
return warnDefault(lang, key, this, translate(getAssetLocale, lang, fallback, key, params))
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isNil } from './util'

/**
* String format template
* - Inspired:
* - Inspired:
* https://github.com/Matt-Esch/string-template/index.js
*/

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

/**
* template
*
*
* @param {String} string
* @param {Array} ...args
* @return {String}
Expand All @@ -37,7 +39,7 @@ export default function (Vue) {
return i
} else {
result = hasOwn(args, i) ? args[i] : match
if (result === null || result === undefined) {
if (isNil(result)) {
return ''
}

Expand Down
3 changes: 3 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isNil (val) {
return val === null || val === undefined
}
2 changes: 2 additions & 0 deletions test/specs/fixture/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
message: {
hello: 'the world',
hoge: 'hoge',
empty: '',
format: {
named: 'Hello {name}, how are you?',
list: 'Hello {0}, how are you?'
Expand Down Expand Up @@ -41,6 +42,7 @@ export default {
message: {
hello: 'ザ・ワールド',
hoge: 'ほげ',
empty: '',
format: {
named: 'こんにちは {name}, ごきげんいかが?',
list: 'こんにちは {0}, ごきげんいかが?'
Expand Down
6 changes: 6 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('i18n', () => {
})
})

describe('empty string', () => {
it('should support empty string', () => {
assert.equal(Vue.t('message.empty'), locales.en.message.empty)
})
})

describe('linked translation', () => {
it('should translate simple link', () => {
assert.equal(Vue.t('message.link'), locales.en.message.hello)
Expand Down

0 comments on commit 8e6d154

Please sign in to comment.