Skip to content

Commit 1947841

Browse files
authored
fix: Translations set to null if key doesn't exist (#146)
1 parent e356a56 commit 1947841

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive, Plugin, computed, ComputedRef } 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'
@@ -391,7 +391,9 @@ export class I18n {
391391
* Get the translation for the given key and watch for any changes.
392392
*/
393393
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
394-
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
394+
watchEffect(() => {
395+
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
396+
})
395397

396398
return computed(() => this.makeReplacements(this.activeMessages[key], replacements))
397399
}

test/plurization.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,15 @@ it.each([
142142

143143
expect(choose(message, number, lang)).toBe(correctMessage);
144144
});
145+
146+
it('checks if watching wTransChoice works if key does not exist', async () => {
147+
await global.mountPlugin('<div />');
148+
149+
const translated = wTransChoice('Not existing translation :count', 1);
150+
151+
expect(translated.value).toBe('Not existing translation 1');
152+
153+
await loadLanguageAsync('en');
154+
155+
expect(translated.value).toBe('Not existing translation 1');
156+
})

test/translate.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,15 @@ it('allows to use html tags on translations even if the key does not exist', asy
245245

246246
expect(trans('<div>Not Exist</div>')).toBe('<div>Not Exist</div>');
247247
})
248+
249+
it('checks if watching wTrans works if key does not exist', async () => {
250+
await global.mountPlugin('<div />');
251+
252+
const translated = wTrans('Not existing translation');
253+
254+
expect(translated.value).toBe('Not existing translation');
255+
256+
await loadLanguageAsync('en');
257+
258+
expect(translated.value).toBe('Not existing translation');
259+
})

0 commit comments

Comments
 (0)