vue-i18n icon indicating copy to clipboard operation
vue-i18n copied to clipboard

Cannot silence fallback warnings like on V8

Open LifeIsStrange opened this issue 2 years ago • 12 comments

Reporting a bug

By default when fallbacking fail, two warnings are printed e.g

[intlify] Fall back to translate 'bf.headerBar.homeSearchBarPlaceholder' key with 'en' locale
[intlify] Not found 'bf.headerBar.homeSearchBarPlaceholder' key in 'en' locale messages.

The documentation clearly state:

To suppress these warnings (while keeping those which warn of the total absence of translation for the given key) set silentTranslationWarn: true, and silentFallbackWarn: true when initializing the createI18n.

Since the failing fallback warnings are much too noisy, I want to silence them, but keep other i18n warnings. Unfortunately, it seems that those booleans have been silently renamed and the documentation no longer reflect the state of V9 Apparently silentFallbackWarn has become fallbackWarn and the boolean logic reversed. If so why not I don't care and indeed setting fallbackWarn remove half of the warnings. silentTranslationWarn seems to now longer exist ? (at least in non-legacy mode), however I cannot find the new boolean. It should have been renamed to translationWarn to be consistent with fallbackWarn renaming but there is no such thing. The ComposerOptions type expose the following candidates:

  • missingWarn: when enabled it effectively hide warnings, but it hide too much warnings e.g I have no key in the fallback languages nor in the user locale. This is in direct contradiction with

while keeping those which warn of the total absence of translation for the given key

  • missing: this boolean might be the new silentTranslationWarn ? weird cause the type is not a boolean despite what the doc claims, I have no idea how to set missing.

I am on the latest stable Vue-i18n 9.1.x release.

const i18n = createI18n({
  fallbackWarn: false,
  missingWarn: false,
  fallbackLocale: ["en", "fr"],
  globalInjection: true,
  legacy: false,
  useScope: "global",
  locale: locale,
  messages: loadTranslations(),
  datetimeFormats,
  numberFormats,
});

edit: the doc confirm that silentTranslationWarn has been renamed to missingWarn. Then how come I have now warning when no key is found for fallbackLocale? @kazupon friendly ping

Validations

LifeIsStrange avatar Nov 08 '21 14:11 LifeIsStrange

Thank you for your feedback!

Since the failing fallback warnings are much too noisy, I want to silence them, but keep other i18n warnings. Unfortunately, it seems that those booleans have been silently renamed and the documentation no longer reflect the state of V9 Apparently silentFallbackWarn has become fallbackWarn and the boolean logic reversed. If so why not I don't care and indeed setting fallbackWarn remove half of the warnings. silentTranslationWarn seems to now longer exist ? (at least in non-legacy mode), however I cannot find the new boolean. It should have been renamed to translationWarn to be consistent with fallbackWarn renaming but there is no such thing.

You're right The documentation doesn't say much about fallback for the Composition API (when legacy: false is specified for createI18n). It is our design intent to reverse the value of fallback. The reason why we reversed the value of fallback is that we wanted the option name to suggest that true/false can be used to output warnings.

In any case, only the renamed map is not enough, we need to improve the documentation.

kazupon avatar Nov 09 '21 04:11 kazupon

Close, We've just updated docs. Thank @PeterAlfredLee ! ❤️

kazupon avatar Nov 09 '21 14:11 kazupon

@kazupon No my main issue has not been fixed. @PeterAlfredLee

I appreciate that the doc was clarified but that is not my main issue in the first place.

The remaining problem is that (while keeping those which warn of the total absence of translation for the given key) is no longer true. I have a basic, very common use case: A user connect to my website in en-US. My language doesn't has keys for en-US so we fallback to en.

  1. If there is a en key, I want to silence fallback warning.
  2. if there is no en (fallback locale) key (AKA a failure since no text will be displayed to the user..) I want Vuei18n to warn me, despite those flags: fallbackWarn: false, missingWarn: false,

the booleans silence far too much warnings since they silence 2). This is an incorrect behaviour tthat needs to be fixed (you can provide an additional boolean if needed)

LifeIsStrange avatar Nov 09 '21 14:11 LifeIsStrange

Hi @LifeIsStrange

I have a basic, very common use case: A user connect to my website in en-US. My language doesn't has keys for en-US so we fallback to en.

  1. If there is a en key, I want to silence fallback warning.
  2. if there is no en (fallback locale) key (AKA a failure since no text will be displayed to the user..) I want Vuei18n to warn me,

According to my understanding of vue-i18n-next, you can currently set fallbackWarn: false and missingWarn: true. The fallbackWarn: false will suppresses all Fall back to... warnings, and the missingWarn: true will let you see all Not found key... warnings(Include not found key in current locale and fallbackLocale)

This is an incorrect behaviour tthat needs to be fixed

I don't think this can be called incorrect, just the current vue-i18n-next is not as detailed as it is. And we can improve.

the booleans silence far too much warnings since they silence

Like I say, if you keep missingWarn: true, you can still see all Not found key... warnings. So I guess you want a option that can only let vue-i18n-next only show Not found key... warnings in fallback locale.

I'm not sure if my guess is correct, so can you make it clear what kind of warning you want to see and what kind of warning you don't want to see? Also, PR is welcome, thanks.

PeterAlfredLee avatar Nov 11 '21 03:11 PeterAlfredLee

For example, there is such a file with translation: en.json

{
  "password": "Password"
}

And vue file:

<template>
  <div>{{ t('password' }}</div>
  <div>{{ t('password2' }}</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { useI18n } from 'vue-i18n'

export default defineComponent({
  setup() {
    const { t } = useI18n({ useScope: 'global' })
    return {
      t,
    }
  },
})
</script>

By default I get messages like this in the console:

Fall back to translate 'password' key with 'en' locale. 
Not found 'password' key in 'en' locale messages. 
Fall back to translate 'password' with root locale. 
Fall back to translate 'password2' key with 'en' locale. 
Not found 'password2' key in 'en' locale messages. 
Fall back to translate 'password2' with root locale. 
Fall back to translate 'password2' key with 'en' locale. 
Not found 'password2' key in 'en' locale messages.

missingWarn: false and fallbackWarn: false suppress all of these messages, but I want to see only warning message for password2 and not for password.

darkxanter avatar Feb 03 '22 16:02 darkxanter

There should be a way to silence unimportant warnings, namely when a key is present and translated no matter where ! I'm using an approach where all often repeated messages (like day of the week) are stored in the root instead of repeating them in all components that use them. Then only the component specific messages are stored in the component own json. Because of this I'm getting a giant amount of "key missing" messages.

Simply said, fallbackWarn: false should actually silence all kind of not found and fallback warnings when the key is found somewhere with right locale. By the way, looking at the warnings order - I also think the order of the fallbacks lookup is wrong. Why, on earth, when a key is missing for let's say NL locale, it is looked up first in the local EN messages instead of the root NL messages (where it is most probably found in my case and in many others too, I believe).

So if you ask me :

  1. Make the fallback look up first to the root locale and then to the default language local locale. This will eliminate all "missing" warnings for keys that are actually living shared in the root locale.
  2. Make the fallbackWarn: false also silence the fallback to root warnings. Edit : I'll correct myself - there is fallbackRoot: false that silences the root fallback warnings, so this is irrelevant. Still - because of the order the missing key warnings remain. I'll create a sperate issue for this, though. Edit 2 : nope, it is - fallbackRoot: false actually totally switches off the fallbacks to the root. So - relevant.

This would provide a much more logical and consistent experience.

lllopo avatar Feb 05 '22 14:02 lllopo

By the way a workaround to all this mess is to use two instances - one local and one with global context and refer to the right one when translating. I tried it and it works, but I'm not quite sure how good this practice is and if it is a performance hit somehow ... a-a-and it is unnecessarily annoying, of course.

lllopo avatar Feb 10 '22 06:02 lllopo

Hello everybody, just now I found the right config by debug the source code. We just can only set 'silentTranslationWarn: true' and 'silentFallbackWarn: true' the source codes are in the file 'vue-i18n.esm-bundler.js' at line 661. const missingWarn = isBoolean(options.silentTranslationWarn) || isRegExp(options.silentTranslationWarn) ? !options.silentTranslationWarn : true; const fallbackWarn = isBoolean(options.silentFallbackWarn) || isRegExp(options.silentFallbackWarn) ? !options.silentFallbackWarn : true;

JWong1105 avatar Mar 22 '22 10:03 JWong1105

and the 'warn codes' are at line 347. if (_fallbackRoot && (isTranslateFallbackWarn(_fallbackWarn, key) || isTranslateMissingWarn(_missingWarn, key))) { warn(getWarnMessage(6 /* FALLBACK_TO_ROOT */, { key, type: warnType })); }

JWong1105 avatar Mar 22 '22 10:03 JWong1105

@JWong1105 thanks, your solution works for me too!

AEiosApp avatar Apr 12 '22 02:04 AEiosApp

您的邮件我已收到!祝您天天好心情~王杰 

JWong1105 avatar Apr 12 '22 02:04 JWong1105

Is there any news on this?

Regex is not evaluated and it's annoying. For example, when using component based localization there might be some global translations that has prefix global_, so it would be really nice if it was possible to remove warnings that fallback to the global by providing regex silentFallbackWarn: /^global_/.

Currently, it is only possible to remove all warnings or none.

just7mile avatar May 09 '22 12:05 just7mile

您的邮件我已收到!祝您天天好心情~王杰 

JWong1105 avatar May 09 '22 12:05 JWong1105