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

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


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”命令。
Tags: Rails, Ruby, faq, i18n, use
Hierarchy: previous, next