Internationalizing a React Application with Polyglot

17
I18N W2H R3T Internationalization With React Jonathan Petitcolas www.jonathanpetitcolas.com API Hours Clermont Ferrand, France 2017/03/27

Transcript of Internationalizing a React Application with Polyglot

Page 1: Internationalizing a React Application with Polyglot

I18N W2H R3T

Internationalization With React

Jonathan Petitcolas ­ www.jonathan­petitcolas.comAPI Hours ­ Clermont Ferrand, France ­ 2017/03/27

Page 2: Internationalizing a React Application with Polyglot

Jonathan Petitcolas

marmelab developer

Agility FTW!

Exciting technologies: Node, React, Go, etc.

Free for charity!

More details on: @marmelab / www.marmelab.com

Page 3: Internationalizing a React Application with Polyglot

Jonathan Petitcolas

Open­Source Contributor

admin­on­rest: marmelab/admin­on­rest

Page 4: Internationalizing a React Application with Polyglot

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"

Page 6: Internationalizing a React Application with Polyglot

5 different languages

Symfony3 for server, React for client

Server­side uses Twig trans filter (isomorphic is coming)

Client­side uses Airbnb Polyglot

Page 7: Internationalizing a React Application with Polyglot

Using Polyglot.js

import Polyglot from 'node­polyglot';

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 ));

Page 8: Internationalizing a React Application with Polyglot

What About a Real World App?

Page 9: Internationalizing a React Application with Polyglot

Cascading props? So boring...

Page 10: Internationalizing a React Application with Polyglot

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

Page 11: Internationalizing a React Application with Polyglot

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

Page 13: Internationalizing a React Application with Polyglot

Using Context via a ProviderDeclare getChildContext method,

Define childContextTypes ,

Render component children.

Page 14: Internationalizing a React Application with Polyglot

I Promised Dan to Write an HOC...But I don't even know what it is!

A higher­order component (HOC) is a function that takes acomponent and returns a new component.

const EnhancedComponent = hoc(Component);

Page 15: Internationalizing a React Application with Polyglot

Using recompose Recompose is a React utility belt for function components andhigher­order components. Think of it like lodash for React.

Source: recompose repository

Page 16: Internationalizing a React Application with Polyglot

Any Questions?

Page 17: Internationalizing a React Application with Polyglot

Thanks!GitHub repository: jpetitcolas/react­i18n­sample

Blog post (soon): How­to implement Polyglot on a React App?

Further questions: @Sethpolma