Skip to content

Commit

Permalink
improvement: vue-i18n-loader bridge mode (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Sep 30, 2021
1 parent 74629fc commit 09e4d79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ export default function defineMixin (bridge: boolean = false) {
: { // regulary
beforeCreate (): void {
const options: any = this.$options
options.i18n = options.i18n || (options.__i18n ? {} : null)
options.i18n = options.i18n || ((options.__i18nBridge || options.__i18n) ? {} : null)

if (options.i18n) {
if (options.i18n instanceof VueI18n) {
// init locale messages via custom blocks
if (options.__i18n) {
if ((options.__i18nBridge || options.__i18n)) {
try {
let localeMessages = options.i18n && options.i18n.messages ? options.i18n.messages : {}
options.__i18n.forEach(resource => {
const __i18n = options.__i18nBridge || options.__i18n
__i18n.forEach(resource => {
localeMessages = merge(localeMessages, JSON.parse(resource))
})
Object.keys(localeMessages).forEach((locale: Locale) => {
Expand Down Expand Up @@ -60,10 +61,11 @@ export default function defineMixin (bridge: boolean = false) {
}

// init locale messages via custom blocks
if (options.__i18n) {
if ((options.__i18nBridge || options.__i18n)) {
try {
let localeMessages = options.i18n && options.i18n.messages ? options.i18n.messages : {}
options.__i18n.forEach(resource => {
const __i18n = options.__i18nBridge || options.__i18n
__i18n.forEach(resource => {
localeMessages = merge(localeMessages, JSON.parse(resource))
})
options.i18n.messages = localeMessages
Expand Down Expand Up @@ -105,7 +107,7 @@ export default function defineMixin (bridge: boolean = false) {

beforeMount (): void {
const options: any = this.$options
options.i18n = options.i18n || (options.__i18n ? {} : null)
options.i18n = options.i18n || ((options.__i18nBridge || options.__i18n) ? {} : null)

if (options.i18n) {
if (options.i18n instanceof VueI18n) {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/custom_blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,35 @@ describe('custom blocks', () => {
}).then(done)
})
})

describe('bridge mode', () => {
it('should be translated', done => {
const el = document.createElement('div')
const vm = new Vue({
i18n,
components: {
child: {
__i18nBridge: [JSON.stringify({
en: { who: 'child' },
ja: { who: '子' }
})],
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')])
])
}
}
},
render (h) {
return h('div', {}, [h('child', { ref: 'child' })])
}
}).$mount(el)
Vue.nextTick().then(() => {
assert.strictEqual(vm.$refs.child.$refs.who.textContent, '子')
i18n.locale = 'en'
}).then(() => {
assert.strictEqual(vm.$refs.child.$refs.who.textContent, 'child')
}).then(done)
})
})
})

0 comments on commit 09e4d79

Please sign in to comment.