i18next / i18next-chained-backend

An i18next backend to chain multiple backends (add fallbacks, caches, ...)
MIT License
69 stars 10 forks source link

Allow passing in instantiated backends #3

Closed vjpr closed 5 years ago

vjpr commented 6 years ago

If I instantiate my backend, this plugin doesn't work.

Not sure if this is necessary to pass in opts and constructors separately by design. If so, add to readme.

jamuhl commented 6 years ago

Did you add other backends to it? The backend does only chain other backends to eg. support a caching backend before doing a call via xhr backend: https://github.com/i18next/i18next-chained-backend#more-complete-sample

gorhawk commented 6 years ago

Did you try to init the chained backend instead of passing it to i18next init? It works like this, you just have to pass the options as the second parameter as I have referenced in this issue

import i18next from "i18next";
import XHR from "i18next-xhr-backend";
import ChainedBackend from 'i18next-chained-backend';
import LocalStorageBackend from 'i18next-localstorage-backend';

const xhr = new XHR();

xhr.init(null, {
    loadPath: function(language, namespace) {
        return Routing.generate("api_locales", {language, namespace});
    },
    // allow credentials on cross domain requests
    withCredentials: false
});

const localStorageBackendOptions = {
    // prefix for stored languages
    prefix: 'i18next_res_',
    // expiration
    expirationTime: 60*1000,
    // language versions
    versions: {}
};

const chainedBackendOptions = {
    backends: [
        LocalStorageBackend,
        xhr,
    ],

    // array of options in order of backends above
    backendOptions: [
        localStorageBackendOptions
    ]
};

const chainedBackend = new ChainedBackend();
chainedBackend.init(null, chainedBackendOptions); // <- pass options as second

i18next.use(chainedBackend);
...