Giter VIP home page Giter VIP logo

Comments (8)

AgDude avatar AgDude commented on May 23, 2024 2

Here is what I ended up with. We had to copy the request class from i18next to deal with some other issues around status code, which I think are not documented earlier in this issue. Our initialization also ended up a bit ugly, because depending on your javascript framework within cordova (ionic v1 in this case), you can't let the application initialize until after the translation strings are loaded.

function requestWithXmlHttpRequest (options, url, payload, callback) {
  if (payload && typeof payload === 'object') {
    // if (!cache) payload._t = Date.now()
    // URL encoded form payload must be in querystring format
    payload = addQueryString('', payload).slice(1)
  }

  try {
    let x
    x = new XMLHttpRequest();
    x.open('GET', url, 1)
    if (!options.crossDomain) {
      x.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
    }
    x.withCredentials = !!options.withCredentials
    if (payload) {
      x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    }
    if (x.overrideMimeType) {
      x.overrideMimeType('application/json')
    }
    let h = options.customHeaders
    h = typeof h === 'function' ? h() : h
    if (h) {
      for (var i in h) {
        x.setRequestHeader(i, h[i])
      }
    }
    x.onreadystatechange = () => {
      x.readyState > 3 && callback(x.status >= 400 ? x.statusText : null, { status: x.status || 200, data: x.responseText }) // in android webview loading a file is status status 0
    }
    x.send(payload)
  } catch (e) {
    console && console.log(e)
  }
}


i18next
.use(window.i18nextHttpBackend)
.init({
  backend: {
    loadPath: 'locale/{{lng}}.json',
    request: requestWithXmlHttpRequest
  },
  load: 'currentOnly',
  ns: ['translation'],
  defaultNS: 'translation',
  lng: 'en-US',
  fallbackLng: 'en-US',
  languages: [{{{LANGUAGES}}}, 'dev']  // replaced by our build task with currently available languages
})
.then((t) => {
    var packages = {{{PACKAGES}}};  // This loads the application, but we don't want to load that without first initializing the languages or we end up caching some of the wrong/undefined strings
    console.log('loading packages');

    packages.forEach(function(pkg, index) {
      var scriptTag = document.createElement('script');
      scriptTag.async = false;
      scriptTag.setAttribute('src', pkg);
      document.head.appendChild(scriptTag);
    });

  });

from i18next-http-backend.

adrai avatar adrai commented on May 23, 2024

This is an http adapter, not a file adapter...
Maybe this helps: https://github.com/i18next/i18next-fs-backend

from i18next-http-backend.

AgDude avatar AgDude commented on May 23, 2024

@adrai Cordova is not node. The fs backend won't work in cordova. Cordova works using http, and all files are served with the file:// scheme. Perhaps a completely separate plugin is required, but this should work if it is allowed to fallback to XHR.

from i18next-http-backend.

jamuhl avatar jamuhl commented on May 23, 2024

@AgDude might be time for a custom backend plugin: https://www.i18next.com/misc/creating-own-plugins

(you also can chain multiples: eg. HTTP and file:// -> https://github.com/i18next/i18next-chained-backend)

from i18next-http-backend.

adrai avatar adrai commented on May 23, 2024

Sorry, never worked with Cordova...
This module uses the offered fetch api by the runtime and if not there, the XMLHttpRequest...
If this is not suitable for you, you may override the request option to pass an own request function => https://github.com/i18next/i18next-http-backend#backend-options

from i18next-http-backend.

AgDude avatar AgDude commented on May 23, 2024

Thanks for the pointer on requestOptions as a function. I can probably make that work.

from i18next-http-backend.

McGiverGim avatar McGiverGim commented on May 23, 2024

@AgDude did you fix this? I have the same problem.

from i18next-http-backend.

McGiverGim avatar McGiverGim commented on May 23, 2024

Thanks for your help!!

from i18next-http-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.