Internationalization I18n in Ruby on Rails

In this Article we are going to built simple rails simple application to demonstrate the features of Rails Internationalization (I18n). We will cover very basic aspect of Internationalization.

What is Internationalization?

Internationalization means developing the application that can adapt any language easily without making any complex changes. Internationalization can be apply on string, date and currency format of the rails application web page.

Let’s build a demo Rails App with I18n

We are using I18 since the early version rails 2.2. It provides the framework that makes so easy to translate the App in various languages. Let’s create one demo. Apply the following steps as written and you will get the nice demo app at the end of this article.

Step:1 Create rails application using below command and I am using rails 5.2 version

rails new tech-internationalization

Step2: Go inside directory and bundle install

cd tech-internationalization

bundle install

Step3: Generate controller

rails generate controller Welcome index

Step4: Create the CRUD using following command

rails g scaffold User name email

Step5: Migrate database and start rails server

rails db:migrate

rails s

Step6: Configuration for I18

Gem “rails-i18n” use for internationalization

Gem “httparty” use for third party API

Step7: Install the bundle and restart the server

How to store the Translations?

Inside config/locales

There is one default file en.yml and you may create other file for each language

How to use this string in header html file?

t ‘header.menu.home’

Create custom message with your text

t ‘header.menu.service’my-name:’techcompose’

Change the language

http://localhost:3000/en/welcomes

http://localhost:3000/hi/welcomes

And inside application controller set locale param

How to switch language from html?

_header.html.erb

<%= link_to ‘English’, url_for(locale: :en), class: “dropdown-item” %>
<%= link_to ‘Russisch’, url_for(locale: :ru), class: “dropdown-item” %>
<%= link_to ‘हिंदी’, url_for(locale: :hi), class: “dropdown-item” %>
<%= link_to ‘Español’, url_for(locale: :es), class: “dropdown-item” %>
<%= link_to ‘Nederlands’, url_for(locale: :nl), class: “dropdown-item” %>

Contact Ruby on Rails Development Company to develop your Business mobile app or web application with elegant design. Contact us to Hire dedicated ROR developer today or reach us at inquiry@techcompose.com for any assistance regarding ROR development requirement.

0 Comments
Urjit Rajgor

Do You Have Any Interesting Project In Mind? Let`s Discuss