0

I have locale storage values that I would like to change but the problem is the values are object and I want to change "lang" property inside depends on the language preference of user.

That is how my local storage looks like Key (_I18N_config) and value {"lang":"en","JSON":{"All fields are required": "You have to fill in all fields"}}

If it was not in this type and it was just language string inside like "en" I would use localStroge.removeItem("_I18N_config") and then localStorage.setItem("_I18N_config","ger") to change language options. But I can not figure out how to react lang property to use this approach.

I am using it in React.js by the way. Thanks in advance!

Evren
  • 4,147
  • 1
  • 9
  • 16

1 Answers1

1

The MDN documentation specifies that the SetItem function accept à DOMString in argument. So maybe you can try to convert that object in json format ?

this function JSON.stringify will convert your JS Object in a string type, then you will be able to save it in your localStorage.

like so :

let _I18N_config = {
  lang: "ger", 
  JSON: {
   // your translations data
  }
}

localStorage.saveItem(JSON.stringify(_I18N_config))
Maxime Oger
  • 160
  • 1
  • 8