π Translate Active Record
1. Define the dictionary at some yml files
The file name is not related. The term at the second level activerecord:
works.
#config/locales/models_ja.yml
#config/locales/activerecords_ja.yml
#...etc
ja:
activerecored:
models:
author: θθ
attributes:
author:
name: εε
description: ζ¦θ¦
2. Use it in ERB
<div>
<%= Author.model_name.human %>
<%= Author.human_attribute_name :name %>
<%= Author.human_attribute_name :description %>
</div>
π Translate any words
1. Define the dictionary at some yml files
- π You can write words in split multiple files.
- π You can write words with parameter, see also
mail.registration_thanks.name_sir
. - π You can write an array of words, see also
date.day_names
.
# config/locales/words.ja.yml
ja:
labels:
profile: γγγγ£γΌγ«
messages:
sending: ιδΏ‘δΈ
error: γ¨γ©γΌγηΊηγγΎγγ
mail:
registration:
subject: "γη»ι²γγγγ¨γγγγγΎγπ %{name}ζ§"
# config/locales/words.ja.yml
ja:
date:
day_names:
- ζ₯
- ζ
- η«
- ζ°΄
- ζ¨
- ι
- ε
2. Use it
<%= label_tag t('labels.profile') %>
<p><%= t('messages.sending') %></p>
# class RegistMailer < ApplicationMailer
@user.name = n350071
subject = I18n.t('mail.registration.subject', name: @user.name)
subject = I18n.t(:subject, scope: 'mail.registration', name: @user.name)
# => "γη»ι²γγγγ¨γγγγγΎγπ n350071ζ§
# => "γη»ι²γγγγ¨γγγγγΎγπ n350071ζ§
I18n.t('date.day_names')
# => "[\"ζ₯\", \"ζ\", \"η«\", \"ζ°΄\", \"ζ¨\", \"ι\", \"ε\"]"
π Localize Format
The formats reference.
Set your formats
en:
date:
formats:
default: ! '%Y/%m/%d'
long: ! '%Y/%m/%d/(%a)'
short: ! '%m/%d'
middle: ! '%b %d %a'
<%= l(@event.start_date, format: :middle) %>
#=> 'Nov 15 Fri'
default
<%=l Time.now, format: :short %>
I18n.l(Time.now, format: :short) #=> "2019/11/12 10:21"
I18n.l(Time.now, format: :long) #=> "2019εΉ΄11ζ12ζ₯(η«) 10ζ21ε41η§ +0900"
Top comments (0)