stefalda / react-localization

Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module.
MIT License
373 stars 58 forks source link

SetContent not setting content #105

Closed cmsamo closed 4 years ago

cmsamo commented 4 years ago

I set up a local string variable populated by a strings.json file in my app:

const localAppStrings = require('./data/strings.json'); let tempStrings = new LocalizedStrings(localAppStrings);

In my component state I add an object called AppStrings to hold the contents of a remote JSON file.

`constructor(props) { super(props);

this.state = {
  connection_Status: null,
  isLoading: true,
  stringsLoaded: false,
  appStrings: [],
  deviceLocale: this.GetPlatformAndLocale(),
};`

In my componentwillmount I make an API call to load some remote strings and I set the language of tempStrings here

componentDidMount() { // get the remote string data this.GetData(); //set the appStrings state inside GetData so we can use it when we render tempStrings.setLanguage(this.state.deviceLocale); }

When I come to render the data, I try and set content to use the new remote string:

`render() { const {connection_Status, isLoading, appStrings, deviceLocale, stringsLoaded} = this.state;

if (connection_Status === 'Online' && isLoading === false) {
  //console.log('---render---appStrings:' + JSON.stringify(appStrings));
  var tempJsonStrings = JSON.stringify(appStrings);
  console.log('---render---tempJsonStrings: ' + tempJsonStrings)
  tempStrings.setContent({tempJsonStrings});

  // set the language on the local copy of the loaded strings
  console.log("---render---deviceLocale: "+ deviceLocale);
  console.log("---render---tempstrings---appString:" + tempStrings.appInfoString)`

For some reason. tempStrings.setContent is not using the new strings from my state. Console.log shows me that the appStrings are what I expect them to be, but when I apply them to tempStrings and then log tempStrings, the data is the local data - it is never overwritten.

Am I doing something in the wrong order?