i18next / i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
MIT License
451 stars 69 forks source link

Loading Localizations - Multiple Namespaces, Single File #157

Closed burnhamrobertp closed 2 weeks ago

burnhamrobertp commented 2 weeks ago

Documentation issue

It isn't clear (or perhaps isn't possible...although this would be odd?) whether there is a way to configure the http-backend to fetch localizations on a per-language basis, rather than on a per-namespace basis.

Using something like the following will work, but it will send off multiple requests (one for each namespace) as that's typically how i18next is utilized:

const Localizations = {};

// ...

backend: {
  loadPath: '/locale/{{lng}}.json',
  parse: (file, language, namespace) => {
    const data = Localizations[language] ?? JSON.parse(file);

    if (!Localizations[language]) {
      Localizations[language] = data;
    }

    return data[namespace];
  },
}

Unfortunately, this codebase predates quite a bit of best-practices in this regard, and while migrating the namespaces into their own files could work, its not ideal. It occurs to us that using request we might be able to achieve this outcome ourselves, but it seems peculiar that it would be so involved. Any assistance would be appreciated; I'm not certain this is even a documentation/configuration issue with i18next-http-backend; there might be a setting in i18next that we can't find; or might not exist at all

adrai commented 2 weeks ago

Did you try: https://github.com/i18next/i18next-multiload-backend-adapter ?

burnhamrobertp commented 2 weeks ago

No, I'll check that out. Its documentation seems a bit sparse but I'll give it a try

burnhamrobertp commented 2 weeks ago

If you have any idea as to how to structure the use if i18next-http-backend (notably the parse function) in combination with multiload backend, that would be helpful. I do think that multiload is closer to being a proper solution that what we were trying to accomplish before, so I appreciate the suggestion.

Presently, no matter what I return from the parse function in http-backend I don't actually get any localizations loaded for namespaces (even though i18next thinks they're loaded) image

adrai commented 2 weeks ago
{
  en: {
    account: {
      my: 'key'
    },
    analytics: {
      my: 'key in another ns'
    }
  },
  de: {
    account: {
      my: 'key in de'
    },
    analytics: {
      my: 'key in de in another ns'
    }
  }
}
burnhamrobertp commented 2 weeks ago

Thanks, I do see now that this is even addressed in the http-backend readme, just a little too subtle for me to have noticed initially