Skip to content

Commit

Permalink
⭐ new(mixin): shared locale messages feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jul 9, 2019
1 parent a17660f commit 82543de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default {
}
}

const { sharedMessages } = options.i18n
if (sharedMessages && isPlainObject(sharedMessages)) {
options.i18n.messages = merge(options.i18n.messages, sharedMessages)
}

this._i18n = new VueI18n(options.i18n)
this._i18nWatcher = this._i18n.watchI18nData()

Expand Down
8 changes: 8 additions & 0 deletions test/unit/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ describe('component translation', () => {
messages: {
'en-US': { who: 'child1' },
'ja-JP': { who: '子1' }
},
sharedMessages: { // shared messages for child1 component
'en-US': { foo: { bar: 'bar' } },
'ja-JP': { foo: { bar: 'バー' } }
}
},
components: {
Expand All @@ -52,6 +56,7 @@ describe('component translation', () => {
h('p', { ref: 'fallback' }, [this.$t('fallback')]),
h('p', { ref: 'datetime' }, [this.$d(dt, 'short')]),
h('p', { ref: 'number' }, [this.$n(money, 'currency')]),
h('p', { ref: 'shared' }, [this.$t('foo.bar')]),
h('sub-child1', { ref: 'sub-child1' })
])
}
Expand Down Expand Up @@ -97,6 +102,7 @@ describe('component translation', () => {
const child1Fallback = vm.$refs.child1.$refs.fallback
const child1DateTime = vm.$refs.child1.$refs.datetime
const child1Number = vm.$refs.child1.$refs.number
const child1Shared = vm.$refs.child1.$refs.shared
const child2 = vm.$refs.child2.$refs.who
const subChild1 = vm.$refs.child1.$refs['sub-child1'].$refs.who
const subChild2 = vm.$refs.child2.$refs['sub-child2'].$refs.who
Expand All @@ -107,6 +113,8 @@ describe('component translation', () => {
// NOTE: avoid webkit(phatomjs/safari) & Intl polyfill wired localization...
isChrome && assert.strictEqual(child1DateTime.textContent, '12/19/2012, 10:00 PM')
isChrome && assert.strictEqual(child1Number.textContent, '$101.00')
assert.strictEqual(child1Shared.textContent, 'bar')

assert.strictEqual(child2.textContent, 'ルート')
assert.strictEqual(subChild1.textContent, 'ルート')
assert.strictEqual(subChild2.textContent, 'サブの子2')
Expand Down

0 comments on commit 82543de

Please sign in to comment.