简体   繁体   English

从网址导入json文件

[英]importing json file from url

I am doing translations with react-i18next , at the moment everything works as expected as i am importing the translations files locally as below 我正在使用react-i18next进行翻译,此刻一切都按预期进行,因为我正在本地导入翻译文件,如下所示

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

Now the challenge is , i want to be able to update the translation files , and the implementation i decided to go with is to put the files on Amazon S3 and read them , whenever update is needed i will send the updated json object to the backend service that will update the files . 现在的挑战是,我希望能够更新翻译文件,而我决定采用的实现方式是将文件放在Amazon S3上并读取它们,每当需要更新时,我都会将更新的json对象发送到后端将更新文件的服务。

The problem i am facing is figuring out how to import the files in react project as if i do 我面临的问题是弄清楚如何像我一样在React项目中导入文件

import translationEN from 'https:my-s3-url/locales/en/translation.json'
import translationPT from 'https:my-s3-url/locales/en/translation.json'

It gives me an error . 这给我一个错误。 Module not found: Error: Cannot resolve module 'https:my-s3-url/locales/en/translation.json' . 找不到模块:错误:无法解析模块'https:my-s3-url / locales / en / translation.json'。

Using i18next-xhr-backend plugin also does not work with the below configurations :- 使用i18next-xhr-backend插件也不适用于以下配置:-

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;

Any suggestion on the best way to approach this will be highly appreciated , thanks :-) 任何关于采用最佳方法的建议都将受到高度赞赏,谢谢:-)

You need to provide values for interpolation in loadPath field, not just a string. 您需要在loadPath字段中提供用于插值的值,而不仅仅是字符串。 And most likely crossDomain has to be also set to true . 而且最有可能的crossDomain也必须设置为true

backend: {
  loadPath: 'https://my-s3-bucket.s3.amazonaws.com/locales/{{lng}}/{{ns}}.json',
  crossDomain: true,
},

Link to docs 链接到文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM