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

feat: add a new option fallbackRootWithEmptyString #1441

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ declare type I18nOptions = {
modifiers?: Modifiers,
root?: I18n, // for internal
fallbackRoot?: boolean,
fallbackRootWithEmptyString?: boolean,
formatFallbackMessages?: boolean,
sync?: boolean,
silentTranslationWarn?: boolean | RegExp,
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class VueI18n {
_root: any
_sync: boolean
_fallbackRoot: boolean
_fallbackRootWithEmptyString: boolean
_localeChainCache: { [key: string]: Array<Locale>; }
_missing: ?MissingHandler
_exist: Function
Expand Down Expand Up @@ -95,6 +96,9 @@ export default class VueI18n {
this._fallbackRoot = options.fallbackRoot === undefined
? true
: !!options.fallbackRoot
this._fallbackRootWithEmptyString = options.fallbackRootWithEmptyString === undefined
? true
: !!options.fallbackRootWithEmptyString
this._formatFallbackMessages = options.formatFallbackMessages === undefined
? false
: !!options.formatFallbackMessages
Expand Down Expand Up @@ -379,7 +383,7 @@ export default class VueI18n {
}

_isFallbackRoot (val: any): boolean {
return !val && !isNull(this._root) && this._fallbackRoot
return (this._fallbackRootWithEmptyString? !val : isNull(val)) && !isNull(this._root) && this._fallbackRoot
}

_isSilentFallbackWarn (key: Path): boolean {
Expand Down Expand Up @@ -461,7 +465,7 @@ export default class VueI18n {
// We are going to replace each of
// them with its translation
const matches: any = ret.match(linkKeyMatcher)

// eslint-disable-next-line no-autofix/prefer-const
for (let idx in matches) {
// ie compatible: filter custom array
Expand Down
127 changes: 127 additions & 0 deletions test/unit/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,131 @@ describe('component translation', () => {
vm.$destroy()
}).then(done)
})

it('fallbackRootWithEmptyString default to be true', done => {
const el = document.createElement('div')
let vm = new Vue({
i18n,
components: {
child: { // translation with component
i18n: {
locale: 'en-US',
sync: false,
messages: {
'en-US': {
who: 'child'
},
'ja-JP': {
who: '子',
}
},
},
components: {
'sub-child': { // translation with root
i18n: {
locale: 'ja-JP',
sync: false,
messages: {
'en-US': {
who: 'sub-child'
},
'ja-JP': {
who: ''
}
},
sharedMessages: { // shared messages for child1 component
'en-US': { foo: { bar: 'bar' } },
'ja-JP': { foo: { bar: 'バー' } }
}
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')])
])
}
}
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')]),
h('sub-child', { ref: 'sub-child' })
])
}
},
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')]),
h('child', { ref: 'child' }),
])
}
}).$mount(el)
Vue.nextTick().then(() => {
assert.strictEqual(vm.$refs.child.$refs['sub-child'].$refs.who.textContent, 'ルート')
}).then(done)
})

it('fallbackRootWithEmptyString should work when set to false', done => {
const el = document.createElement('div')
let vm = new Vue({
i18n,
components: {
child: { // translation with component
i18n: {
locale: 'en-US',
sync: false,
messages: {
'en-US': {
who: 'child'
},
'ja-JP': {
who: '子',
}
},
},
components: {
'sub-child': { // translation with root
i18n: {
locale: 'ja-JP',
sync: false,
fallbackRootWithEmptyString: false,
messages: {
'en-US': {
who: 'sub-child'
},
'ja-JP': {
who: ''
}
},
sharedMessages: { // shared messages for child1 component
'en-US': { foo: { bar: 'bar' } },
'ja-JP': { foo: { bar: 'バー' } }
}
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')])
])
}
}
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')]),
h('sub-child', { ref: 'sub-child' })
])
}
},
},
render (h) {
return h('div', {}, [
h('p', { ref: 'who' }, [this.$t('who')]),
h('child', { ref: 'child' }),
])
}
}).$mount(el)
Vue.nextTick().then(() => {
assert.strictEqual(vm.$refs.child.$refs['sub-child'].$refs.who.textContent, '')
}).then(done)
})
})