<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Internationalization on Janik von Rotz</title>
    <link>https://janikvonrotz.ch/tags/internationalization/</link>
    <description>Recent content in Internationalization on Janik von Rotz</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 02 Mar 2017 12:51:10 +0000</lastBuildDate>
    <atom:link href="https://janikvonrotz.ch/tags/internationalization/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Make your Redux React app multilingual</title>
      <link>https://janikvonrotz.ch/2017/03/02/make-your-redux-react-app-multilingual/</link>
      <pubDate>Thu, 02 Mar 2017 12:51:10 +0000</pubDate>
      <guid>https://janikvonrotz.ch/2017/03/02/make-your-redux-react-app-multilingual/</guid>
      <description>&lt;p&gt;For my current React app in development I&amp;rsquo;m using Redux to manage the client state. As this is the first time using Redux I&amp;rsquo;m not quite sure what to put in the store yet. But I&amp;rsquo;ve decided to give the multilingual labels and wordings a try. Below you&amp;rsquo;ll find a simple approach on how I made my app multilingual using the Redux client state to store the translated content. I assume you are familiar with Redux and React.&lt;/p&gt;&#xA;&lt;p&gt;The &lt;code&gt;i18n&lt;/code&gt; state reducer contains all translation and puts the default language content into the store.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;reducers/i18n.js&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;let phrases = {&#xA;  de: {&#xA;    button: {&#xA;      remove: &amp;#39;Löschen&amp;#39;,&#xA;      update: &amp;#39;Speichern&amp;#39;,&#xA;      commit: &amp;#39;Neue Version&amp;#39;,&#xA;      add_router: &amp;#39;Router hinzufügen&amp;#39;,&#xA;      search: &amp;#39;Suche&amp;#39;,&#xA;      cancel: &amp;#39;Abrechen&amp;#39;,&#xA;    }&#xA;  },&#xA;  en: {&#xA;    button: {&#xA;      remove: &amp;#39;Delete&amp;#39;,&#xA;      update: &amp;#39;Save&amp;#39;,&#xA;      commit: &amp;#39;Commit&amp;#39;,&#xA;      add_router: &amp;#39;Add Router&amp;#39;&#xA;    }&#xA;  }&#xA;}&#xA;&#xA;export default (state = phrases.de, action) =&amp;gt; {&#xA;  switch (action.type) {&#xA;    case &amp;#39;SWITCH_LANGUAGE&amp;#39;:&#xA;      return phrases[action.language]&#xA;    default:&#xA;      return state&#xA;  }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To get f.g. the translated button labels simply map the &lt;code&gt;i18n&lt;/code&gt; state as prop on the component.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;components/RouterSearch.js&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;...&#xA;import React from &amp;#39;react&amp;#39;&#xA;import { connect } from &amp;#39;react-redux&amp;#39;&#xA;&#xA;class RouterSearch extends React.Component {&#xA;&#xA;  ...&#xA;&#xA;  render() {&#xA;    let { i18n } = this.props&#xA;&#xA;    return &amp;lt;Card&amp;gt;&#xA;      &amp;lt;CardText&amp;gt;&#xA;&#xA;        &amp;lt;TextField&#xA;        floatingLabelText={ i18n.button.search }&#xA;&#xA;        ...&#xA;&#xA;      &amp;lt;/CardText&amp;gt;&#xA;    &amp;lt;/Card&amp;gt;&#xA;  }&#xA;}&#xA;&#xA;const mapStateToProps = (state) =&amp;gt; {&#xA;  return {&#xA;    i18n: state.i18n,&#xA;  }&#xA;}&#xA;export default connect(mapStateToProps)(RouterSearch)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Switching the language is easily done by setting the &lt;code&gt;language&lt;/code&gt; state.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;actions.js&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;...&#xA;export const switchLanguage = (language) =&amp;gt; {&#xA;  return {&#xA;    type: &amp;#39;SWITCH_LANGUAGE&amp;#39;,&#xA;    language&#xA;  }&#xA;}&#xA;...&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Obviously the are a lot cons to this method. First, there is no fallback for not translated content as you access to &lt;code&gt;i18n&lt;/code&gt; state directly. Second, putting multilingual content into the store is probably not a good idea in the first place. What do you think? Are there better options?&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
