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>
)
}
}
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?