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

Commit 4245b85

Browse files
committed
feat(interpolation): i18next component supports options params
1 parent bc0bdf1 commit 4245b85

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,64 @@ Vue.component("app", {
158158
</i18next>
159159
</div>`
160160
});
161+
162+
Vue.component("app", {
163+
template: `
164+
<div>
165+
<i18next path="term" tag="label">
166+
<a href="#" target="_blank">{{ $t("tos") }}</a>
167+
<strong>{{ $t("promise") }}</strong>
168+
</i18next>
169+
</div>`
170+
});
171+
```
172+
173+
```javascript
174+
// i18next component support to specify the place
175+
176+
const locales = {
177+
en: {
178+
tos: "Term of Service",
179+
term: "I accept {{tos}}. {{promise}}.",
180+
promise: "I promise"
181+
}
182+
};
183+
184+
...
185+
186+
Vue.component("app", {
187+
template: `
188+
<div>
189+
<i18next path="term" tag="label">
190+
<a href="#" target="_blank" place="tos">{{ $t("tos") }}</a>
191+
<strong place="promise">{{ $t("promise") }}</strong>
192+
</i18next>
193+
</div>`
194+
});
161195
```
162196
197+
198+
```javascript
199+
// i18next component support the ($t)[https://www.i18next.com/overview/api#t] options param
200+
201+
const locales = {
202+
en: {
203+
counter: "{{0}} dude",
204+
counter_plural: "{{0}} dudes"
205+
}
206+
};
207+
208+
...
209+
210+
Vue.component("app", {
211+
template: `
212+
<div>
213+
<i18next path="term" tag="label" options="{ count: 2 }">
214+
<strong>Hello</strong>
215+
</i18next>
216+
</div>`
217+
});
218+
163219
### Directive
164220

165221
Full Featured properties:

src/component.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default {
1010
type: String,
1111
required: true,
1212
},
13+
options: {
14+
type: Object,
15+
},
1316
},
1417
render(h, { props, data, children, parent }) {
1518
const i18next = parent.$i18n;
@@ -18,9 +21,10 @@ export default {
1821
}
1922

2023
const path = props.path;
24+
const options = props.options || {};
2125

2226
const REGEXP = i18next.i18next.services.interpolator.regexp;
23-
const format = i18next.t(path, { interpolation: { prefix: '#$?', suffix: '?$#' } });
27+
const format = i18next.t(path, { ...options, interpolation: { prefix: '#$?', suffix: '?$#' } });
2428
const tchildren = [];
2529

2630
format.split(REGEXP).reduce((memo, match, index) => {

test/unit/interpolation.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('interpolation', () => {
1616
hello1: 'Hello {{0}} {{1}}',
1717
hello2: 'Hello {{1}} {{0}}.',
1818
hello3: 'Hello {{first}} {{second}}.',
19+
counter: '{{0}} singular',
20+
counter_plural: '{{0}} plural',
1921
},
2022
},
2123
},
@@ -68,6 +70,21 @@ describe('interpolation', () => {
6870
});
6971

7072
it('should interpolate components by place', async () => {
73+
const el = document.createElement('div');
74+
const vm = new Vue({
75+
i18n: vueI18Next,
76+
render(h) {
77+
return h('i18next', { ref: 'text', props: { tag: 'div', path: 'counter', options: { count: 2 } } }, [
78+
h('p', { domProps: { textContent: 'Counter' } }),
79+
]);
80+
},
81+
}).$mount(el);
82+
83+
await nextTick();
84+
expect(vm.$el.outerHTML).to.equal('<div><p>Counter</p> plural</div>');
85+
});
86+
87+
it('should interpolate components supporting plurals', async () => {
7188
const el = document.createElement('div');
7289
const vm = new Vue({
7390
i18n: vueI18Next,

0 commit comments

Comments
 (0)