Rails - i18ncheatsheet
贡献者:BAI
常见用法
常规用法
RUBYt('my.messages.hello')# 和 'my.messages.hello' 一样t(:hello, scope: 'my.messages')t(:hello, scope: [:my, :messages])t('my.messages.hello', default: "Hello")
YMLen: my: messages: hello: "Hello"
插补字符
RUBYt('hello', name: "John")
YMLhello: "Hello %{name}"
懒查询机制
RUBY# 在 'books/index' 页面中t('.title')
YMLen: books: index: title: "Título"
复数
RUBYt(:inbox, count: 1) #=> 'one message't(:inbox, count: 2) #=> '2 messages'
YMLinbox: one: 'one message', other: '%{count} messages'
本地化
时间
RUBYl(Time.now)l(Time.now, format: :short)
YMLen: time: formats: default: "%a, %d %b %Y %H:%M:%S %z" long: "%B %d, %Y %H:%M" short: "%d %b %H:%M"
日期
RUBYl(Date.today)
YMLen: date: formats: default: "%Y-%m-%d" # 2015-06-25 long: "%B %d, %Y" # June 25, 2015 short: "%b %d" # Jun 25
ActiveRecord
模型名称
RUBYUser.model_name.human #=> "User"Child.model_name.human(count: 2) #=> "Children"
YMLen: activerecord: models: user: "User" child: one: "Child" other: "Children"
属性
RUBYUser.human_attribute_for :name #=> "Name"
YMLen: activerecord: attributes: user: # activerecord.attributes.<model>.<field> name: "Name" email: "Email"
错误信息
RUBYerror_messages_for(...)
YMLactiverecord: errors: models: venue: attributes: name: blank: "Please enter a name."
可能的查询路径(按下列顺序)
YMLactiverecord.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]
的值可能为:
YMLvalidatesconfirmation - :confirmationacceptance - :acceptedpresence - :blanklength - :too_short (%{count})length - :too_long (%{count})length - :wrong_length (%{count})uniqueness - :takenformat - :invalidnumericality - :not_a_number
表单标签
RUBYform_for @post do f.label :body
YMLhelpers: # helpers.label.<model>.<field> label: post: body: "Your body text"
提交按钮
RUBYform_for @post do f.submit
YMLhelpers: submit: # helpers.submit.<action> create: "Create a %{model}" update: "Confirm changes to %{model}" # helpers.submit.<model>.<action> article: create: "Publish article" update: "Update article"
数字
数字
RUBYnumber_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"
分隔符
RUBYnumber_to_delimited(n)
YMLnumber: format: separator: "." delimiter: "," precision: 3 significant: false strip_insignificant_zeroes: false
货币
RUBYnumber_to_currency(n)
YMLnumber: currency: format: format: "%u%n" # %u = unit, %n = number unit: "$" separator: "." delimiter: "," precision: 3 # (see number.format)
百分比
RUBYnumber_to_percentage(n)
YMLnumber: percentage: format: format: "%n%" # (see number.format)
一些设置
RUBYI18n.backend.store_translations :en, ok: "Ok"I18n.locale = :enI18n.default_locale = :enI18n.available_localesI18n.translate :ok # aka, I18n.tI18n.localize date # aka, I18n.l