22
Internationalizing React Apps George Bukhanov github.com/northerneyes @notherneyes

Internationalizing react apps

Embed Size (px)

Citation preview

Page 1: Internationalizing react apps

Internationalizing React AppsGeorge Bukhanov

github.com/northerneyes

@notherneyes

Page 2: Internationalizing react apps

Localization was always difficult

Page 3: Internationalizing react apps

Even in different ecosystems● Windows forms applications,

● Asp.net sites,

● iOS development,

● Android

● etc

Page 4: Internationalizing react apps

React has no any special instruments

Page 5: Internationalizing react apps

Redux can help

Page 6: Internationalizing react apps

We can create a special intl reducer

Page 7: Internationalizing react apps

intl reducer, an exampleimport messages from './messages';import {Record} from 'immutable';

const InitialState = Record({ availableLanguages: ['en'], messages, selectedLanguage: 'en'});

const initialState = new InitialState;

const revive = state => initialState .mergeDeep(state) .update('messages', messages => messages.toJS());

export default function intlReducer(state = initialState) { if (!(state instanceof InitialState)) return revive(state);

return state;}

Page 8: Internationalizing react apps

Benefits ● Server rendering

● Changing locale without reloading the page

● Consistency

Page 9: Internationalizing react apps

But it leads to many problems

Page 10: Internationalizing react apps

We should pass msg object everywhereconst mapStateToProps = state => { return { msg: state.intl.messages[state.intl.selectedLanguage] };};

@connect(mapStateToProps)export default class JobsPage extends React.Component {

static propTypes = { msg: React.PropTypes.object.isRequired };

render() { const {msg} = this.props;

...

Page 11: Internationalizing react apps

Or make all components smart

Page 12: Internationalizing react apps

And more, we get big json file with all messages… export default { alerts: { title: 'Alerts', confirmDelete: "Are you sure you want to delete this Email alert?" },

alertCreateModal: { title: 'New Email Alert', notificationFrequency: 'How often do you want to receive emails?', save: 'Save Email Alert', cancel: 'Cancel', timeIntervals: { immediately: 'immediately', daily: 'daily', weekly: 'weekly', never: 'never' } },...

Page 13: Internationalizing react apps

Disadvantages● It’s hard to work with it● It’s hard to support it● It’s hard to translate it● Performace issue (smart components)● Or hanging prop (msg)● Formatting numbers, dates, and string messages?

Page 14: Internationalizing react apps

Disadvantages● It’s hard to work with it● It’s hard to support it● It’s hard to translate it● Performace issue (smart components)● Or hanging prop (msg)● Formatting numbers, dates, and string messages?

React context?

Page 15: Internationalizing react apps
Page 16: Internationalizing react apps

React Intl story beginsReact Intl is part of FormatJS. It provides bindings to React via its components and API.

Page 17: Internationalizing react apps

Really easy to useimport React from 'react';

import {IntlProvider} from 'react-intl';

ReactDOM.render(

<IntlProvider locale="en">

<App />

</IntlProvider>,

document.getElementById('container')

);

Page 18: Internationalizing react apps

We are ready to gorender() { const {name, unreadCount} = this.state; return ( <p> <FormattedMessage id="welcome" defaultMessage={`Hello {name}, you have {unreadCount, number} {unreadCount, plural, one {message} other {messages} }`} values={{name: <b>{name}</b>, unreadCount}} /> </p> );}

Page 19: Internationalizing react apps

Provides different formatters<FormattedDate

value={Date.now()}

day="numeric"

month="long"

year="numeric"

/>

<FormattedNumber

value={price.value}

style="currency"

currency="EUR"

/>

February 21, 2017

€1,000.00

Page 20: Internationalizing react apps

Provides different formatters<FormattedMessage id='app.greeting' description='Greeting to welcome the user to the app' defaultMessage='Hello, {name}!' values={{ name: 'Eric' }}/>

<FormattedRelative

value={Date.now()}

/>

…60 seconds later:<span>1 minute ago</span>

<span>Hello, Eric!</span>

Page 21: Internationalizing react apps

Demo

Page 22: Internationalizing react apps

Thank you!Link to demo:

github.com/northerneyes/react-intl-example

@notherneyes