Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  38] [ 2]  / answers: 1 / hits: 16897  / 8 Years ago, mon, july 4, 2016, 12:00:00

We want to have our backoffice for the main site as a multilingual solution for our users. We have already decided to use React + Redux for it, as it makes a lot of sense to use the already deployed API for several functionalities such as authorization and data fetching ..



I used a custom approach in the past, but it's so complex and maybe we are missing a best practices method here. The main site is already in 4 languages, and soon to grow into others.



I've taken a look at some of the ongoing libs, such as React-intl (https://github.com/yahoo/react-intl) and Airbnb Polyglot (http://airbnb.io/polyglot.js/)



What would be the best approach/lib/solution for building a multilingual site in React? (just on front-end, not an isomorphic app thou)


More From » reactjs

 Answers
16

You can use redux-polyglot to easily use Airbnb's Polyglot in a React/Redux application.
(Note: I'm one of the authors)



It provides :




  • a reducer to store language and corresponding messages. You can supply both by either :


    • a middleware that you can configure to catch specific action, deduct current language and get/fetch associated messages.

    • direct dispatch of setLanguage(lang, messages)


  • a getP(state) selector that retrieves a P object that exposes 4 methods :


    • t(key): original polyglot T function

    • tc(key): capitalized translation

    • tu(key): upper-cased translation

    • tm(morphism)(key): custom morphed translation


  • a getLocale(state)selector to get current language

  • a translate higher order component to enhance your React components by injecting the p object in props



Simple usage example :



dispatch new language :



import setLanguage from 'redux-polyglot/setLanguage';

store.dispatch(setLanguage('en', {
common: { hello_world: 'Hello world' } } }
}));


in component :



import React, { PropTypes } from 'react';
import translate from 'redux-polyglot/translate';

const MyComponent = props => (
<div className='someId'>
{props.p.t('common.hello_world')}
</div>
);
MyComponent.propTypes = {
p: PropTypes.shape({t: PropTypes.func.isRequired}).isRequired,
}
export default translate(MyComponent);


Please tell me if you have any question/suggestion !


[#61524] Friday, July 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylor

Total Points: 334
Total Questions: 100
Total Answers: 111

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
;