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

Versions doco #3

Closed dcworldwide closed 5 years ago

dcworldwide commented 6 years ago

Could you please expand on your explanation of the versions option? I don't really understand how it's supposed to be used.

I want to know how to invalidate the local storage cache of my user when releasing a new version of my translations. How can I do that?

Thanks

jamuhl commented 6 years ago

change the value of version - that will invalidate the cache

jamuhl commented 6 years ago

it will only return cached value if version is same: https://github.com/i18next/i18next-localstorage-backend/blob/master/src/index.js#L62

dcworldwide commented 6 years ago

Ok i think I get it. Better docs wouldn't hurt.

adrai commented 6 years ago

@dcworldwide would you like to share your feedback/addition for the documentation with us? Or even better, submit a PR? Would be very nice.

dcworldwide commented 6 years ago

Once I get it working ok. What is missing is an example invalidation scenario.

CloudPower97 commented 5 years ago

Hello there, I've the same problem as described by @dcworldwide, I'm not able to invalidate the cache, both using versions alone and in conjunction with the expirationTime set to 0. How can one correctly invalidate the cache after changing the translation files? Thanks in advance

jamuhl commented 5 years ago

@CloudPower97 it's just change the versions

in code it was: versions: { en: 'v1.2', fr: 'v1.1' }

change eg: versions: { en: 'v1.3', fr: 'v1.1' } --> english will be not loaded from cache

CloudPower97 commented 5 years ago

@jamuhl Already tried, it doesn't work...

This is my i18n.js file:

import i18n from 'i18next'
import detector from 'i18next-browser-languagedetector'
import backend from 'i18next-chained-backend'
import LocalStorageBackend from 'i18next-localstorage-backend' // primary use cache
import XHR from 'i18next-xhr-backend' // fallback xhr load
import { reactI18nextModule } from 'react-i18next'

i18n
  .use(detector)
  .use(backend)
  .use(reactI18nextModule) // passes i18n down to react-i18next
  .init({
    backend: {
      backends: [LocalStorageBackend, XHR],
      backendOptions: {
        prefix: 'i18next_res_',
        versions: {
          en: 'v1.0',
          it: 'v1.0',
        },
      },
    },
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // react already safes from xss
    },
    whitelist: ['en', 'it'],
    ns: ['common', 'homepage'],
    defaultNS: 'common',
  })

export default i18n

Here's what I've done:

  1. Update it translation file in public/locales/it/
  2. Bump the it version in i18n.js file above from v1.0 to v1.1

Expected behavior : I can see the new updated translation because the cache is now invalidated Current behavior: I can't see the new updated translation because the cache is NOT invalidated.

Am I doing something wrong?

As stated before I also tried adding expirationTime: 0 to force somehow the invalidation, but this didn't the trick.

Thank you for your time

jamuhl commented 5 years ago

https://github.com/i18next/i18next-chained-backend#backend-options backend options is an array per backend in chained backend...

CloudPower97 commented 5 years ago

@jamuhl Whoops, completely missed it! Thank you, everything is working fine now!

jamuhl commented 5 years ago

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 :pray: