• <i id='FjgNI'><tr id='FjgNI'><dt id='FjgNI'><q id='FjgNI'><span id='FjgNI'><b id='FjgNI'><form id='FjgNI'><ins id='FjgNI'></ins><ul id='FjgNI'></ul><sub id='FjgNI'></sub></form><legend id='FjgNI'></legend><bdo id='FjgNI'><pre id='FjgNI'><center id='FjgNI'></center></pre></bdo></b><th id='FjgNI'></th></span></q></dt></tr></i><div id='FjgNI'><tfoot id='FjgNI'></tfoot><dl id='FjgNI'><fieldset id='FjgNI'></fieldset></dl></div>

    1. <legend id='FjgNI'><style id='FjgNI'><dir id='FjgNI'><q id='FjgNI'></q></dir></style></legend>
    2. <small id='FjgNI'></small><noframes id='FjgNI'>

        <bdo id='FjgNI'></bdo><ul id='FjgNI'></ul>
      <tfoot id='FjgNI'></tfoot>

      1. JavaScript(ReactJS WebApp)中的条件导入或替代?

        时间:2023-08-01
            <tbody id='jIg4K'></tbody>

          <legend id='jIg4K'><style id='jIg4K'><dir id='jIg4K'><q id='jIg4K'></q></dir></style></legend>
            • <bdo id='jIg4K'></bdo><ul id='jIg4K'></ul>

              <small id='jIg4K'></small><noframes id='jIg4K'>

                <i id='jIg4K'><tr id='jIg4K'><dt id='jIg4K'><q id='jIg4K'><span id='jIg4K'><b id='jIg4K'><form id='jIg4K'><ins id='jIg4K'></ins><ul id='jIg4K'></ul><sub id='jIg4K'></sub></form><legend id='jIg4K'></legend><bdo id='jIg4K'><pre id='jIg4K'><center id='jIg4K'></center></pre></bdo></b><th id='jIg4K'></th></span></q></dt></tr></i><div id='jIg4K'><tfoot id='jIg4K'></tfoot><dl id='jIg4K'><fieldset id='jIg4K'></fieldset></dl></div>
                • <tfoot id='jIg4K'></tfoot>
                  本文介绍了JavaScript(ReactJS WebApp)中的条件导入或替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为 ReactJS webapp 实现国际化.如何避免加载所有语言文件?

                  I'm implementing internationalization for a ReactJS webapp. How can I avoid loading all language files?

                  import ru from './ru';
                  import en from './en';
                  
                  // next lines are not important for this question from here
                  import locale from 'locale';
                  const supported = new locale.Locales(["en", "ru"])
                  
                  let language = 'ru';
                  
                  const acceptableLanguages = {
                      ru: ru,
                      en: en,
                  }
                  if (typeof window !== 'undefined') {
                      const browserLanguage = window.navigator.userLanguage || window.navigator.language;
                      const locales = new locale.Locales(browserLanguage)
                      language = locales.best(supported).code
                  }
                  
                  // till here
                  
                  // and here i'm returning a static object, containing all language variables
                  const chooseLang = () => {
                      return acceptableLanguages[language];
                  }
                  const lang = chooseLang();
                  
                  export default lang;
                  

                  推荐答案

                  不幸的是,在 ES6 中没有办法动态加载模块.

                  Unfortunately there is no way to dynamically load modules in ES6.

                  即将推出的 HTML Loader Spec 将支持此功能,因此您可以使用 一个 polyfill 以便使用它.

                  There is an upcoming HTML Loader Spec which will allow for this functionality, so you could use a polyfill in order to use that.

                  const chooseLang = () => System.import(`./${language}`);
                  export default chooseLang;
                  

                  但是,这现在是基于 Promise 的,因此需要像这样调用它:

                  However, this would now be promise-based so it would need to be called like so:

                  import language from "./language";
                  language.chooseLang().then(l => {
                      console.log(l);
                  });
                  

                  但请记住,该规范可能会彻底改变(或完全放弃).

                  But bear in mind, that spec could change radically (or be dropped altogether).

                  另一种选择是不将本地化存储为 Javascript 模块,而是存储为 JSON,例如

                  Another alternative would be to not store your localizations as Javascript modules, but as JSON instead, e.g.

                  en.json

                  { "hello_string": "Hi!" }
                  

                  language.js

                  const chooseLang = () => {
                      return fetch(`./${language}.json`)
                          .then(response => response.json());
                  };
                  

                  同样,这将是基于承诺的,因此需要这样访问:

                  Again, this would be promise based so would need to be accessed as such:

                  import language from "./language";
                  language.chooseLang().then(l => {
                      console.log(l.hello_string);
                  });
                  

                  该解决方案将完全符合 ES6 标准,并且不依赖于未来可能的功能.

                  That solution would be fully ES6-compliant and would not rely on possible future features.

                  这篇关于JavaScript(ReactJS WebApp)中的条件导入或替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:本地化字符串时使用strong/em标签的正确方法是什么? 下一篇:来自 JavaScript 的串行通信?

                  相关文章

                  <legend id='ALaTo'><style id='ALaTo'><dir id='ALaTo'><q id='ALaTo'></q></dir></style></legend>
                    <tfoot id='ALaTo'></tfoot>
                      <bdo id='ALaTo'></bdo><ul id='ALaTo'></ul>

                    1. <i id='ALaTo'><tr id='ALaTo'><dt id='ALaTo'><q id='ALaTo'><span id='ALaTo'><b id='ALaTo'><form id='ALaTo'><ins id='ALaTo'></ins><ul id='ALaTo'></ul><sub id='ALaTo'></sub></form><legend id='ALaTo'></legend><bdo id='ALaTo'><pre id='ALaTo'><center id='ALaTo'></center></pre></bdo></b><th id='ALaTo'></th></span></q></dt></tr></i><div id='ALaTo'><tfoot id='ALaTo'></tfoot><dl id='ALaTo'><fieldset id='ALaTo'></fieldset></dl></div>
                    2. <small id='ALaTo'></small><noframes id='ALaTo'>