Skip to content

Commit c5921bc

Browse files
authored
fix: Not handling empty string translations. (#155)
1 parent 4850e94 commit c5921bc

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/index.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {reactive, Plugin, computed, ComputedRef, watchEffect} from 'vue'
1+
import { reactive, Plugin, computed, ComputedRef, watchEffect } from 'vue'
22
import { OptionsInterface } from './interfaces/options'
33
import { PluginOptionsInterface } from './interfaces/plugin-options'
44
import { LanguageInterface } from './interfaces/language'
@@ -349,13 +349,13 @@ export class I18n {
349349
}
350350

351351
for (const [key, value] of Object.entries(this.fallbackMessages)) {
352-
if (!this.activeMessages[key] || this.activeMessages[key] === key) {
352+
if (!this.isValid(messages[key]) || this.activeMessages[key] === key) {
353353
this.activeMessages[key] = value
354354
}
355355
}
356356

357357
for (const [key] of Object.entries(this.activeMessages)) {
358-
if (!messages[key] && !this.fallbackMessages[key]) {
358+
if (!this.isValid(messages[key]) && !this.isValid(this.fallbackMessages[key])) {
359359
this.activeMessages[key] = null
360360
}
361361
}
@@ -392,7 +392,13 @@ export class I18n {
392392
*/
393393
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
394394
watchEffect(() => {
395-
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
395+
let value = this.findTranslation(key)
396+
397+
if (!this.isValid(value)) {
398+
value = this.findTranslation(key.replace(/\//g, '.'))
399+
}
400+
401+
this.activeMessages[key] = this.isValid(value) ? value : key
396402
})
397403

398404
return computed(() => this.makeReplacements(this.activeMessages[key], replacements))
@@ -420,7 +426,7 @@ export class I18n {
420426
* Find translation in memory.
421427
*/
422428
findTranslation(key) {
423-
if (this.activeMessages[key]) {
429+
if (this.isValid(this.activeMessages[key])) {
424430
return this.activeMessages[key]
425431
}
426432

@@ -457,6 +463,13 @@ export class I18n {
457463
return message
458464
}
459465

466+
/**
467+
* Checks if the message provided is valid.
468+
*/
469+
isValid(message: string | null | undefined): boolean {
470+
return message !== undefined && message !== null
471+
}
472+
460473
/**
461474
* Resets all the data stored in memory.
462475
*/

test/fixtures/lang/pt.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"foo.bar": "baz",
77
"Start/end": "Início/Fim",
88
"Get started.": "Comece.",
9-
"<div>Welcome</div>": "<div>Bem-vindo</div>"
9+
"<div>Welcome</div>": "<div>Bem-vindo</div>",
10+
"Empty string": ""
1011
}

test/translate.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ it('translates with "trans" helper', async () => {
1717
expect(trans('Welcome!')).toBe('Bem-vindo!');
1818
})
1919

20+
it('handles empty translations', async () => {
21+
await global.mountPlugin();
22+
23+
expect(trans('Empty string')).toBe('');
24+
})
25+
2026
it('returns the same message if there is no resolve method provided', async () => {
2127
const wrapper = mount({ template: `<h1>{{ $t('Welcome!') }}</h1>` }, {
2228
global: {
@@ -256,4 +262,4 @@ it('checks if watching wTrans works if key does not exist', async () => {
256262
await loadLanguageAsync('en');
257263

258264
expect(translated.value).toBe('Not existing translation');
259-
})
265+
})

0 commit comments

Comments
 (0)