道喜技术日记 .^. 天天红玉世界

Rails Ruby MacOSX 。。。创新来自于刻苦的实践和勤奋的思考... www.hhtong.com


Rails2.2世界:国际化功能问与答(五)

Gudao Luo2008-12-28 at 15:02发表的

国际化功能问与答(五)
* 本地化问题(五):如何对模型进行本地化?
模型文件代码
# db/migrate/20081115224456_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.text :body
t.boolean :published t.timestamps
end
end
针对模型的本地化文件
# config/locales/zh-CN.yml
# I18n.locale="zh-CN"
# I18n.translate 'activerecord.models.post'
# I18n.translate 'activerecord.attributes'
# I18n.translate 'activerecord.attributes.post'
# I18n.translate 'activerecord.attributes.post.title'
zh-CN:
activerecord:
models:
post: 帖子
attributes:
post:
title: 标题
body: 内容
published: 发布时间

Rails2.2世界:国际化功能问与答(四)

Gudao Luo2008-12-13 at 10:04发表的

国际化功能问与答(四)
  • 本地化问题(四):本地化工作方法
    • 重新载入本地化文件命令:
      I18n.reload!
    • 设置当前本地化语言
      I18n.locale = “zh-CN”
    • 查看当前本地化语言
      I18n.locale
    • 查看本地化内容信息
      I18n.t “time.formats”
    • 简化本地化函数方法
简化本地化函数方法
class String
def t(locale="zh-CN")
I18n.t self.to_sym, :locale => locale
end
end
"hello".t
"hello".t "en"

Rails2.2世界:国际化功能问与答(二)

Gudao Luo2008-11-28 at 23:50发表的

国际化功能问与答(二)
  • 国际化功能问与答(二):用户定义本地化文件
1. 如何确定自己软件的本地化默认语言?
vi config/environment.rb
config.i18n.default_locale = :"zh-CN"
2. 如何确定自己软件的本地化默认文件路径?
vi config/environment.rb
config.i18n.load_path += Dir[File.join("#{RAILS_ROOT}/config/", 'locales', 'admin', '*.{rb,yml}')]
用户定义本地化文件的实例命令
rails 02_i18n_demo && cd 02_i18n_demo
 
vi config/environment.rb
# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
config.i18n.default_locale = :"zh-CN"
config.i18n.load_path += Dir[File.join("#{RAILS_ROOT}/config/", 'locales', 'admin', '*.{rb,yml}')]
 
mkdir config/locales/admin
 
vi config/locales/admin/zh-CN.yml
zh-CN:
# I18n.translate :hello
hello: "世界,你好!"
用户定义本地化文件的操作方法命令
localhost:demo gudao$ ./script/console
Loading development environment (Rails 2.2.2)
>> I18n.load_path
=> ["/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/locale/en.yml",
"/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/locale/en.yml",
"/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/locale/en.yml",
"/Users/gudao/Documents/railsspace/open-projects/i18n/demo/config/locales/admin/en.yml",
"/Users/gudao/Documents/railsspace/open-projects/i18n/demo/config/locales/admin/zh-CN.yml"]
>> I18n.t :hello
=> "世界,你好!"
>> exit

Rails2.2世界:国际化功能问与答(一)

Gudao Luo2008-11-26 at 06:02发表的

国际化功能问与答(一)i18n
  • 国际化功能问与答(一):国际化(i18n)文件
    • 默认国际化文件在哪里?
      $RAILS_ROOT/config/locales
    • 使用什么样语言写国际化文件?
      YAML语言
      或者
      Ruby语言
    • 通常中国大陆中文使用什么国际化文件名称?
      zh-CN.yml
      或者
      zh-CN.rb
    • 默认国际化文件是定义了什么文字语言?
      English
    • 国际化文件最大特点是什么?
      以命名空间为基础结构
    • 一个国际化文件是否可以存在几种语言或者方言(zh-CN,zh-HK,zh-TW)呢?
      可以
    • 一种语言或者一种方言的国际化内容是否可以存放到几个国际化文件呢?
      可以
    • 一个国际化文件名称是否与文件内容相关呢?
      无关