Working with Internationalization

#68 Working with Internationalization
2/19/2017

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.
10
rails i18n view 13:59 min

Resources

Rails Guide - http://guides.rubyonrails.org/i18n.html
i18n-tasks gem - https://github.com/glebm/i18n-tasks
Source - 

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.rbclass 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: [email protected]
        last_name: [email protected]
    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-tasksgem 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 | [email protected]   |
|   ja   | activerecord.attributes.user.last_name  | [email protected]    |
|   ja   | activerecord.model.user                 | Users!       |
|   ja   | back                                    | Back         |
|   ja   | edit                                    | Edit         |
|   ja   | visitors.index.greetings                | Hello guest! |
+--------+-----------------------------------------+--------------+

216339?v=3&s=64
glebm said 5 months ago:

  1. Add the `i18n-tasks-use` comments where the unrecognized translations are used (such as the date and activerecord ones).
    This way your unused list will correspond to the keys that are not actually used, and you can keep your files clean by using the `remove-unused` task.
  2. Using `activerecord` translations is coupling your ActiveRecord layer to your UI, which is the wrong level of abstraction. They will cause issues when refactoring.
    I recommend avoiding these entirely.
  3.  I'd also suggest mentioning reference keys. These are invaluable in engines and larger apps. E.g.:
    en:
      greetings: Hello guest!
      visitors:
        index:
          greetings: :greetings
  4. You can use `translate-missing` instead of `add-missing` to add Google-Translated values Japanese version.
  5. Another useful task is `i18n-tasks mv` for renaming keys.

635114?v=3&s=64
kobaltz said 5 months ago:

Thanks for the comments and your i18n-tasks gem!

Photo
Rocela Durazo Verdugo said 4 months ago:

great episode

Login to Comment