Working with Internationalization

Episode #68 by Teacher's Avatar David Kimura

Summary

Adding translations into your website does not have to be hard. With Rails built in I18N functionality, we gain access to many helpers and conventions that makes it a much easier task. Learn how to add I18N into your Rails application.
rails i18n view 13:59

Resources

Summary

# config/application.rb
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.i18n.available_locales = [:en, :ja]
    config.i18n.default_locale = :en
  end

# routes.rb
  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    resources :users
    root 'visitors#index'
  end

# application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :set_locale

  private

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
    # current_user.locale
    # request.env["HTTP_ACCEPT_LANGUAGE"]
  end

  def default_url_options(options = {})
    { locale: I18n.locale }.merge options
  end
end

# config/locales/en.yml
---
en:
  activerecord:
    attributes:
      user:
        email: Email@
        first_name: First@Name
        last_name: Last@Name
    model:
      user: Users
  back: Back
  date:
    formats:
      default: "%m/%d/%Y"
      short: "%m/%d"
  edit: Edit
  greetings: Hello guest!
  visitors:
    index:
      greetings: Hello guest!

# _navigation_links.html.erb
<%= content_tag :li do %>
  <%= link_to User.model_name.human, users_path %>
<% end %>

<%= content_tag :li do %>
  <%= link_to 'Switch to English', locale: :en %>
<% end unless current_page?(locale: :en) %>

<%= content_tag :li do %>
  <%= link_to 'Switch to Japanese', locale: :ja %>
<% end unless current_page?(locale: :ja) %>

# different translations used
<%= t '.greetings' %>
<%= l Date.today %
<%= l Date.today, format: :short %>
<%= User.model_name.human %>
<%= User.human_attribute_name(:first_name) %>
<%= User.human_attribute_name(:last_name) %>
<%= User.human_attribute_name(:email) %>
<%= link_to t(:edit), edit_user_path(@user) %>
<%= link_to t(:back), users_path %>


# i18n-tasks
gem install i18n-tasks

# common commands
i18n-tasks health | missing | unused
i18n-tasks add-missing
i18n-tasks normalize

# sample output

Added 7 keys
+--------+-----------------------------------------+--------------+
| Locale | Key                                     | Value        |
+--------+-----------------------------------------+--------------+
|   ja   | activerecord.attributes.user.email      | Email@       |
|   ja   | activerecord.attributes.user.first_name | First@Name   |
|   ja   | activerecord.attributes.user.last_name  | Last@Name    |
|   ja   | activerecord.model.user                 | Users!       |
|   ja   | back                                    | Back         |
|   ja   | edit                                    | Edit         |
|   ja   | visitors.index.greetings                | Hello guest! |
+--------+-----------------------------------------+--------------+