{"size": 1756, "ext": "js", "max_stars_repo_path": "app/javascript/snap/components/withTranslation.js", "max_stars_repo_name": "BarnesFoundation/Barnes-Focus", "max_stars_repo_stars_event_min_datetime": "2020-05-28T15:04:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-17T17:55:43.000Z", "max_issues_repo_path": "app/javascript/snap/components/withTranslation.js", "max_issues_repo_name": "BarnesFoundation/Barnes-Focus", "max_issues_repo_issues_event_min_datetime": "2019-11-07T01:17:10.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-12T02:57:58.000Z", "max_forks_repo_path": "app/javascript/snap/components/withTranslation.js", "max_forks_repo_name": "BarnesFoundation/Barnes-Focus", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 5, "max_issues_count": 20, "max_forks_count": null, "avg_line_length": 27.4375, "max_line_length": 75, "alphanum_fraction": 0.6127562642}
import React from "react";
import { SearchRequestService } from "../services/SearchRequestService";
import { SNAP_LANGUAGE_TRANSLATION } from "./Constants";

const withTranslation = WrappedComponent => {
  return class WithTranslation extends React.Component {
    constructor(props) {
      super(props);

      this.sr = new SearchRequestService();
      this.state = {
        translations: null,
        loaded: false
      };
    }

    async componentWillMount() {
      console.log(
        "WithTranslation >> componentWillMount. Load the translations here"
      );
      const translations = await this.sr.getAppTranslations();
      this.setState({ translations: translations, loaded: true });
      localStorage.setItem(
        SNAP_LANGUAGE_TRANSLATION,
        JSON.stringify(translations)
      );
    }

    updateTranslations = async () => {
      console.log(
        "WithTranslation >> updateTranslations. Update translations."
      );
      const translations = await this.sr.getAppTranslations();
      this.setState({ translations: translations, loaded: true });
      localStorage.setItem(
        SNAP_LANGUAGE_TRANSLATION,
        JSON.stringify(translations)
      );
    };

    getTranslation = (screen, textId) => {
      return (
        this.state.translations[screen][textId].translated_content ||
        this.state.translations[screen][textId].screen_text
      );
    };

    render() {
      return (
        <div>
          {this.state.loaded && (
            <WrappedComponent
              {...this.props}
              getTranslation={this.getTranslation}
              updateTranslations={this.updateTranslations}
            />
          )}
        </div>
      );
    }
  };
};
export default withTranslation;
