Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement localization in reactjs?

Tags:

We need to implement the localization in reactjs to define the string value(s). How can I implement that?

One link is there https://www.npmjs.com/package/react-localization, but I am not getting the correct steps to add that.

I have tried by following steps:

  1. I am adding my component in ES6:
    class Home extends React.Component
    {
        constructor(props) {
            super(props);
        }
        render() {
            return (
              <Text>{strings.how}</Text>
             );
        }
    }

  1. I have added the localization code as:-
    import LocalizedStrings from 'react-localization';
    let strings = new LocalizedStrings({
        en:{
            how:"How do you want your egg today?",
            boiledEgg:"Boiled egg",
            softBoiledEgg:"Soft-boiled egg",
            choice:"How to choose the egg"
        },
        it: {
            how:"Come vuoi il tuo uovo oggi?",
            boiledEgg:"Uovo sodo",
            softBoiledEgg:"Uovo alla coque",
            choice:"Come scegliere l'uovo"
        }
    });

Now if you will see above :- {strings.how} I should able to get the strings value as it is defined in localization but I am not able to do it.

like image 300
Gorakh Nath Avatar asked Nov 22 '16 12:11

Gorakh Nath


2 Answers

npm install react-localization

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import LocalizedStrings from 'react-localization';

let strings = new LocalizedStrings({
  en:{
    how:"How do you want your egg today?",
    boiledEgg:"Boiled egg",
    softBoiledEgg:"Soft-boiled egg",
    choice:"How to choose the egg"
  },
  it: {
    how:"Come vuoi il tuo uovo oggi?",
    boiledEgg:"Uovo sodo",
    softBoiledEgg:"Uovo alla coque",
    choice:"Come scegliere l'uovo"
  }
 });

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      language: 'en'
    }
    
    this.handleLanguageChange = this.handleLanguageChange.bind(this);
  }

  handleLanguageChange(e) {
    e.preventDefault();
    let lang = e.target.value;
    this.setState(prevState => ({
      language: lang
    }))
  }

  render() {
    strings.setLanguage(this.state.language);
    return (
      <div>
        Change Language: <select onChange={this.handleLanguageChange}>
          <option value="en">En- English</option>
          <option value="it">It- Italian</option>
        </select>
        <br /><br />
        {strings.how}
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

u can put your language specific data in a JSON file or or .js file. call that file in your current file and pass that object to new LocalizedStrings().

Example: data.js

const data = {
  en:{
    how:"How do you want your egg today?",
    boiledEgg:"Boiled egg",
    softBoiledEgg:"Soft-boiled egg",
    choice:"How to choose the egg"
  },
  it: {
    how:"Come vuoi il tuo uovo oggi?",
    boiledEgg:"Uovo sodo",
    softBoiledEgg:"Uovo alla coque",
    choice:"Come scegliere l'uovo"
  }
}
export {data};

in your current file import it as import { data } from './data.js'; and then you can initialise as let strings = new LocalizedStrings({data});

You can read the detailed article here - react-localization with hooks

Check the working demo Here

like image 61
Raj Rj Avatar answered Oct 11 '22 15:10

Raj Rj


Yahoo has created a package for implementing localization in React that might be what you are looking for: https://github.com/yahoo/react-intl. It takes care of "dates, numbers, and strings, including pluralization and handling translations".

Update 2021: Here's the package now days:

  • https://github.com/formatjs/formatjs
  • https://www.npmjs.com/package/react-intl
like image 42
Oskar Avatar answered Oct 11 '22 14:10

Oskar