diff --git a/src/install.js b/src/install.js index 451f062..79fd69f 100644 --- a/src/install.js +++ b/src/install.js @@ -9,8 +9,8 @@ export function install(_Vue) { Vue = _Vue; - const getByKey = i18nOptions => (key) => { - if (i18nOptions && i18nOptions.keyPrefix) { + const getByKey = (i18nOptions, i18nextOptions) => (key) => { + if (i18nOptions && i18nOptions.keyPrefix && !key.includes(i18nextOptions.nsSeparator)) { return `${i18nOptions.keyPrefix}.${key}`; } return key; @@ -19,7 +19,7 @@ export function install(_Vue) { Vue.mixin({ computed: { $t() { - const getKey = getByKey(this._i18nOptions); + const getKey = getByKey(this._i18nOptions, this.$i18n.i18next.options); if (this._i18nOptions && this._i18nOptions.namespaces) { const { lng, namespaces } = this._i18nOptions; diff --git a/test/unit/render.test.js b/test/unit/render.test.js index 6e6dc5e..f77c168 100644 --- a/test/unit/render.test.js +++ b/test/unit/render.test.js @@ -116,7 +116,10 @@ describe('$t loaded languages', () => { i18next.init({ lng: 'en', resources: { - en: { translation: { messages: { hello: 'Hello' } } }, + en: { + translation: { messages: { hello: 'Hello' }, }, + common: { goodbye: 'Goodbye' } + }, }, }); }); @@ -135,5 +138,20 @@ describe('$t loaded languages', () => { await nextTick(); expect(text.textContent).to.equal('Hello'); }); + + it('should ignore keyPrefix property if namespace prefix used', async () => { + const el = document.createElement('div'); + const vm = new Vue({ + i18n: vueI18Next, + i18nOptions: { keyPrefix: 'messages' }, + render(h) { + return h('p', { ref: 'text' }, [this.$t('common:goodbye')]); + }, + }).$mount(el); + + const { text } = vm.$refs; + await nextTick(); + expect(text.textContent).to.equal('Goodbye'); + }); }); });