[RUBY] [Rails] How to write in Japanese

Introduction

This time I learned how to write in Japanese.

I would like to output this as well to get used to it.

I think there are many ways to do it, but first as an introduction.

i18n

It is an internationalization function because it supports multiple languages.

It is called "i18n" because it has 18 characters for internationalization.

Easy to remember! !!

Description of controller

Switch the display language according to the URL option.

application_controller.rb


class ApplicationController < ActionController::Base
    before_action :set_locale
  
    def set_locale
      I18n.locale = params[:locale] || I18n.default_locale
    end
  
    def default_url_options(options = {})
      { locale: I18n.locale }.merge options
    end
end

Described in yaml file

Let's take a look at the contents of the "en.yml" file in the locale directory in the config directory. (config/locales/en.yml)

I'm trying to describe the morning: "morning" in the en: part.

en.yml


# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
#     I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
#     <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
#     I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
#   'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at https://guides.rubyonrails.org/i18n.html.

en:
  hello: "Hello world"
  morning: "morning"

Create a "ja.yml" file in the same hierarchy as "en.yml".

(You can copy files with different names and the same contents with "cp -a config/locales/en.yml config/locales/ja.yml".)

ja.yml


# (en.The same thing as yml is written above)
#      .
#・
#・


ja:
  hello: "World of Hi everyone"
  morning: "Good morning! !!"

Described in view file

html:index.html.erb



 <!--(Only the part that changes the character is described.)-->

<div class="wrapper">
<h1><%= t('hello') %></h1>
<p><%= t('morning') %></p>

If you write like this, at first

Although it is in English

スクリーンショット 2020-12-29 22.34.26.png

If you write "/? Locale = ja" after "localhost: 3000", it will be converted to Japanese.

スクリーンショット 2020-12-29 22.35.22.png

If ​​it is a label tag

html:new.html.erb


<div class="wrapper">
<h1><%= t('hello') %></h1>
  <div class="form">
  <%= form_with model: @map do |f|  %>
    <div class="field">
        <%= f.label :address %>
        <%= f.text_field :address %>
        <%= f.label :name %>
        <%= f.text_field :name %>
        <%= f.submit 'Submit' %>
      </div>
    <% end %>
  </div>
</div>

I will write it directly in "ja.yml".


ja.yml



ja:
  hello: "World of Hi everyone"
  morning: "Good morning! !!"


  activerecord:
      models:
        map: 
  
      attributes:
        map:
          name:Is the name! !!
          address:It's an address! !!

#Pay attention to the position to write! !!


By doing so

スクリーンショット 2020-12-29 23.05.12.png

If you write "/? Locale = ja" after "localhost: 3000", it will be converted to Japanese.

スクリーンショット 2020-12-29 23.05.35.png

Summary

I'm glad that you can easily change to other languages ​​as well as Japanese by using this.

I still have a lot to remember.

Next time, I'll read a little more and learn how to use gems.

I will paste what is written in more detail.

That's all for today.

Document

Github rails-i18n

About i18n, a report of the LT Association that makes you want to do personal development

Rails Guide Rails Internationalization (i18n) API

pika waka [Rails] I18n Primer

Recommended Posts

[Rails] How to write in Japanese
[Ruby on Rails] How to write enum in Japanese
How to write Rails
How to write Rails validation
How to write Rails seed
How to write Rails routing
How to translate Rails into Japanese in general
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write a date comparison search in Rails
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
How to display error messages in Japanese
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write the view when Vue is introduced in Rails?
Rails: How to write a rake task nicely
[JavaFX] How to write Eclipse permissions in build.gradle
[Rails] How to write when making a subquery
[Rails] Various ways to write in seed files
[Rails] How to use select boxes in Ransack
JUnit 5: How to write test cases in enum
How to prevent direct URL typing in Rails
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to write Java String # getBytes in Kotlin?
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
Notes on how to write comments in English
[Rails] How to convert UC time display to Japanese time display
How to write good code
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized