Skip to content

Commit

Permalink
⭐ new(api): add 'mergeLocaleMessage' method
Browse files Browse the repository at this point in the history
Closes #131
  • Loading branch information
kazupon committed Apr 14, 2017
1 parent a1763e6 commit ef21621
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare interface I18n {
set silentTranslationWarn (silent: boolean): void,
getLocaleMessage (locale: Locale): LocaleMessage,
setLocaleMessage (locale: Locale, message: LocaleMessage): void,
mergeLocaleMessage (locale: Locale, message: LocaleMessage): void,
t (key: Path, ...values: any): TranslateResult,
tc (key: Path, choice?: number, ...values: any): TranslateResult,
te (key: Path, locale?: Locale): boolean
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export default class VueI18n {
setLocaleMessage (locale: Locale, message: LocaleMessage): void {
this._vm.messages[locale] = message
}

mergeLocaleMessage(locale: Locale, message: LocaleMessage): void {
this._vm.messages[locale] = Vue.util.extend(this.getLocaleMessage(locale), message)
}
}

VueI18n.install = install
Expand Down
62 changes: 41 additions & 21 deletions test/unit/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,47 @@ describe('message', () => {
i18n.setLocaleMessage('ja', orgJaLocaleMessage)
})

it('should be reload', done => {
const vm = new Vue({
i18n,
render (h) {
return h('p', { ref: 'text' }, [this.$t('message.hello')])
}
}).$mount(el)
describe('getLocaleMessage / setLocaleMessage', () => {
it('should be worked', done => {
const vm = new Vue({
i18n,
render (h) {
return h('p', { ref: 'text' }, [this.$t('message.hello')])
}
}).$mount(el)

const { text } = vm.$refs
nextTick(() => {
assert.equal(text.textContent, messages.en.message.hello)
// hot reload (set reactivity messages)
messages.en.message.hello = expectEnLocale
i18n.setLocaleMessage('en', messages.en)
}).then(() => {
assert.equal(text.textContent, expectEnLocale)
// upade locale
i18n.setLocaleMessage('ja', expectJaLocaleMessage)
i18n.locale = 'ja'
}).then(() => {
assert.equal(text.textContent, expectJaLocaleMessage.message.hello)
}).then(done)
const { text } = vm.$refs
nextTick(() => {
assert.equal(text.textContent, messages.en.message.hello)
// hot reload (set reactivity messages)
messages.en.message.hello = expectEnLocale
i18n.setLocaleMessage('en', messages.en)
}).then(() => {
assert.equal(text.textContent, expectEnLocale)
// upade locale
i18n.setLocaleMessage('ja', expectJaLocaleMessage)
i18n.locale = 'ja'
}).then(() => {
assert.equal(text.textContent, expectJaLocaleMessage.message.hello)
}).then(done)
})
})

describe('mergeLocaleMessage', () => {
it('should be merged', () => {
i18n = new VueI18n({
locale: 'en',
messages: {
en: {
foo: 'bar'
},
ja: {
foo: 'バー'
}
}
})
i18n.mergeLocaleMessage('en', { bar: 'foo' })
assert.deepEqual({ foo: 'bar', bar: 'foo' }, i18n.getLocaleMessage('en'))
})
})
})

0 comments on commit ef21621

Please sign in to comment.