<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kazumi Karbowski</title>
    <description>The latest articles on DEV Community by Kazumi Karbowski (@codingmamakaz).</description>
    <link>https://dev.to/codingmamakaz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F57837%2F37c3588c-b525-4197-800c-f1492dce344e.jpg</url>
      <title>DEV Community: Kazumi Karbowski</title>
      <link>https://dev.to/codingmamakaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingmamakaz"/>
    <language>en</language>
    <item>
      <title>Internationalization with Rails and i18n-js</title>
      <dc:creator>Kazumi Karbowski</dc:creator>
      <pubDate>Tue, 06 Oct 2020 22:36:00 +0000</pubDate>
      <link>https://dev.to/codingmamakaz/internationalization-with-rails-and-i18n-js-1bjb</link>
      <guid>https://dev.to/codingmamakaz/internationalization-with-rails-and-i18n-js-1bjb</guid>
      <description>&lt;p&gt;According to Wikipedia, Internationalization (shorthand as i18n as there are exactly eighteen characters between the first “i” and the last “n”) is a process of designing a software application so that it can be adapted to various languages and regions without engineering changes.&lt;br&gt;
According to the Rails Guides, The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits.&lt;/p&gt;

&lt;p&gt;I was working on an app built with React on Rails. Most of the pages were written in React, but some pages were written in and rendered from the Rails views. This meant that I needed to figure out a way to provide translations on both the backend and the frontend.&lt;br&gt;
I searched for articles and tutorials, but I couldn’t find one that was solving the same problem I had. I found a lot of tutorials on “i18n-ing” Rails apps, and some on Rails and Javascript, but not on Rails and React.&lt;br&gt;
After spending some time researching, I decided to try &lt;a href="https://github.com/svenfuchs/rails-i18n" rel="noopener noreferrer"&gt;rails-i18n&lt;/a&gt; and &lt;a href="https://github.com/fnando/i18n-js" rel="noopener noreferrer"&gt;i18n-js&lt;/a&gt; gems.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails-i18n&lt;/code&gt; provides an easy-to-use and extensible framework for translating your app. &lt;code&gt;i18n-js&lt;/code&gt; is a small library to generate the Rails i18n translations on the JavaScript side. It provides a middleware that auto-generates Javascript files using the yml translation files from the Rails side.&lt;/p&gt;

&lt;p&gt;Using those gems seemed like a good way to solve the problem I had.&lt;br&gt;&lt;br&gt;
I would like to share how I did it.&lt;/p&gt;

&lt;p&gt;First, add the gems to your Gemfile.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

gem "i18n-js"
gem "rails-i18n"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Don't forget to install gems by running&lt;br&gt;
&lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you are using webpacker, run&lt;br&gt;
&lt;code&gt;npm install i18n-js&lt;/code&gt;&lt;br&gt;
If you are using asset pipeline, check out &lt;a href="https://github.com/fnando/i18n-js" rel="noopener noreferrer"&gt;this ReadMe&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Define available locales in &lt;code&gt;config/application.rb&lt;/code&gt;. In my case, I'm adding support for English and Japanese. &lt;br&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

config.i18n.available_locales = [:en, :ja]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can check all the locales available in the Rails console.&lt;br&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

I18n::JS.filtered_translations.keys
=&amp;gt; [:en, :ja]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now add English strings in &lt;code&gt;en.yml&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

en:
  hello: "Hello world"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can check to see if it's working in the rails console like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

I18n.t 'hello'
=&amp;gt; "Hello world"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's add Japanese translation in &lt;code&gt;ja.yml&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ja:
  hello: "こんにちは 世界"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is how we can access the translation in a slim file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

h1 = t('hello')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we got it working on the Rails side, let's move onto getting the translation on the React side.&lt;/p&gt;

&lt;p&gt;Add a middleware by adding this line&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

config.middleware.use I18n::JS::Middleware


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;to &lt;code&gt;config/application.rb&lt;/code&gt;. The middleware generates translation files on the frontend.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;rails generate i18n:js:config&lt;/code&gt;, which will generate &lt;code&gt;config/i18n-js.yml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is my configuration added to  &lt;code&gt;config/i18n-js.yml&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

translations:
- file: 'app/javascript/bundles/i18n/en.js'
  prefix: "import I18n from 'i18n-js';\n"
  pretty_print: true
  only: 'en.*'
- file: 'app/javascript/bundles/i18n/ja.js'
  prefix: "import I18n from 'i18n-js';\n"
  pretty_print: true
  only: 'ja.*'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;file&lt;/code&gt; is specifying the path of my choice for js translation file.&lt;br&gt;&lt;br&gt;
&lt;code&gt;prefix&lt;/code&gt; is optional, but without it, I was getting "I18n is not defined" error, and I couldn’t get it to work. It will add the line at the beginning of the resultant translation file.&lt;br&gt;&lt;br&gt;
&lt;code&gt;pretty_print&lt;/code&gt; is optional as well, but I definitely recommend putting this. It adds whitespace and indentation in your output file, which makes it so much easier to read the files.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;rake i18n:js:export&lt;/code&gt;&lt;br&gt;
This will generate translation files to your chosen paths.&lt;/p&gt;

&lt;p&gt;Add this to a react file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import i18n from 'i18n-js'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To set default locale and locale on the backend, I added this in &lt;code&gt;views/layouts/application.slim&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

- javascript_tag do
      I18n.locale = I18n.locale
      I18n.defaultLocale = I18n.default_locale


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And add this in &lt;code&gt;application.rb&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

config.i18n.default_locale = :ja



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To set them on the React side, you can add this inside the render (this set them to be in Japanese).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

I18n.defaultLocale = "ja"
I18n.locale = "ja"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;ja needs to be a string like &lt;code&gt;"ja"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, add we can access your Rails translations from React like so.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;h2&amp;gt;{I18n.t('hello')}&amp;lt;/h2&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When the locale is set to English, you'll see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcodingmamakaz%2Fimage%2Fupload%2Fv1602023343%2FScreen_Shot_2020-10-06_at_6.27.25_PM_oy5y05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcodingmamakaz%2Fimage%2Fupload%2Fv1602023343%2FScreen_Shot_2020-10-06_at_6.27.25_PM_oy5y05.png" alt="English hello world"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the locale is set to Japanese, you'll see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcodingmamakaz%2Fimage%2Fupload%2Fv1602023899%2FScreen_Shot_2020-10-06_at_6.38.01_PM_q0qiog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fcodingmamakaz%2Fimage%2Fupload%2Fv1602023899%2FScreen_Shot_2020-10-06_at_6.38.01_PM_q0qiog.png" alt="Japanese hello world"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, some of the translation's strings might be longer, which may cause you a lot of headaches dealing with css 😱&lt;/p&gt;

&lt;p&gt;I hope this post helps someone.&lt;br&gt;
Happy i18n-ing!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
      <category>i18n</category>
      <category>howisolved</category>
    </item>
  </channel>
</rss>
