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

Chaining HTTPBackend with LocalStorageBackend #13

Closed
abhijit945 opened this issue Jul 1, 2020 · 3 comments
Closed

Chaining HTTPBackend with LocalStorageBackend #13

abhijit945 opened this issue Jul 1, 2020 · 3 comments

Comments

@abhijit945
Copy link

abhijit945 commented Jul 1, 2020

🐛 Bug Report

A clear and concise description of what the bug is.

To Reproduce

A codesandbox example or similar
or at least steps to reproduce the behavior:

Assume System.resolve resolves to localhost:5000 via @libra/ui

import ChainedBackend from 'i18next-chained-backend';
import HTTPBackend from 'i18next-http-backend';
import LocalStorageBackend from 'i18next-localstorage-backend';

  const loadPath = (language: string, namespace: string) =>
    // eslint-disable-next-line no-undef
    System.resolve(`@libra/ui/locales/${language}/${namespace}.json`);

  const instance = i18n.createInstance();

  instance
    .use(ChainedBackend)
    .use(initReactI18next)
    .init({
      supportedLngs: ['ja-jp'],
      fallbackLng: 'en-us',
      ns: ['translation'],
      react: {
        wait: true,
        useSuspense: false,
      },
      backend: {
        backends: [LocalStorageBackend, HTTPBackend],
        backendOptions: [{ loadPath }],
      },
      keySeparator: false,
      lowerCaseLng: true,
    });

Expected behavior

There are 2 scenarios here:

  1. With ChainedBackend + HTTPBackend + LocalStorageBackend similar to here
    The network request that are made are http://localhost:8000/locales/ja-jp/translation.json instead (resulting in 404)

  2. With HTTPBackend only: With following changes:

instance
    .use(HTTPBackend)
    .use(initReactI18next)
    .init({
        supportedLngs: ["ja-jp"],
        fallbackLng: "en-us",
        ns: ["translation"],
        react: {
            wait: true,
            useSuspense: false,
        },
        backend: {
            loadPath,
        },
        keySeparator: false,
        lowerCaseLng: true,
    });

Can confirm this works: http://localhost:5000/locales/ja-jp/translation.json with a satisfying 200 OK!

Additional Debug information

For some reason the loadPath going into each plugin is different. The loadPath should be a function (which is only true to first plugin and not the second)

Screen Shot 2020-07-01 at 2 08 08 PM

Screen Shot 2020-07-01 at 2 08 22 PM

Your Environment

  • runtime version: i.e. node v12, browser chrome
  • i18next version: latest
  • os: Mac
@jamuhl
Copy link
Member

jamuhl commented Jul 1, 2020

might be you should pass backendOptions correctly...backendOptions: [{}, { loadPath }],

@abhijit945
Copy link
Author

Works! That does make sense! Thank you.

@adrai
Copy link
Member

adrai commented Jul 1, 2020

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project.

There are many ways to help this project 🙏

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