question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

i18n falls back to translate but key actually exists

See original GitHub issue

vue & vue-i18n version

Vue 2.0.8 Vue-i18n 4.7.2 Chrome 54, IE11, Firefox 49

Steps to reproduce

I’ll reproduce on request (if I can manage, seems like an odd bug).

What is Expected?

No warnings

What is actually happening?

A single $t() is not being translated but goes to fall back language instead.

More info: I load multiple json files eg nl-NL.json and en-US.json with a bunch of strings for the entire website. This works perfectly expect for 1 single instance. When I put the language on nl-NL, it will always fallback to en-US for this one string (always the same). The rest of the website is correctly translated. Both json files contain the correct key value.

Getting the following warning when this happens:

vue-i18n.common.js?2c49:26 [vue-i18n] Fall back to translate the keypath "infobar_start_season" with "en-US" language.

The template is (originally in Jade/Pug):

    div(v-if='!open')
      p(v-if='isSeasonOver') {{ $t('infobar_start_season', season.startMonth, season.startDay) }}
      p(v-else) {{ $t('infobar_open_nextday') }}

or converted to HTML:

<div v-if="!open">
  <p v-if="isSeasonOver">{{ $t('infobar_start_season', season.startMonth, season.startDay) }}</p>
  <p v-else="v-else">{{ $t('infobar_open_nextday') }}</p>
</div>

And the en-US string inside the json is:

  "infobar_start_season": "Our season will start on {0} {1}",

The nl-NL one is the same except different language.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Christilutcommented, Nov 20, 2016

I honoustly don’t know how to reproduce this. I can send you my project. Could you send me a mail at [omitted] or let me know what your preferred way to get the project is.

0reactions
Leslie-Wong-Hcommented, Jun 20, 2022

Using en and zh would cause the warning, while “en-US” and “zh-CN” wouldn’t.

vue-i18n: “^8.27.1”

/**
 * Get the browser language
 */
export const browserLang = (() => {
  const supports = ["en", "zh"];
  const langs = {
    zh: "zh-CN",
    en: "en-US",
  };
  const userLang = window.navigator.language || window.navigator.userLanguage;
  let lang = supports[0];
  if (userLang) {
    const useLang = userLang.trim().slice(0, 2);
    if (useLang in langs) {
      lang = useLang;
    }
  }
  return langs[lang];
})();

const i18n = new VueI18n({
  locale: browserLang,
  // Using en and zh would cause the warning, while "en-US" and "zh-CN" wouldn't.
  // messages: { en: translationEN, zh: translationCN },
  messages: { "en-US": translationEN, "zh-CN": translationCN },
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation Resolution - i18next documentation
If the key is still not found, i18n will then return the key itself, that being the first key specified if you also...
Read more >
Rails I18n, check if translation exists? - Stack Overflow
Based on what you've described, this should work: I18n.t("some_translation.key", :default => "fallback text").
Read more >
Fallback localization | Vue I18n
Explicit fallback with one locale​​ By default, falling back to fallbackLocale generates two console warnings: [vue-i18n] Value of key 'hello' is ...
Read more >
Keys to efficient i18n - Medium
In Phrase, translations are identified by “i18n key + locale”, and alongside their translation string also have two different attributes:.
Read more >
Translations (Symfony Docs)
When a key is not found in a message file, the translator will automatically fall back to the lower priority message files. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found