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.

Fallback language not found if $locale is not listed in dictionary

See original GitHub issue

Describe the bug I have a website that can handle hundreds of languages decided by their authors. some support pages are maintained by us and by a small supported amount of languages.

Currently the page locale is selected based on whatever the user currently is having or the author has selected for that content, this is likely to be not covered by the dictionaries.

For instance, we support en-GB language and also this one is the fallback language if the user has selected one without dictionary. But when the user selects en-AU. Then i18n fails and none is loaded.

Is all languages to be expected to be part of dictionaries?

In the console log I get the following message as example (and many more with the same):

[svelte-i18n] The message "page.help.title" was not found in "en", "en-AU", "en", "en-GB". but the dictionary works and the language is loaded:

{
    "en-GB": {
        "page": {
            "help": {
                "title": "Need some help?",
               ...
            },
           ...
        }
    },   
    "zh-CN": {...},
    ...  // IMPORTANT: en-AU is NOT a dictionary supported and is not in this list
}

As soon as i switch the language by clicking on en-GB all works again, and also if the language is covered by the dictionaries (ex: zh-CN ) even if some translations for that language are missing.

Does all the languages has to be included as dictionaries? The fallback method should always work I believe

How do I load dictionaries:

  register('en-GB', () => import('..../en-GB.json'));
  register('zh-CN', () => import('..../zh-CN.json'));
  ...  // IMPORTANT: en-AU is NOT a dictionary supported and is not registered here

  init({
    fallbackLocale: 'en-GB',
    initialLocale: 'en-GB',
  });

How locale is set by user actions:

$: if ($lang !== undefined) { // Lang is reactive and selected by the options of the page, NOT the locales available
    const l = $lang;
    setLang(l);          // some local cookies for usage to handle reload
    const parsedLang = isChineseSpecific(l) ? 'zh-CN' : l; // Chinese fix to force simplified instead of traditional chinese
    if ($locale !== parsedLang) {
      $locale = parsedLang; // if users is on `en-AU`, support pages should use still the fallback `en-GB`, but it fails.
    }
  }

Logs

main.js:1147 [svelte-i18n] The message "page.help.title" was not found in "en", "en-AU", "en", "en-GB".
main.js:1147 [svelte-i18n] The message "page.help.subtitle" was not found in "en", "en-AU", "en", "en-GB".
main.js:1147 [svelte-i18n] The message "page.help.text" was not found in "en", "en-AU", "en", "en-GB".

Stacktrace for the first line

main.js:1147 [svelte-i18n] The message "page.help.title" was not found in "en", "en-AU", "en", "en-GB".

[svelte-i18n] The message “page.help.title” was not found in “en”, “en-AU”, “en”, “en-GB”. console.<computed> @ main.js:1147 B @ main.js:10229 instance @ help.js:116 init @ main.js:910 Help @ help.js:150 create_default_slot @ main.js:2380 create_slot @ main.js:110 create_fragment @ main.js:1180 init @ main.js:925 Noop @ main.js:1247 create_each_block$1 @ main.js:2478 update_keyed_each @ main.js:731 update @ main.js:2175 update @ main.js:2591 update @ main.js:420 flush @ main.js:388 Promise.then (async) schedule_update @ main.js:363 make_dirty @ main.js:880 (anonymous) @ main.js:916 onComponentLoaded @ main.js:2682 Promise.then (async) setComponent @ main.js:2702 $$self.$$.update @ main.js:2797 update @ main.js:416 flush @ main.js:388 Promise.then (async) schedule_update @ main.js:363 make_dirty @ main.js:880 (anonymous) @ main.js:916 callback @ main.js:3126 updatePage @ main.js:2897 async function (async) updatePage @ main.js:2894 updatePage @ main.js:3124 goto @ main.js:1931 onTutorial @ index3.js:265 (anonymous) @ main.js:333 (anonymous) @ main.js:332 showTutorial @ WelcomePage.js:18643 (anonymous) @ main.js:350 bubble @ main.js:350 click_handler_1 @ WelcomePage.js:5582

To Reproduce

  1. Create an svelte app with 1 language i18n only and set it as fallback
  2. force locale to any other thing
  3. Check that the variables are not loading the fallback

Expected behavior Whichever string you add as locale, if it does not exist, fallback should be used instead.

Information about your project:

Built and tested with:

  • Rollup
  • Svelte Preprocess
  • svelte-i18n v.3.3.7
  • Mac Catalina
  • Latest Chrome, Firefox, Safari.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kaisermanncommented, Mar 27, 2021

Release in 3.3.8.

I think this change makes sense, at least for now. Let’s see what the future brings.

1reaction
Egnuscommented, Mar 27, 2021

Nice, I cloned the project, built it, changed dist with my dist in node_modules and it works perfect for my case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fallback Language support · Issue #166 · aralroca/next-translate
It would be great to have support for fallback translations if the current language locale file is missing a given translation key, ...
Read more >
Set a fallback language - Product Documentation | ServiceNow
Set a fallback language · Navigate to All > System Localization > Languages. · In the Languages [sys_language] table, select the name of...
Read more >
How to set i18n.js fallback language? - Stack Overflow
For establishing the fallback language, I am just trying to find the given locale (function argument) in my array of app languages.
Read more >
i18n URLs are not matched against the fallback language.
It activates "id" language when meets "id-button" in slug despite I only use in settings "id" not "id-button" locale. And I'm sure no...
Read more >
Fallback - i18next documentation
By default, if a variant (containing region, script, etc) is not found, i18next will look for the same key in the broader version...
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