Rails Study(13)I18N API

本文介绍 Rails 中的 I18N API 特性,包括翻译查找、插值、复数形式等基本操作,并讲解如何设置及传递语言环境。此外,还介绍了 ActiveRecord 模型错误消息的作用域与插值,以及定制 I18N 设置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rails Study(13)I18N API

4 Overview of the I18n API Features
4.1 Looking up Translations
4.1.1 Basic Lookup, Scops and Nested Keys
Translations are looked up by keys which can be both Symbols or Strings.
I18n.t :message
I18n.t 'message'

so I can use this in index.html.erb
<p><%= t 'hello world' %></p>
<p><%= flash[:notice] %></p>

In controller file
flash[:notice] = t 'hello flash'

test:
hello_flash: test God
hello_world: test Hell
hello world: kill them all
hello flash: kill all
en:
hello_flash: Hello God
hello_world: hello Hell

But t 'hello flash' works differently with <%= t 'hello world' %> in erb

in http://localhost:3000
Hello World
translation missing: en.hello flash

but http://localhost:3000?locale=test
kill them all
kill all

4.1.2 Defaults
When a :default option is given, its value will be returned if the translation is missing.
change something in controller
flash[:notice] = t 'hello flash', :default => 'hello flash'

4.1.3 Bulk and Namespace Lookup
4.1.4 "Lazy" Lookup

4.2 Interpolation
4.3 Pluralization
4.4 Setting and Passing a Locale
set the locale at first
I18n.locale = :de
I18n.t :foo
I18n.l Time.now

pass the locale as parameters
I18n.t :foo, :locale => :de
I18n.l Time.now, :locale => :de

I18n.default_locale = :de

5. How to Store your Custom Translations
My yml file will be
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"

In controller or erb file:
I18n.t 'date.formats.short'
I18n.t 'formats.short', :scope => :date
I18n.t :short, :scope => 'date.formats'
I18n.t :short, :scope => [:date, :formats]

5.1 Translations for Active Record Models
5.1.1 Error Message Scopes
class User < ActiveRecord::Base
validates_presence_of :name
end

looking up order will be:
activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

Another example, you might have an Admin model inheriting from User
class Admin < User
validates_presence_of :name
end

activerecord.errors.models.admin.attributes.title.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.title.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.title.blank
errors.messages.blank

5.1.2 Error Message Interpolation
5.2 Overview of Other Built-In Methods that Provide I18n Support
6. Customize your I18n Setup
6.1 Using Different Backends
we can change the backends
I18n.backend = Globalize::Backend::Static.new

6.2 Using Different Exception Handlers

references:
http://guides.rubyonrails.org/i18n.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值