i18next / i18next-localstorage-backend

This is a i18next cache layer to be used in the browser. It will load and cache resources from localStorage and can be used in combination with the chained backend.
MIT License
88 stars 43 forks source link

Namespaces added via addResourceBundle after init to localstorage/cache #43

Open cemalokten opened 1 year ago

cemalokten commented 1 year ago

Is it possible to store namespaces that are added after init via addResourceBundle in localstorage?

I asked the question on stackoverflow and have not had an answer.


Question posted on stackoverflow: https://stackoverflow.com/questions/74475045/addresourcebundle-after-init-and-cacheing

I am fetching namespaces for each component from a database that store JSON and adding them via addResourceBundle after init:

    // Component
          i18next.addResourceBundle('ar', 'topicNS', {
            topics,
          });

I would like to add these to the cache/localstorage. I am using the localStorageBackend to cache the static JSON resources, how can I add to the cached namespaces after init?

I have tried using the resourcesToBackend plugin, but am not sure how to make it work.

    // i18n.js
        backend: {
          backends: [
            LocalStorageBackend,
            HttpApi,
            resourcesToBackend((language, namespace, callback) => {
              // ???????
            }),
          ],
          loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json`,
        },
adrai commented 1 year ago

i18next-resources-to-backend is used mainly as fallback backend... for example if your http backend did not serve the translations... I don't know your use case but if your translations (those you are currently adding via addResourceBundle) are not lazy loaded i18next-resources-to-backend should work as expected, like described in the readme.

If that's not what you're looking for (but I really don't know why), you could directly invoke the save function of the local storage backend... but this is really a hack and not an official interface: i18next.services.backendConnector.backend.backends[0].save('ar', 'topicNS, { topics })