Skip to content

Commit 355c218

Browse files
committed
fix: Allow multiple occurrences replacements
1 parent c30f97c commit 355c218

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ export class I18n {
385385
value = value.toString()
386386

387387
message = message
388-
.replace(`:${key}`, value)
389-
.replace(`:${key.toUpperCase()}`, value.toUpperCase())
390-
.replace(`:${capitalize(key)}`, capitalize(value))
388+
.replace(new RegExp(`:${key}`, 'g'), value)
389+
.replace(new RegExp(`:${key.toUpperCase()}`, 'g'), value.toUpperCase())
390+
.replace(new RegExp(`:${capitalize(key)}`, 'g'), capitalize(value))
391391
})
392392

393393
return message

test/fixtures/lang/pt.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Welcome!": "Bem-vindo!",
33
"Welcome, :name!": "Bem-vindo, :name!",
4+
"hi :name, hi :name": "olá :name, olá :name",
45
"{1} :count minute ago|[2,*] :count minutes ago": "{1} há :count minuto|[2,*] há :count minutos",
56
"foo.bar": "baz",
67
"Start/end": "Início/Fim",

test/translate.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,26 @@ it('translates key with values with $t mixin', async () => {
6161
expect(wrapper.html()).toBe('<h1>Bem-vindo, Francisco!</h1>')
6262
})
6363

64+
it('replaces multiple occurrences with $t mixin', async () => {
65+
const wrapper = await global.mountPlugin(`<h1>{{ $t('hi :name, hi :name', { name: 'Francisco' }) }}</h1>`);
66+
67+
expect(wrapper.html()).toBe('<h1>olá Francisco, olá Francisco</h1>')
68+
})
69+
6470
it('translates key with values with "trans" helper', async () => {
6571
await global.mountPlugin();
6672

6773
expect(trans('Welcome, :name!', { name: 'Francisco' }))
6874
.toBe('Bem-vindo, Francisco!')
6975
})
7076

77+
it('replaces multiple occurrences with "trans" helper', async () => {
78+
await global.mountPlugin();
79+
80+
expect(trans('hi :name, hi :name', { name: 'Francisco' }))
81+
.toBe('olá Francisco, olá Francisco')
82+
})
83+
7184
it('loads a lang', async () => {
7285
const wrapper = await global.mountPlugin(`<h1>{{ $t('Welcome, :name!', { name: 'Francisco' }) }}</h1>`, 'en');
7386

@@ -204,4 +217,4 @@ it('does not translate existing strings which contain delimiter symbols', async
204217

205218
expect(trans('Start/end')).toBe('Início/Fim');
206219
expect(trans('Get started.')).toBe('Comece.');
207-
})
220+
})

0 commit comments

Comments
 (0)