|
1 |
| -import {reactive, Plugin, computed, ComputedRef, watchEffect} from 'vue' |
| 1 | +import { reactive, Plugin, computed, ComputedRef, watchEffect } from 'vue' |
2 | 2 | import { OptionsInterface } from './interfaces/options'
|
3 | 3 | import { PluginOptionsInterface } from './interfaces/plugin-options'
|
4 | 4 | import { LanguageInterface } from './interfaces/language'
|
@@ -349,13 +349,13 @@ export class I18n {
|
349 | 349 | }
|
350 | 350 |
|
351 | 351 | 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) { |
353 | 353 | this.activeMessages[key] = value
|
354 | 354 | }
|
355 | 355 | }
|
356 | 356 |
|
357 | 357 | 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])) { |
359 | 359 | this.activeMessages[key] = null
|
360 | 360 | }
|
361 | 361 | }
|
@@ -392,7 +392,13 @@ export class I18n {
|
392 | 392 | */
|
393 | 393 | wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
|
394 | 394 | 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 |
396 | 402 | })
|
397 | 403 |
|
398 | 404 | return computed(() => this.makeReplacements(this.activeMessages[key], replacements))
|
@@ -420,7 +426,7 @@ export class I18n {
|
420 | 426 | * Find translation in memory.
|
421 | 427 | */
|
422 | 428 | findTranslation(key) {
|
423 |
| - if (this.activeMessages[key]) { |
| 429 | + if (this.isValid(this.activeMessages[key])) { |
424 | 430 | return this.activeMessages[key]
|
425 | 431 | }
|
426 | 432 |
|
@@ -457,6 +463,13 @@ export class I18n {
|
457 | 463 | return message
|
458 | 464 | }
|
459 | 465 |
|
| 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 | + |
460 | 473 | /**
|
461 | 474 | * Resets all the data stored in memory.
|
462 | 475 | */
|
|
0 commit comments