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.

Dynamic loading a language object instead of load all in the begin when i have 10 language.

See original GitHub issue

now ,my code is like that:

import Vue from 'vue';
import VueI18n from 'vue-i18n';
import en from 'module/user/i18n/en';
import zh from 'module/user/i18n/zh';

Vue.use(VueI18n);

const messages = {
  en: en,
  zh: zh,
  ...
};
let lang = window.localStorage.getItem('lang') || 'en';

export default new VueI18n({
  locale: lang,
  messages
});

but and i want to dynamic loading a language objec like this:

import Vue from 'vue';
import VueI18n from 'vue-i18n';
//  import en from 'module/user/i18n/en';
// import zh from 'module/user/i18n/zh';

Vue.use(VueI18n);

const messages = {
  en:  () => import('module/user/i18n/en');  // i want to  dynamic loading,but it does not work!!!
  zh:  () => import('module/user/i18n/zh');  
};
...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
kuanhsuhcommented, Sep 26, 2017

Hi guys, I figure it out.

Here’s the code in my vuex.

async setLangNew({commit}, payload){
    if (payload in app.$i18n.messages) {
      commit(types.SET_LANG, payload)
    } else {
      try {
        // const res = await axios.get(`./src/lang/${payload}.json`)
        // const res = await axios.get(`../../src/lang/${payload}.json`)
        const res = await import(`./src/lang/locale/${payload}.json`)
        app.$i18n.setLocaleMessage(payload, res.data)
        commit(types.SET_LANG, payload)
      }
      catch(e) {
        console.log(e)
      }
    }
  }

if you want the full demo, you can check out my repo, vue-i18n sandbox

5reactions
smarescommented, Sep 19, 2017

Loading all possible languages if your user only uses English for example is not sensible. Not only does it increase the bundle size, but the localizations for several languages also have to be kept in memory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a module system (dynamic loading) in C
dynamic loading library is a mechanism with which we can run our program, and at runtime, decide what function, we want to use...
Read more >
Dynamic loading - Wikipedia
Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve...
Read more >
Principles of Programming Languages: Chapter 9 - RICE CS
A static field f in a class C is simply a global variable with the name C.f: there is one such field in...
Read more >
Dynamic component loader - Angular
Instead, you need a way to load a new component without a fixed reference to the ... Angular comes with its own API...
Read more >
Vue i18n: Building a multi-language app with locale switcher
Datetime and number localization. Switching the locale. Integrating Vue Router and making it work with multiple locales. Lazy load translation ...
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