ctrlcv-dev.comgithub

Rails - i18ncheatsheet

贡献者:BAI

常见用法

常规用法

RUBY
t('my.messages.hello')
# 和 'my.messages.hello' 一样
t(:hello, scope: 'my.messages')
t(:hello, scope: [:my, :messages])
t('my.messages.hello', default: "Hello")
YML
en:
my:
messages:
hello: "Hello"

插补字符

RUBY
t('hello', name: "John")
YML
hello: "Hello %{name}"

懒查询机制

RUBY
# 在 'books/index' 页面中
t('.title')
YML
en:
books:
index:
title: "Título"

复数

RUBY
t(:inbox, count: 1) #=> 'one message'
t(:inbox, count: 2) #=> '2 messages'
YML
inbox:
one: 'one message',
other: '%{count} messages'

本地化

时间

RUBY
l(Time.now)
l(Time.now, format: :short)
YML
en:
time:
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
long: "%B %d, %Y %H:%M"
short: "%d %b %H:%M"

日期

RUBY
l(Date.today)
YML
en:
date:
formats:
default: "%Y-%m-%d" # 2015-06-25
long: "%B %d, %Y" # June 25, 2015
short: "%b %d" # Jun 25

ActiveRecord

模型名称

RUBY
User.model_name.human #=> "User"
Child.model_name.human(count: 2) #=> "Children"
YML
en:
activerecord:
models:
user: "User"
child:
one: "Child"
other: "Children"

属性

RUBY
User.human_attribute_for :name #=> "Name"
YML
en:
activerecord:
attributes:
user:
# activerecord.attributes.<model>.<field>
name: "Name"
email: "Email"

错误信息

RUBY
error_messages_for(...)
YML
activerecord:
errors:
models:
venue:
attributes:
name:
blank: "Please enter a name."

可能的查询路径(按下列顺序)

YML
activerecord.errors.models.[model_name].attributes.[attribute_name].[error]
activerecord.errors.models.[model_name].[error]
activerecord.errors.messages.[error]
errors.attributes.[attribute_name].[error]
errors.messages.[error]

[error] 的值可能为:

YML
validates
confirmation - :confirmation
acceptance - :accepted
presence - :blank
length - :too_short (%{count})
length - :too_long (%{count})
length - :wrong_length (%{count})
uniqueness - :taken
format - :invalid
numericality - :not_a_number

表单标签

RUBY
form_for @post do
f.label :body
YML
helpers:
# helpers.label.<model>.<field>
label:
post:
body: "Your body text"

提交按钮

RUBY
form_for @post do
f.submit
YML
helpers:
submit:
# helpers.submit.<action>
create: "Create a %{model}"
update: "Confirm changes to %{model}"
# helpers.submit.<model>.<action>
article:
create: "Publish article"
update: "Update article"

数字

数字

RUBY
number_to_delimited(2000) #=> "2,000"
number_to_currency(12.3) #=> "$12.30"
number_to_percentage(0.3) #=> "30%"
number_to_rounded(3.14, precision: 0) #=> "3"
number_to_human(12_000) #=> "12 Thousand"
number_to_human_size(12345) #=> "12.3 kb"

分隔符

RUBY
number_to_delimited(n)
YML
number:
format:
separator: "."
delimiter: ","
precision: 3
significant: false
strip_insignificant_zeroes: false

货币

RUBY
number_to_currency(n)
YML
number:
currency:
format:
format: "%u%n" # %u = unit, %n = number
unit: "$"
separator: "."
delimiter: ","
precision: 3
# (see number.format)

百分比

RUBY
number_to_percentage(n)
YML
number:
percentage:
format:
format: "%n%"
# (see number.format)

一些设置

RUBY
I18n.backend.store_translations :en, ok: "Ok"
I18n.locale = :en
I18n.default_locale = :en
I18n.available_locales
I18n.translate :ok # aka, I18n.t
I18n.localize date # aka, I18n.l

参考