Skip to content

Commit

Permalink
⭐ new(fallback): add fallback translation feature
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
kazupon committed Aug 12, 2016
1 parent 7ba0a99 commit 1d1f0f2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 34 deletions.
13 changes: 12 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getWatcher, getDep } from './observer'

export default function (Vue, langVM) {
let fallback // fallback lang

export default function (Vue, langVM, lang) {
const { bind } = Vue.util
const Watcher = getWatcher(langVM)
const Dep = getDep(langVM)
Expand Down Expand Up @@ -28,4 +30,13 @@ export default function (Vue, langVM) {
get: makeComputedGetter(() => { return langVM.lang }, langVM),
set: bind(val => { langVM.lang = val }, langVM)
})

// define Vue.config.fallbackLang configration
fallback = lang
Object.defineProperty(Vue.config, 'fallbackLang', {
enumerable: true,
configurable: true,
get: () => { return fallback },
set: val => { fallback = val }
})
}
14 changes: 10 additions & 4 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default function (Vue) {

function parseArgs (...args) {
let lang = Vue.config.lang
let fallback = Vue.config.fallbackLang

if (args.length === 1) {
if (isObject(args[0]) || Array.isArray(args[0])) {
args = args[0]
Expand All @@ -32,7 +34,7 @@ export default function (Vue) {
}
}

return { lang, params: args }
return { lang, fallback, params: args }
}

function translate (locale, key, args) {
Expand Down Expand Up @@ -64,8 +66,10 @@ export default function (Vue) {
Vue.t = (key, ...args) => {
if (!key) { return '' }

const { lang, params } = parseArgs(...args)
return translate(Vue.locale(lang), key, params) || warnDefault(key)
const { lang, fallback, params } = parseArgs(...args)
return translate(Vue.locale(lang), key, params)
|| translate(Vue.locale(fallback), key, params)
|| warnDefault(key)
}


Expand All @@ -80,9 +84,11 @@ export default function (Vue) {
Vue.prototype.$t = function (key, ...args) {
if (!key) { return '' }

const { lang, params } = parseArgs(...args)
const { lang, fallback, params } = parseArgs(...args)
return translate(this.$options.locales && this.$options.locales[lang], key, params)
|| translate(this.$options.locales && this.$options.locales[fallback], key, params)
|| translate(Vue.locale(lang), key, params)
|| translate(Vue.locale(fallback), key, params)
|| warnDefault(key)
}

Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ function plugin (Vue, opts = {}) {
const lang = 'en'

setupLangVM(Vue, lang)

Asset(Vue)

Override(Vue, langVM)
Config(Vue, langVM)
Config(Vue, langVM, lang)
Extend(Vue)
}

Expand Down
12 changes: 11 additions & 1 deletion test/specs/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ describe('component locales', () => {
bar: {
buz: 'hello world'
}
}
},
fallback: 'this is fallback on component'
},
ja: {
}
}
}
Expand Down Expand Up @@ -67,4 +70,11 @@ describe('component locales', () => {
assert.equal(comp1.$t('message.hello'), 'the world')
})
})

describe('fallback', () => {
it('should be work', () => {
const comp1 = vm.$children[0] // component1
assert.equal(comp1.$t('fallback', 'ja'), 'this is fallback on component')
})
})
})
6 changes: 4 additions & 2 deletions test/specs/fixture/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default {
format: {
named: 'Hello {name}, how are you?',
list: 'Hello {0}, how are you?'
}
},
fallback: 'this is fallback'
},
'hello world': 'Hello World',
'Hello {0}': 'Hello {0}',
Expand All @@ -20,7 +21,8 @@ export default {
format: {
named: 'こんにちは {name}, ごきげんいかが?',
list: 'こんにちは {0}, ごきげんいかが?'
}
},
fallback1: 'これはフォールバック'
}
}
}
91 changes: 68 additions & 23 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ describe('i18n', () => {
)
})
})

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


Expand Down Expand Up @@ -197,6 +206,16 @@ describe('i18n', () => {
)
})
})

describe('fallback', () => {
it('should return fallback string', () => {
const vm = new Vue()
assert.equal(
vm.$t('message.fallback', 'ja'),
locales.en.message.fallback
)
})
})
})


Expand Down Expand Up @@ -282,36 +301,62 @@ describe('i18n', () => {
})


describe('global lang config', () => {
let vm
beforeEach(done => {
vm = new Vue()
vm.$nextTick(done)
})

afterEach(done => {
vm.$destroy()
vm = null
Vue.nextTick(done)
})
describe('extend Vue.config', () => {
describe('lang', () => {
let vm
beforeEach(done => {
vm = new Vue()
vm.$nextTick(done)
})

context('ja', () => {
it('should translate with japanese', done => {
Vue.config.lang = 'ja'
Vue.nextTick(() => {
assert.equal(vm.$t('message.hello'), locales.ja.message.hello)
done()
})
afterEach(done => {
vm.$destroy()
vm = null
Vue.nextTick(done)
})

context('en', () => {
it('should translate with english', done => {
Vue.config.lang = 'en'
context('ja', () => {
it('should translate with japanese', done => {
Vue.config.lang = 'ja'
Vue.nextTick(() => {
assert.equal(vm.$t('message.hello'), locales.en.message.hello)
assert.equal(vm.$t('message.hello'), locales.ja.message.hello)
done()
})
})

context('en', () => {
it('should translate with english', done => {
Vue.config.lang = 'en'
Vue.nextTick(() => {
assert.equal(vm.$t('message.hello'), locales.en.message.hello)
done()
})
})
})
})
})

describe('fallbackLang', () => {
let orgLang, orgFallbackLang
beforeEach(done => {
orgLang = Vue.config.lang
orgFallbackLang = Vue.config.fallbackLang
Vue.nextTick(done)
})

afterEach(done => {
Vue.config.fallbackLang = orgFallbackLang
Vue.config.lang = orgLang
Vue.nextTick(done)
})

it('should be changed', done => {
Vue.config.lang = 'ja'
Vue.nextTick(() => {
Vue.config.fallbackLang = 'ja'
assert.equal(Vue.t('message.fallback1'), locales.ja.message.fallback1)
done()
})
})
})
})
Expand Down

0 comments on commit 1d1f0f2

Please sign in to comment.