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.

Load fallback language when needed (lazy-load)

See original GitHub issue

nuxt-i18n version: 2.3.4

Imagine you have two languages, en (default) and de. But only parts of the en language is localized in the de file. If you now enter a page (e.g. /de/only-half-localized-route), you see the correct translations (for de) where it applies, but no en translation because the fallback language isn’t loaded properly because of lazy-loading. Instead, you see the translation keys.

A naive fix for it would be to load the fallback language every time, but I guess there is a way to check if a fallback is needed and load the fallback language in that case (if not already loaded).

<div align="right">This feature request is available on Nuxt.js community (#c39)</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:24 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
rchlcommented, Nov 6, 2018

Possibly simpler workaround:

In nuxt_config.js, using require or import, load default locale manually and pass to vue-i18n configuration:

[
      'nuxt-i18n',
      {
            // ...
            vueI18n: {
                fallbackLocale: 'en',
                messages: {
                    en: require('./app/locales/en.json'),
                },
            },
      }
],

nuxt-i18n will merge current locale messages into same object and then vue-i18n will be able to access default locale for fallback.

6reactions
MarvinMilescommented, Jan 9, 2019

My function for load fallback language when needed

/plugins/i18n.js:

export default ({ app }, inject) => {

inject ('loadLanguageAsync', async (lang) => {
    if (app.i18n.locale !== lang) {
      if (!app.i18n.loadedLanguages.includes(lang)) {
        try {
          const module = await import(/* webpackChunkName: "lang-[request]" */ `@/lang/${lang}`)
          const messages = module.default ? module.default : module
          app.i18n.setLocaleMessage(lang, typeof messages === 'function' ? await Promise.resolve(messages()) : messages)
          app.i18n.loadedLanguages.push(lang)
        } catch (error) {
          console.error(error)
        }
      }
      app.i18n.locale = lang
    }
  })

}

then call “this.$loadLanguageAsync(lang)” from anywhere

I am using that for translating via keyboard shortcuts events 😃

Hope this will help someone

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load fallback language when needed (lazy-load) #34 - GitHub
Possibly simpler workaround: In nuxt_config.js, using require or import , load default locale manually and pass to vue-i18n configuration:.
Read more >
Lazy loading translations | Vue I18n
Lazy loading or asynchronously loading the translation files is really easy when using Webpack. The lang folder is where all of our translation ......
Read more >
Add lazy loading to React i18next with React Suspense
Error: withI18nextTranslation(Unknown) suspended while rendering, but no fallback UI was specified. Step 1: Install i18n-xhr-backend NPM module. First you will ...
Read more >
Lazy loading best practices - web.dev
Lazy loading delays the loading of resources until after the DOM is interactive when scripts have finished loading and begin execution. For ...
Read more >
Lazy Load Translation Files In Angular Using Transloco
Adding the TRANSLOCO_SCOPE to the providers instruct Transloco load the corresponding scope based on the active language and merge it under 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