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! !!
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
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! !!"
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
If you write "/? Locale = ja" after "localhost: 3000", it will be converted to Japanese.
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
If you write "/? Locale = ja" after "localhost: 3000", it will be converted to Japanese.
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.
About i18n, a report of the LT Association that makes you want to do personal development
Rails Guide Rails Internationalization (i18n) API
Recommended Posts