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

Commit

Permalink
fix(i18nOptions): inherit i18nOptions form parent if none defined
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiocro committed May 28, 2017
1 parent c9d7b08 commit 839e122
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ export function install(_Vue) {
Vue.mixin({
computed: {
$t() {
if (this.$options.i18nOptions) {
const { lng = null } = this.$options.i18nOptions;
let { namespaces } = this.$options.i18nOptions;
namespaces = namespaces || this.$i18n.i18next.options.defaultNS;
if (typeof namespaces === 'string') namespaces = [namespaces];
this.$i18n.i18next.loadNamespaces(namespaces);

if (this._i18nOptions) {
const { lng, namespaces } = this._i18nOptions;
const fixedT = this.$i18n.i18next.getFixedT(lng, namespaces);
return (key, options) => fixedT(key, options, this.$i18n.i18nLoadedAt);
}
Expand All @@ -33,6 +28,18 @@ export function install(_Vue) {
} else if (options.parent && options.parent.$i18n) {
this.$i18n = options.parent.$i18n;
}

if (this.$i18n && this.$options.i18nOptions) {
const { lng = null } = this.$options.i18nOptions;
let { namespaces } = this.$options.i18nOptions;
namespaces = namespaces || this.$i18n.i18next.options.defaultNS;
if (typeof namespaces === 'string') namespaces = [namespaces];
this.$i18n.i18next.loadNamespaces(namespaces);

this._i18nOptions = { lng, namespaces };
} else if (this.$i18n && options.parent && options.parent._i18nOptions) {
this._i18nOptions = options.parent._i18nOptions;
}
},
});

Expand Down
36 changes: 36 additions & 0 deletions test/unit/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,39 @@ describe('Components namespaces', () => {
]);
},
},
child2: {
i18nOptions: { namespaces: 'common' },
components: {
'sub-child1': {
i18nOptions: { lng: 'de' },
render(h) {
return h('div', { }, [
h('p', { ref: 'hello' }, [this.$t('hello')]),
]);
},
},
'sub-child2': {
render(h) {
return h('div', { }, [
h('p', { ref: 'key1' }, [this.$t('key1', { name: 'Waldo' })]),
]);
},
},
},
render(h) {
return h('div', {}, [
h('p', { ref: 'hello' }, [this.$t('hello')]),
h('sub-child1', { ref: 'sub-child1' }),
h('sub-child2', { ref: 'sub-child2' }),
]);
},
},
},
render(h) {
return h('div', {}, [
h('p', { ref: 'hello' }, [this.$t('hello')]),
h('child1', { ref: 'child1' }),
h('child2', { ref: 'child2' }),
]);
},
}).$mount(el);
Expand All @@ -74,12 +102,19 @@ describe('Components namespaces', () => {
const deHello = vm.$refs.child1.$refs['sub-child1'].$refs.hello;
const commonHello = vm.$refs.child1.$refs['sub-child2'].$refs.key1;

// const child2Hello = vm.$refs.child2;
const child2Sub1 = vm.$refs.child2.$refs['sub-child1'].$refs.hello;
const child2Sub2 = vm.$refs.child2.$refs['sub-child2'].$refs.key1;

backend.flush();
await nextTick();

expect(hello.textContent).to.equal('Hello');
expect(deHello.textContent).to.equal('Hallo');
expect(commonHello.textContent).to.equal('Hello Waldo');

expect(child2Sub1.textContent).to.equal('Hallo');
expect(child2Sub2.textContent).to.equal('Hello Waldo');
});
});

Expand Down Expand Up @@ -113,6 +148,7 @@ describe('Components with backend', () => {

it('should render sub components correctly', async () => {
const root = vm.$refs.hello;
expect(root.textContent).to.equal('key1');
backend.flush();
await nextTick();

Expand Down

0 comments on commit 839e122

Please sign in to comment.