Ruby on Rails Japanese-English support i18n

environment

policy

--Japanese-English correspondence as easily as possible

Overview

  1. Place a language switch button in the header
  2. Store locale information in session variable session [: locale] etc. in the controller
  3. Use before_action to set the language before loading each page
  4. Write the translation in the yml file
  5. Used in View etc.

Implementation

1. Place a language switch button in the header

A button has been installed on the header for easy switching.

erb:application.html.erb


Install the following in the header
<% if session[:locale] == "en" || session[:locale].blank? %>
   <%= link_to "/home/change_language/ja" do %>
Japanese
   <% end %>
<% elsif session[:locale] == "ja" %>
   <%= link_to "/home/change_language/en" do %>
    English
   <% end %>
<% end %>

This time only Japanese and English are supported

--"English" if "ja" is included in session [: locale] --"Japanese" if "en" is included in session [: locale]

Is displayed.

2. Store the session variable in the controller

routes.rb


get "/home/change_language/:language" => "home#change_language"

home_controller.rb


  def change_language
    session[:locale] = params[:language]
    redirect_back(fallback_location: "/")
  end

3. Set locale to I18n with before_action before loading each page

By setting before_action, the language information will be set first no matter which page you access.

application.rb


before_action :set_locale

private
  def set_locale
    if %w(ja en).include?(session[:locale])
      I18n.locale = session[:locale]
    end
  end

  1. app>config>locales>

I wrote the translation separately for. Example) English only changes ja to en.

ja.yml


ja:
  #Commonly used words
  dictionary:
    words:
      hello: "Hello"
  activerecord:
    #Model name
    model:
      user: "user"
    #Column name
    attributes:
      user_name: "User name"
      password: "password"
  #Set for each view
  users:
    show:
      profile: "profile"
      picture: "Photo"
    form:
      required_field: "Required item"

5. Used in View etc.

erb:show.html.erb


 <%= t("dictionary.words.hello") %>  =>Hello
 <%= t("activerecord.attributes.user_name") %>  =>User name

#When View is users show, it can be abbreviated as follows.
 <%= t(".profile") %>  =>profile
 <%= t(".picture") %>  =>Photo

#If you set the column name, even the form helper will automatically switch depending on the language
<%= form_with scope: @user ,,,,,%>
<%= form.label :user_name %> =>User name
<% end %>

erb:_form.html.erb


#Partial is used without underscore View is users_form.html.For erb
 <%= t(".required_field") %>  =>Required item

#This partial is users show.html.Even if it is used in erb
 <%= t(".picture") %>  =>× This is not available.


Recommended Posts

Ruby on Rails Japanese-English support i18n
Ruby on Rails in Visual Studio Codespaces
Beginners create portfolio in Ruby on Rails
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Recommendation of Service class in Ruby on Rails
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Rails new in Ruby on Rails ~ Memorandum until deployment 1
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to install Bootstrap in Rails
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby On Rails] How to reset DB in Heroku
[Ruby on Rails] Post image preview function in refile
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
How to display a graph in Ruby on Rails (LazyHighChart)
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
Apply CSS to a specific View in Ruby on Rails
[Ruby on Rails] Convenient helper method
Ruby methods often used in Rails
[Ruby on Rails] Stop "looping until ..."
Where I was interested in Progate's Ruby on Rails course [params]
(Ruby on Rails6) Creating a database and displaying it in a view
Erase N + 1 in acts_as_tree of tree structure Gem of Ruby on Rails
I summarized the flow until implementing simple_calendar in Ruby on Rails.
Things to remember and concepts in the Ruby on Rails tutorial