Skip to content

Commit

Permalink
⭐ new(config): translation miss capturing configration
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
kazupon committed Sep 15, 2016
1 parent fdf9ab5 commit aca0ed6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getWatcher, getDep } from './observer'

let fallback // fallback lang
let missingHandler = null // missing handler

export default function (Vue, langVM, lang) {
const { bind } = Vue.util
Expand Down Expand Up @@ -35,4 +36,12 @@ export default function (Vue, langVM, lang) {
get: () => { return fallback },
set: val => { fallback = val }
})

// define Vue.config.missingHandler configration
Object.defineProperty(Vue.config, 'missingHandler', {
enumerable: true,
configurable: true,
get: () => { return missingHandler },
set: val => { missingHandler = val }
})
}
7 changes: 4 additions & 3 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ export default function (Vue) {
}


function warnDefault (key) {
function warnDefault (lang, key, vm) {
if (process.env.NODE_ENV !== 'production') {
warn('Cannot translate the value of keypath "' + key + '". '
+ 'Use the value of keypath as default')
}
Vue.config.missingHandler && Vue.config.missingHandler.apply(null, [lang, key, vm])
return key
}

Expand Down Expand Up @@ -117,7 +118,7 @@ export default function (Vue) {
if (!key) { return '' }
const { lang, fallback, params } = parseArgs(...args)
return translate(getAssetLocale, lang, fallback, key, params)
|| warnDefault(key)
|| warnDefault(lang, key, null)
}

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

/**
Expand Down
1 change: 1 addition & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Vue.use(plugin)
require('./i18n')
require('./asset')
require('./component')
require('./missing')
require('./issues')
44 changes: 44 additions & 0 deletions test/specs/missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert from 'power-assert'
import Vue from 'vue'


describe('missing', () => {
let lang
beforeEach(() => {
lang = Vue.config.lang
Vue.config.lang = 'en'
})

afterEach(done => {
Vue.config.lang = lang
Vue.config.missingHandler = null
Vue.nextTick(done)
})

describe('global', () => {
it('should be handled translate missing', done => {
Vue.config.missingHandler = (lang, key, vm) => {
assert.equal('en', lang)
assert.equal('foo.bar.buz', key)
assert(vm === null)
done()
}

Vue.t('foo.bar.buz')
})
})

describe('instance', () => {
it('should be handled translate missing', done => {
const vm = new Vue()
Vue.config.missingHandler = (lang, key, instance) => {
assert.equal('en', lang)
assert.equal('foo.bar.buz', key)
assert(vm === instance)
done()
}

vm.$t('foo.bar.buz')
})
})
})

0 comments on commit aca0ed6

Please sign in to comment.