Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is I18n (Internationalization) in Ruby on Rails?

Internationalization, or I18n for short, is a concept that refers to the process of designing and implementing a software application in such a way that it can be easily adapted for various languages and regions without the need for re-engineering. In this blog post, we'll take a look at how I18n works in Ruby on Rails, a popular web application framework.

Before we dive in, let's break down the term "I18n." It's actually an abbreviation for "internationalization," with the number 18 representing the number of letters between the first "I" and the last "n." This abbreviation helps save space and avoids potential spelling errors.

Now that we know what I18n is, let's explore why it's important for your Ruby on Rails application.

Why I18n Matters

In today's globalized world, it's more important than ever for software applications to cater to users from different countries and cultures. This means that your application should be able to:

  1. Display content in different languages
  2. Format numbers, dates, and times according to local conventions
  3. Handle input from users in various languages

By implementing I18n in your Rails application, you make it more accessible and user-friendly for people from diverse backgrounds.

Getting Started with I18n in Ruby on Rails

Rails has built-in support for I18n, making it easy to internationalize your application. Let's go over the basics of how Rails handles internationalization.

Locales

In Rails, a locale is a set of key-value pairs that define translations and formatting rules for a specific language or region. You can think of a locale as a dictionary that helps Rails translate your application's content into different languages.

Locale files are stored in the config/locales directory of your Rails application. By default, Rails generates a locale file for English called en.yml.

Here's a simple example of a locale file for English:

en:
  hello: "Hello, world!"

And the equivalent file for Spanish:

es:
  hello: "¡Hola, mundo!"

In these examples, the top-level key (en or es) represents the language code, and the nested keys (e.g., hello) represent the translation keys.

Setting the Locale

To set the locale for your Rails application, you can use the I18n.locale method. By default, the locale is set to :en (English).

I18n.locale = :es  # Set the locale to Spanish

You can also configure your application to use a specific locale by default. To do this, add the following line to your config/application.rb file:

config.i18n.default_locale = :es  # Set the default locale to Spanish

Translating Text

Now that we have our locale files set up, we can use the t (or translate) helper method to display translated text in our views.

Let's say we have the following translation keys in our en.yml and es.yml files:

# en.yml
en:
  welcome_message: "Welcome to our website!"

# es.yml
es:
  welcome_message: "¡Bienvenido a nuestro sitio web!"

We can use the t helper method in our view like this:

<!-- app/views/home/index.html.erb -->
<h1><%= t('welcome_message') %></h1>

When the user's locale is set to English, they will see "Welcome to our website!" If their locale is set to Spanish, they will see "¡Bienvenido a nuestro sitio web!"

Localizing Dates, Numbers, and Currencies

In addition to translating text, Rails also provides helper methods for formatting dates, numbers, and currencies according to the user's locale.

For example, let's say you want to display today's date in your view. You can use the l (or localize) helper method like this:

<!-- app/views/home/index.html.erb -->
<p><%= l(Date.today) %></p>

This will display today's date formatted according to the user's locale. If the locale is set to English, they might see something like "February 14, 2023." If the locale is set to Spanish, they might see "14 de febrero de 2023."

Similarly, you can use the number_to_currency helper method to format currency values according to the user's locale:

<!-- app/views/products/show.html.erb -->
<p><%= number_to_currency(@product.price) %></p>

This will display the product's price formatted as a currency value, with the appropriate currency symbol and decimal separator for the user's locale.

Handling User Input

To handle user input in different languages, you may need to adjust your application's input handling and validation logic.

For example, if your application allows users to enter dates, you should ensure that the input is parsed correctly according to the user's locale. You can use the Date.strptime method with the appropriate format string for the user's locale:

# Parse a date string according to the user's locale
date = Date.strptime(params[:date], I18n.t('date.formats.default'))

Similarly, if your application accepts user input in languages with different character sets (e.g., Japanese or Arabic), you may need to update your database and text processing logic to handle these characters correctly.

Pluralization

When translating text that includes a count or quantity, you may need to handle pluralization rules for different languages.

Rails provides a flexible way to define pluralization rules in your locale files. You can use the pluralize helper method in your views, along with translation keys that include a count variable.

For example, let's say you want to display the number of comments on a blog post in your view. You can define the following translation keys in your en.yml and es.yml files:

# en.yml
en:
  comments:
    one: "1 comment"
    other: "%{count} comments"

# es.yml
es:
  comments:
    one: "1 comentario"
    other: "%{count} comentarios"

Then, you can use the pluralize helper method in your view like this:

<!-- app/views/posts/show.html.erb -->
<p><%= pluralize(@post.comments.count, t('comments')) %></p>

This will display the correct pluralization of the word "comment" based on the user's locale and the number of comments on the post.

Conclusion

Implementing I18n in your Ruby on Rails application is a crucial step towards making it accessible and user-friendly for a global audience. By using the built-in I18n support in Rails, you can easily translate your application's content, format dates, numbers, and currencies according to local conventions, and handle user input in different languages.

As you continue to develop your Rails application, keep in mind the importance of internationalization and the tools and techniques we covered in this blog post. Happy coding!