Skip to content

Commit e952fb0

Browse files
authored
feat: Fix translation of strings that use delimiter symbols (#89)
1 parent fec4ff7 commit e952fb0

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ export class I18n {
337337
* Get the translation for the given key and watch for any changes.
338338
*/
339339
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
340-
key = key.replace(/\//g, '.')
340+
if (!this.activeMessages[key] && !this.activeMessages[`${key}.0`]) {
341+
key = key.replace(/\//g, '.')
342+
}
341343

342344
if (!this.activeMessages[key]) {
343345
const hasChildItems = this.activeMessages[`${key}.0`] !== undefined

test/fixtures/lang/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
"Welcome!": "Wecome!",
33
"Welcome, :name!": "Welcome, :name!",
44
"Only Available on EN": "Only Available on EN",
5-
"{1} :count minute ago|[2,*] :count minutes ago": "{1} :count minute ago|[2,*] :count minutes ago"
5+
"{1} :count minute ago|[2,*] :count minutes ago": "{1} :count minute ago|[2,*] :count minutes ago",
6+
"Start/end": "Start/End",
7+
"Get started.": "Get started."
68
}

test/fixtures/lang/pt.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
"Welcome!": "Bem-vindo!",
33
"Welcome, :name!": "Bem-vindo, :name!",
44
"{1} :count minute ago|[2,*] :count minutes ago": "{1} há :count minuto|[2,*] há :count minutos",
5-
"foo.bar": "baz"
5+
"foo.bar": "baz",
6+
"Start/end": "Início/Fim",
7+
"Get started.": "Comece."
68
}

test/translate.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,11 @@ it('translates a nested file item while using "/" and "." at the same time as a
197197

198198
expect(trans('nested/cars/car.is_electric')).toBe('É elétrico?');
199199
expect(trans('nested/cars/car.foo.level1.level2')).toBe('barpt');
200+
})
201+
202+
it('does not translate existing strings which contain delimiter symbols', async () => {
203+
await global.mountPlugin()
204+
205+
expect(trans('Start/end')).toBe('Início/Fim');
206+
expect(trans('Get started.')).toBe('Comece.');
200207
})

0 commit comments

Comments
 (0)