首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从url导入json文件

从url导入json文件
EN

Stack Overflow用户
提问于 2019-09-06 08:59:20
回答 1查看 1.4K关注 0票数 1

我正在使用react-i18next进行翻译,目前一切正常,因为我正在本地导入翻译文件,如下所示

代码语言:javascript
复制
import i18n from 'i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
import { reactI18nextModule } from 'react-i18next';
import translationEN from '!json!../public/locales/en/translation.json'
import translationPT from '!json!../public/locales/pt/translation.json';

// the translations
const resources = {
  en: {
    translation: translationEN
  },
  pt: {
    translation: translationPT
  }
};

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to the react-i18next components.
  // Alternative use the I18nextProvider: https://react.i18next.com/components/i18nextprovider
  .use(reactI18nextModule)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    resources,
    fallbackLng: 'en',
    ........... Code continues

现在的挑战是,我希望能够更新翻译文件,我决定采用的实现是将文件放到亚马逊S3上并读取它们,每当需要更新时,我都会将更新的json对象发送到将更新文件的后端服务。

我面临的问题是弄清楚如何像我那样导入react项目中的文件。

代码语言:javascript
复制
import translationEN from 'https:my-s3-url/locales/en/translation.json'
import translationPT from 'https:my-s3-url/locales/en/translation.json'

它给了我一个错误。找不到模块:错误:无法解析模块'https:my-s3-url/locales/en/translation.json‘。

使用i18next-xhr-backend插件也不能使用以下配置:-

代码语言:javascript
复制
import i18n from 'i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
import { reactI18nextModule } from 'react-i18next';


i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to the react-i18next components.
  // Alternative use the I18nextProvider: https://react.i18next.com/components/i18nextprovider
  .use(reactI18nextModule)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
      backend: {
        loadPath: 'https://my-s3-bucket.s3.amazonaws.com/locales/en/translation.json'
      },
    fallbackLng: 'en',
    debug: true,
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },

    // special options for react-i18next
    // learn more: https://react.i18next.com/components/i18next-instance
    react: {
      wait: true,
      nsMode: 'default'
    },
  });

export default i18n;

任何关于最佳方法的建议都将受到高度赞赏,谢谢:-)

EN

回答 1

Stack Overflow用户

发布于 2019-09-07 05:16:28

您需要在loadPath字段中提供插值的值,而不仅仅是一个字符串。最有可能的情况是,crossDomain也必须设置为true

代码语言:javascript
复制
backend: {
  loadPath: 'https://my-s3-bucket.s3.amazonaws.com/locales/{{lng}}/{{ns}}.json',
  crossDomain: true,
},

Link to docs

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57814519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档