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 #15 from pioniro/interpolate-by-place
Browse files Browse the repository at this point in the history
feat: interpolate by named place
  • Loading branch information
claudiocro authored Nov 28, 2017
2 parents ed9e7dc + c17d09b commit 31635c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ export default {

child = match;
} else {
child = children[parseInt(match, 10)];
const place = match.trim();
if (isNaN(parseFloat(place)) || !isFinite(place)) {
children.forEach((e) => {
if (!child && e.data.attrs && e.data.attrs.place && e.data.attrs.place === place) {
child = e;
}
});
} else {
child = children[parseInt(match, 10)];
}
}

memo.push(child);
Expand Down
17 changes: 17 additions & 0 deletions test/unit/interpolation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('interpolation', () => {
hello: '{{0}} Hello',
hello1: 'Hello {{0}} {{1}}',
hello2: 'Hello {{1}} {{0}}.',
hello3: 'Hello {{first}} {{second}}.',
},
},
},
Expand Down Expand Up @@ -66,6 +67,22 @@ describe('interpolation', () => {
expect(vm.$el.outerHTML).to.equal('<div>Hello <span>test</span> <p>First Hello</p>.</div>');
});

it('should interpolate components by place', async () => {
const el = document.createElement('div');
const vm = new Vue({
i18n: vueI18Next,
render(h) {
return h('i18next', { ref: 'text', props: { tag: 'div', path: 'hello3' } }, [
h('p', { domProps: { textContent: 'First Hello' }, attrs: { place: 'first' } }),
h('span', { domProps: { textContent: 'test' }, attrs: { place: 'second' } }),
]);
},
}).$mount(el);

await nextTick();
expect(vm.$el.outerHTML).to.equal('<div>Hello <p place="first">First Hello</p> <span place="second">test</span>.</div>');
});

it('should just return the children if i18next is not installed', async () => {
const el = document.createElement('div');
const vm = new Vue({
Expand Down

0 comments on commit 31635c8

Please sign in to comment.