`
sillycat
  • 浏览: 2486512 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Rails Study(13)I18N API

阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics