A simple translator for Reactjs load languages from json file.
In your react project, run one of these commands:
$ yarn add i18n-react-json
or
$ npm i i18n-react-json
Lets create your files respository, for example: "locales"
|--project
|--public
|--src
|--locales
|-- en.json
|-- pt.json
In the json file, for example:
// pt.json
{
"Hello" : "Olá",
"I am Gaspar": "Eu sou Gaspar"
}
In your App.js lets import and set the language.
//project/src/App.js
import Translate from "i18n-react-json";
import locales from "./locales";
// call tranlation
const i18n = new Translate(locales);
//set default language
i18n.setLocale("pt");
class App extends Component {
render() {
return (
<div className="App">
<p>{i18n.__("Hello")}, {i18n.__("I am Gaspar")}!</p>
</div>
);
}
export default App;
I also provided an example take a look or download it and run in your environment.