Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: vue-i18n-loader bridge mode #1392

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})
})
})