Skip to content

Commit ffbc92b

Browse files
authored
fix: Translations with / no need to replace with .. (#118)
1 parent 8a35559 commit ffbc92b

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/index.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,7 @@ export class I18n {
381381
* Get the translation for the given key and watch for any changes.
382382
*/
383383
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
384-
if (!this.activeMessages[key] && !this.activeMessages[`${key}.0`]) {
385-
key = key.replace(/\//g, '.')
386-
}
387-
388-
if (!this.activeMessages[key]) {
389-
const hasChildItems = this.activeMessages[`${key}.0`] !== undefined
390-
391-
if (hasChildItems) {
392-
const childItems = Object.entries(this.activeMessages)
393-
.filter((item) => item[0].startsWith(`${key}.`))
394-
.map((item) => item[1])
395-
this.activeMessages[key] = reactive(childItems)
396-
} else {
397-
this.activeMessages[key] = key
398-
}
399-
}
384+
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
400385

401386
return computed(() => this.makeReplacements(this.activeMessages[key], replacements))
402387
}
@@ -419,6 +404,27 @@ export class I18n {
419404
return computed(() => this.makeReplacements(choose(message.value, number, this.options.lang), replacements))
420405
}
421406

407+
/**
408+
* Find translation in memory.
409+
*/
410+
findTranslation(key) {
411+
if (this.activeMessages[key]) {
412+
return this.activeMessages[key]
413+
}
414+
415+
const hasChildItems = this.activeMessages[`${key}.0`] !== undefined
416+
417+
if (hasChildItems) {
418+
const childItems = Object.entries(this.activeMessages)
419+
.filter((item) => item[0].startsWith(`${key}.`))
420+
.map((item) => item[1])
421+
422+
return reactive(childItems)
423+
}
424+
425+
return this.activeMessages[key]
426+
}
427+
422428
/**
423429
* Make the place-holder replacements on a line.
424430
*/

src/mix.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ mix.extend(
5151

5252
config.plugins.push(
5353
new BeforeBuildPlugin(() => {
54-
files = generateFiles(this.langPath, [
55-
...parseAll(this.frameworkLangPath),
56-
...parseAll(this.langPath)
57-
])
54+
files = generateFiles(this.langPath, [...parseAll(this.frameworkLangPath), ...parseAll(this.langPath)])
5855
})
5956
)
6057

test/fixtures/lang/pt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"{1} :count minute ago|[2,*] :count minutes ago": "{1} há :count minuto|[2,*] há :count minutos",
66
"foo.bar": "baz",
77
"Start/end": "Início/Fim",
8-
"Get started.": "Comece."
8+
"Get started.": "Comece.",
9+
"<div>Welcome</div>": "<div>Bem-vindo</div>"
910
}

test/translate.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,15 @@ it('does not translate existing strings which contain delimiter symbols', async
233233
expect(trans('Start/end')).toBe('Início/Fim');
234234
expect(trans('Get started.')).toBe('Comece.');
235235
})
236+
237+
it('allows to use html tags on translations', async () => {
238+
await global.mountPlugin()
239+
240+
expect(trans('<div>Welcome</div>')).toBe('<div>Bem-vindo</div>');
241+
})
242+
243+
it('allows to use html tags on translations even if the key does not exist', async () => {
244+
await global.mountPlugin()
245+
246+
expect(trans('<div>Not Exist</div>')).toBe('<div>Not Exist</div>');
247+
})

0 commit comments

Comments
 (0)