Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing in instantiated backends #3

Closed
vjpr opened this issue Jul 26, 2018 · 2 comments
Closed

Allow passing in instantiated backends #3

vjpr opened this issue Jul 26, 2018 · 2 comments

Comments

@vjpr
Copy link

vjpr commented Jul 26, 2018

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
Copy link
Member

jamuhl commented Jul 27, 2018

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
Copy link

gorhawk commented Nov 15, 2018

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);
...

@jamuhl jamuhl closed this as completed Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants