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

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-30 at 22:27发表的

国际化功能问与答(三)
 

警告:在复制本博客代码时,空行存在中文空格,需要删除!!!
 

  • 国际化功能问与答(三):本地化翻译方法
    • 如何调用翻译函数?
      I18n.translate < key >
      如:
      I18n.translate :hello
      或者
      I18n.t :hello
    • 如何调用具有插值的翻译函数?
      I18n.translate < key >, < 插值 >
      如:
      I18n.translate :hello_with_name, :name=>”Hans”
    • 如何调用多元化的翻译函数?
      I18n.translate < key >, < 多元化变量 >
      如:
      I18n.translate :second_in_words, :count=>12
    • 如何调用具有命名空间的翻译函数?
      I18n.translate < 命名空间.key >, < 多元化变量 >
      如:
      I18n.translate “namespcae.second_in_words”, :count=>2
    • 如何调用时间对象的本地化函数?
      I18n.localize < 时间对象 >, :format=>< key >
      如:
      I18n.localize Time.now, :format=>:long
      或者
      I18n.l Time.now, :format=>:long
    • 如何调用日期对象的本地化函数?
      I18n.localize < 日期对象 >, :format=>< key >
      如:
      I18n.localize Date.parse(‘2008-11-29’)
    • 如何调用时间和日期名称的翻译函数?
      I18n.translate < 命名空间.key >
      如:
      I18n.translate(‘date.month_names’)
      I18n.translate(‘date.month_names’).last
如何使用本地化文件的实例方法
rails 03_i18n_demo && cd 03_i18n_demo
 
vi config/environment.rb
config.i18n.default_locale = :"zh-CN"
 
touch config/locales/zh-CN.yml
vi config/locales/zh-CN.yml
zh-CN:
hello: "世界,你好!"
 
hello_with_name: "{{name}},你好!"
 
second_in_words:
one: "一秒钟"
two: "两秒钟"
other: "{{count}}秒钟"
 
namespace:
second_in_words:
one: "一秒钟"
two: "两秒钟"
other: "{{count}}秒钟"
 
time:
formats:
default: "%Y年%b%d日 %A %H:%M:%S %Z"
short: "%b%d日 %H时%M分"
long: "%Y年%b%d日 %H时%M分%S秒"
am: "上午"
pm: "下午"
 
date:
formats:
default: "%Y年%m月%d日"
abbr_default: "%Y-%m-%d"
short: "%b%d日"
long: "%Y年%b%d日"
day_names: [星期天, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六]
abbr_day_names: [日, 一, 二, 三, 四, 五, 六]
month_names: [~, 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月]
abbr_month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]
order: [ :year, :month, :day ]

    • 在图中的命令sc就是“ruby script/console”命令。
评论: 2 (view/add your own) 标记: Rails, Ruby, faq, i18n, use

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)呢?
      可以
    • 一种语言或者一种方言的国际化内容是否可以存放到几个国际化文件呢?
      可以
    • 一个国际化文件名称是否与文件内容相关呢?
      无关