0

I'm learning to code in React from Tyler Mcginnis' React course (which I strongly recommend btw) and I decided to develop my own project, a university administration website, which can be displayed in different languages.

So far, I have developed the Login page, please note that I'm using Babel:

Login.js

import React from 'react'
import PropTypes from 'prop-types'
import { languagedata } from './Languages'
import languagesdata from '../languagesdata.json'

function LanguagesNav ({ selected, onUpdateLanguage}) {
  const languages = ['EU', 'ES', 'EN']

  return (
    <div >
      <h1 className='center-text header-lg'>
        GAUR 2.0
      </h1>
      <ul className='flex-center'>
        {languages.map((language) => (
          <li key={language}>
            <button
              className='btn-clear nav-link'
              style={language === selected ? { color: 'rgb(187, 46, 31)' } : null }
              onClick={() => onUpdateLanguage(language)}>
              {language}
            </button>
          </li>
        ))}
      </ul>
    </div>
  )
}

LanguagesNav.propTypes = {
  selected: PropTypes.string.isRequired,
  onUpdateLanguage: PropTypes.func.isRequired
}

export default class Login extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      selectedLanguage: 'EU'
    }

    this.updateLanguage = this.updateLanguage.bind(this)
  }
  componentDidMount () {
    this.updateLanguage(this.state.selectedLanguage)
  }
  updateLanguage (selectedLanguage) {
    this.setState({
      selectedLanguage
    })
  }
  render() {
    const { selectedLanguage } = this.state
    return (
      <React.Fragment>
        <LanguagesNav
          selected={selectedLanguage}
          onUpdateLanguage={this.updateLanguage}
        />
        <form className='column player'>
          <label htmlFor='username' className='player-label'>
            //Just testing whether JSON can be displayed
            { languagesdata.data }
          </label>
          <div className='row player-inputs'>
            <input
              type='text'
              id='username'
              className='input-light'
              placeholder='Erabiltzailea'
              autoComplete='off'
            />
          </div>
          <div className='row player-inputs'>
            <input
              type='password'
              id='username'
              className='input-light'
              placeholder='Pasahitza'
              autoComplete='off'
            />
          </div>
          <div className='row player-inputs'>
            <button
              className='btn dark-btn'
              type='submit'
            >
              Sartu
            </button>
          </div>
        </form>
      </React.Fragment>
    )
  }

}

website login interface

I would like to display the website in the selected language based on the information stored in a local JSON file:

 [
  {
    "EU": {
      "welcome": "Sartu GAUR 2.0ra",
      "login": "Sartu"
    },
    "ES": {
      "welcome": "Entra a GAUR 2.0",
      "login": "Entrar"
    },
    "EN":{
      "welcome": "Log into GAUR 2.0",
      "login": "Log in"
    }
  }
]

I've tried several solutions that I've found on the Internet, but none of them worked for me.

I've tried importing a Javascript file that contains a JSON data (see below) and importing a JSON file itself (see JSON above).

const languagedata = {
  "data": {
    "languages": [
      {
        "euskara": {
          welcome: "Sartu GAUR 2.0ra",
          login: "Sartu"
        },
        "español": {
          welcome: "Entra a GAUR 2.0",
          login: "Entrar"
        },
        "english":{
          welcome: "Log into GAUR 2.0",
          login: "Log in"
        }
      }
    ]
  }
};

Thus, I have 2 questions:

  • What would be the best way to do what I'm trying to do?
  • How can I display the JSON data in Login.js?
2
  • It's already answered here. Please have a look. Commented Aug 23, 2019 at 11:25
  • Thanks for your answer, I'm already importing the file using json-loader as specified in the link, however, I'm not sure how to display the data in Login.js. Any idea? Commented Aug 23, 2019 at 12:02

3 Answers 3

1

To get data from json file you have to make a request to that file.

class App extends React.Component{

  render(){
    return(
      <div>geting json data</div>
    );
  }
  
  componentDidMount(){
    fetch('YOUR-JSON-FILE-PATH')
      .then(response => response.json())
      .then(json => console.log(json))
  }
}

ReactDOM.render(<App/>,document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

OR You can use a synchronous way,

const data = require("YOUR-JSON-FILE-PATH");

Hope it can help!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, however, correct me if I'm wrong but I think this wouldn't apply as you're fetching the data from a web repository.
1

I think you're only missing export keyword in your Languages.js file:

export const languagedata = {
  "data": {
    "languages": [
      {
        "euskara": {
          welcome: "Sartu GAUR 2.0ra",
          login: "Sartu"
        },
        "español": {
          welcome: "Entra a GAUR 2.0",
          login: "Entrar"
        },
        "english":{
          welcome: "Log into GAUR 2.0",
          login: "Log in"
        }
      }
    ]
  }
};

Now you should be able to import variable languagedata in your React component using

import { languagedata } from './Languages'

3 Comments

Thanks for your answer, I've tried that but I don't see any of the data. Perhaps I'm not calling the data correctly from Login.js?
You definitely have Language.js file in same directory as is your Login.js file? Are you using webpack as a development server?
Yes, I do have them in the same directory, and yes, I am using webpack as a development server.
1

You need to have json-loader module. If you're using create-react-app, it will come by default.

Please have a look here.

1 Comment

Thanks for your answer, I'm already importing the file using json-loader as specified in the link, however, I'm not sure how to display the data in Login.js. Any idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.