Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from lexxor/ignore-key-prefix-if-namespace
Browse files Browse the repository at this point in the history
Ignore keyPrefix property if namespace prefix used
  • Loading branch information
claudiocro authored Oct 17, 2017
2 parents d7f7dae + 783aed9 commit aee90dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 19 additions & 1 deletion test/unit/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
},
},
});
});
Expand All @@ -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');
});
});
});

0 comments on commit aee90dc

Please sign in to comment.