[RUBY] Supports multilingualization with Rails!

I will write what I did when I tried to support English in web application I am developing.

For more information>

Installation

Gemfile


gem 'rails-i18n'

Installation

bundle

Set in config / application.rb

config/application.rb


    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]

    #Corresponding language. This time in Japanese(ja)And english(en)Only
    config.i18n.available_locales = %i(ja en)

    #If a language other than the above supported languages is specified, set whether to make an error
    config.i18n.enforce_available_locales = true

    #Default language
    config.i18n.default_locale = :ja

controller

controller/application_controller.rb


    before_action :set_locale

    def set_locale
        I18n.locale = locale
    end

    def locale
        @locale ||= params[:locale] ||= I18n.default_locale
    end

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

routes.rb

routes.rb


  scope '(:locale)', locale: /#{I18n.available_locales.map(&:to_s).join('|')}/ do
    root "posts#index"
    resources :posts
  end

The part surrounded by scope is a page that supports multiple languages.

Multilingual

Create en.yml and ja.yml. For sentences that you want to multilingualize

views


<%= t(:hello) %>

Write. Feel free to change hello

ja.yml


  ja:
    hello:Hello
      #All models start from activerecord.
      #This will allow User.model_name.human / User.human_attribute_name({attr_name})Can be accessed with.
    activerecord:
      models:
        user: 'User information'
      attributes:
        user:
          name: 'name'
          mail: 'mail address'
          url:  'Web page'

en.yml


  en:
     hello: "hello"
    #All models start from activerecord.
      #This will allow User.model_name.human / User.human_attribute_name({attr_name})Can be accessed with.
    activerecord:
      models:
        user: 'User'
      attributes:
        user:
          name: 'Name'
          mail: 'Email'
          url:  'URL'

I will do it like this.

reference http://alfa.hatenablog.jp/entry/2013/12/03/221308

Recommended Posts

Supports multilingualization with Rails!
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
Handle devise with Rails
[Rails] Learning with Rails tutorial
[Rails] Test with RSpec
[Rails] Development with MySQL
Supports multi-port with SpringBoot
Double polymorphic with Rails
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
Try using view_component with rails
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Japaneseize using i18n with Rails
API creation with Rails + GraphQL
Preparation for developing with Rails
[Docker] Rails 5.2 environment construction with docker
Use multiple databases with Rails 6.0
[Rails] Specify format with link_to
Login function implementation with rails
[Docker] Use whenever with Docker + Rails
Rails + MySQL environment construction with Docker
Create portfolio with rails + postgres sql
[Rails] Push transmission with LINE Bot
[Rails] Make pagination compatible with Ajax
[Rails] Creating a new project with rails new
Minimal Rails with reduced file generation
Create pagination function with Rails Kaminari
Eliminate Rails FatModel with value object
Recognize Rails projects with Intellij idea
[Grover] Generate PDF with Rails [2020 version]
Build Rails environment with Docker Compose
[Rails] Initial data creation with seed
Track Rails app errors with Sentry
[Environment construction with Docker] Rails 6 & MySQL 8
Connect to Rails server with iPhone
How to get along with Rails
Create My Page with Rails devise
Introducing React to Rails with react-rails
Initial data input with [Rails] seed_fu!
Timeless search with Rails + JavaScript (jQuery)
Let's unit test with [rails] Rspec!
[Rails] Video does not play with video_tag
Generate Dart client code with Rails + apipie
[Rails] Benefit from rubycop with minimal effort
[Ruby on Rails] View test with RSpec
[Rails] Manage secret keys etc. with credentials.yml
Rails environment construction with Docker (personal apocalypse)
Easy deployment with Capistrano + AWS (EC2) + Rails
Building Rails 6 and PostgreSQL environment with Docker
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
Prepare the format environment with "Rails" (VScode)
[Rails withdrawal] Create a simple withdrawal function with rails
[Ruby on Rails] Controller test with RSpec
[Rails] Let's manage constants with config gem
[Rails 6] Interactive graph drawing with Ajax + chart.js
[Form with two arguments] rails, form_for / form_with arguments
Make a login function with Rails anyway