Skip to content

Commit

Permalink
fix: Translations set to null if key doesn't exist (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkie authored Aug 9, 2023
1 parent e356a56 commit 1947841
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, Plugin, computed, ComputedRef } from 'vue'
import {reactive, Plugin, computed, ComputedRef, watchEffect} from 'vue'
import { OptionsInterface } from './interfaces/options'
import { PluginOptionsInterface } from './interfaces/plugin-options'
import { LanguageInterface } from './interfaces/language'
Expand Down Expand Up @@ -391,7 +391,9 @@ export class I18n {
* Get the translation for the given key and watch for any changes.
*/
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
watchEffect(() => {
this.activeMessages[key] = this.findTranslation(key) || this.findTranslation(key.replace(/\//g, '.')) || key
})

return computed(() => this.makeReplacements(this.activeMessages[key], replacements))
}
Expand Down
12 changes: 12 additions & 0 deletions test/plurization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,15 @@ it.each([

expect(choose(message, number, lang)).toBe(correctMessage);
});

it('checks if watching wTransChoice works if key does not exist', async () => {
await global.mountPlugin('<div />');

const translated = wTransChoice('Not existing translation :count', 1);

expect(translated.value).toBe('Not existing translation 1');

await loadLanguageAsync('en');

expect(translated.value).toBe('Not existing translation 1');
})
12 changes: 12 additions & 0 deletions test/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,15 @@ it('allows to use html tags on translations even if the key does not exist', asy

expect(trans('<div>Not Exist</div>')).toBe('<div>Not Exist</div>');
})

it('checks if watching wTrans works if key does not exist', async () => {
await global.mountPlugin('<div />');

const translated = wTrans('Not existing translation');

expect(translated.value).toBe('Not existing translation');

await loadLanguageAsync('en');

expect(translated.value).toBe('Not existing translation');
})

0 comments on commit 1947841

Please sign in to comment.