Internationalizing a React Application with Polyglot
-
Upload
jonathan-petitcolas -
Category
Engineering
-
view
89 -
download
5
Transcript of Internationalizing a React Application with Polyglot
I18N W2H R3T
Internationalization With React
Jonathan Petitcolas www.jonathanpetitcolas.comAPI Hours Clermont Ferrand, France 2017/03/27
Jonathan Petitcolas
marmelab developer
Agility FTW!
Exciting technologies: Node, React, Go, etc.
Free for charity!
More details on: @marmelab / www.marmelab.com
Jonathan Petitcolas
OpenSource Contributor
adminonrest: marmelab/adminonrest
What Is Internationalization?
Translating sentences using dictionnary
"Hello world!" FR> "Bonjour le monde !"
Translating date and currencies format
"$199 on 2016/30/10" FR> "199 € le 30/10/2016"
Translating numbers
"14,910.45 units" FR> "14 910,45 unités"
5 different languages
Symfony3 for server, React for client
Serverside uses Twig trans filter (isomorphic is coming)
Clientside uses Airbnb Polyglot
Using Polyglot.js
import Polyglot from 'nodepolyglot';
const locale = 'fr';const phrases = fullscreen: 'Voir en plein écran', views: '%smart_count vue |||| %smart_count vues',;
const polyglot = new Polyglot( locale, phrases );
// Voir en plein écranconsole.log(polyglot.t('fullscreen');
// 42 vuesconsole.log(polyglot.t('views', smart_count: 42 ));
What About a Real World App?
Cascading props? So boring...
Context to the Rescue!In some cases, you want to pass data through the componenttree without having to pass the props down manually at everylevel. You can do this directly in React with the powerful"context" API.
Source: React official docs
Context is Experimental!If you want your application to be stable, don't use context. Itis an experimental API and it is likely to break in futurereleases of React.
Source: React (same) official docs
Source: @dan_abramov
Using Context via a ProviderDeclare getChildContext method,
Define childContextTypes ,
Render component children.
I Promised Dan to Write an HOC...But I don't even know what it is!
A higherorder component (HOC) is a function that takes acomponent and returns a new component.
const EnhancedComponent = hoc(Component);
Using recompose Recompose is a React utility belt for function components andhigherorder components. Think of it like lodash for React.
Source: recompose repository
Any Questions?
Thanks!GitHub repository: jpetitcolas/reacti18nsample
Blog post (soon): Howto implement Polyglot on a React App?
Further questions: @Sethpolma