Skip to content

Commit 734df7c

Browse files
authored
Merge pull request #73 from REJack/feat/language-array-support
feat: add support for language arrays
2 parents 7cdeff9 + 2f5f3fa commit 734df7c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,14 @@ export class I18n {
338338
*/
339339
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
340340
if (!this.activeMessages[key]) {
341-
this.activeMessages[key] = key
341+
const hasChildItems = this.activeMessages[`${key}.0`] !== undefined;
342+
343+
if (hasChildItems) {
344+
const childItems = Object.entries(this.activeMessages).filter((item) => item[0].startsWith(`${key}.`)).map(item => item[1]);
345+
this.activeMessages[key] = reactive(childItems);
346+
} else {
347+
this.activeMessages[key] = key;
348+
}
342349
}
343350

344351
return computed(() => this.makeReplacements(this.activeMessages[key], replacements))

test/fixtures/lang/de.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"auth.arr.0": "foo",
3+
"auth.arr.1": "bar"
4+
}

test/translate.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,17 @@ it('resolves translated data with loader if there is only .php files for that la
173173

174174
expect(wrapper.html()).toBe('<h1>Ces identifiants ne correspondent pas à nos enregistrements.</h1>')
175175
})
176+
177+
178+
it('translates arrays with $t mixin', async () => {
179+
const wrapper = await global.mountPlugin(`<h1 v-for="line in $t('auth.arr')">{{line}}</h1>`, 'de');
180+
181+
expect(wrapper.html()).toBe("<h1>foo</h1>\n<h1>bar</h1>")
182+
})
183+
184+
it('translates arrays with "trans" helper', async () => {
185+
await global.mountPlugin(undefined, 'de');
186+
187+
expect(trans('auth.arr')).toStrictEqual(['foo', 'bar']);
188+
})
189+

0 commit comments

Comments
 (0)