2009年10月23日星期五

Rails本地化

Rails2.3加入了I18n,已经能够很好的支持本地化(Locale)了
主要为
  • 对框架返回的信息的本地化
  • 对数据库表项目的本地化
  • 对页面文字的本地化

以下是我的一个例子,点这里看效果

实现对 英文,中文,日文 三种语言的支持

RAILS_ROOT/app/controller/application_controller.rb
RAILS_ROOT/config/initializers/i18n.rb
RAILS_ROOT/coonfig/locales/en.yml
RAILS_ROOT/coonfig/locales/zh.yml
RAILS_ROOT/coonfig/locales/ja.yml

application_controller.rb
# 为本地化处理加一个过滤器
class ApplicationController < ActionController::Base
  before_filter :set_locale

protected
  def set_locale
    session[:locale] = params[:locale] if params[:locale]
    I18n.locale = session[:locale] || I18n.default_locale
  end
end

i18n.rb
I18n.default_locale = 'en'

LOCALES_DIRECTORY = "#{RAILS_ROOT}/config/locales/"

LANGUAGES = {
  'English' => 'en',
  '中文' => 'zh',
  '日本語' => 'ja'
}

en.yml
en:
  number:
    currency:
      format:
        unit: "&yen;" 
  
  layout:
    title: "Xi-yang-yang Wedding Garden"
    side:
      home: "Home"
      faq:  "Questions"
      news: "News"
      contact: "Contact"
    cart:
      title: "Your Cart"
      total: "Total"
      checkout: "Checkout"
      empty_cart: "Empty Cart" 

zh.yml
zh:
  number:
    currency:
      format:
        unit: "&yen;"
        precision: 2
        separator: "."
        delimiter: ","
        format: "%u%n"

  activerecord:
    errors:
      # The values :model, :attribute and :value are always available for interpolation
      # The value :count is available when applicable. Can be used for pluralization.
      messages:
        inclusion: "必须是表内的值" #is not included in the list
        exclusion: "保留" # is reserved"
        invalid: "无效" # "is invalid"
        confirmation: "与确认值不一致" # "doesn't match confirmation"
        accepted: "必须被接受" # "must be accepted"
        empty: "不能为空" # "can't be empty"
        blank: "不能为空白" # "can't be blank"
        too_long: "超长(最長{{count}})" # "is too long (maximum is {{count}} characters)"
        too_short: "超短(最短{{count}})" # "is too short (minimum is {{count}} characters)"
        wrong_length: "长度不对(字数必须为{{count}})" # "is the wrong length (should be {{count}} characters)"
        taken: "该值已经存在" # "has already been taken"
        not_a_number: "不是数字" # "is not a number"
        greater_than: "必须大于{{count}}" # "must be greater than {{count}}"
        greater_than_or_equal_to: "不得小于{{count}}" # "must be greater than or equal to {{count}}"
        equal_to: "必须等于{{count}}" # "must be equal to {{count}}"
        less_than: "必须小于{{count}}" # "must be less than {{count}}"
        less_than_or_equal_to: "不得大于{{count}}" # "must be less than or equal to {{count}}"
        odd: "必须为奇数" # "must be odd"
        even: "必须为偶数" # "must be even"
        record_invalid: "验证失败: {{errors}}" # "Validation failed: {{errors}}"
      template:
        header:
          one: "1个错误发生导致处理失败" # "1 error prohibited this {{model}} from being saved"
          other: "{{count}}个错误发生导致处理失败" # "{{count}} errors prohibited this {{model}} from being saved"
        body: "以下的项目出错" # "There were problems with the following fields:" 

    model:
      order: "订购"
    attributes:
      order:
        name: "姓名"
        address: "地址"
        email: "E-mail"
        pay_type: "付款方式" 

  layout:
    title: "喜洋洋婚庆花苑"
    side:
      home: "主页"
      faq:  "常见问题"
      news: "新信息"
      contact: "联系"
    cart:
      title: "你的购物车"
      total: "合计"
      checkout: "购买"
      empty_cart: "清空购物车"
 

ja.yml
ja:
  number:
    currency:
      format:
        unit: "&yen;"
        precision: 2
        separator: "."
        delimiter: ","
        format: "%u%n"

  activerecord:
    errors:
      # The values :model, :attribute and :value are always available for interpolation
      # The value :count is available when applicable. Can be used for pluralization.
      messages:
        inclusion: "リストには含まれていません" #is not included in the list
        exclusion: "保留" # is reserved"
        invalid: "無効です" # "is invalid"
        confirmation: "一致していません" # "doesn't match confirmation"
        accepted: "設定しないとなりません" # "must be accepted"
        empty: "空にしてはいけません" # "can't be empty"
        blank: "空白にしてはいけません" # "can't be blank"
        too_long: "長すぎ(最長{{count}}桁数)" # "is too long (maximum is {{count}} characters)"
        too_short: "短すぎ(最短{{count}}桁数)" # "is too short (minimum is {{count}} characters)"
        wrong_length: "桁数が不正({{count}}桁数)" # "is the wrong length (should be {{count}} characters)"
        taken: "もう設定されていました" # "has already been taken"
        not_a_number: "数字ではありません" # "is not a number"
        greater_than: "{{count}}より大きくしないといけません" # "must be greater than {{count}}"
        greater_than_or_equal_to: "{{count}}以上にしないといけません" # "must be greater than or equal to {{count}}"
        equal_to: "{{count}}ではないといけません" # "must be equal to {{count}}"
        less_than: "{{count}}より小さくしないといけません" # "must be less than {{count}}"
        less_than_or_equal_to: "{{count}}以下にしないといけません" # "must be less than or equal to {{count}}"
        odd: "奇数にしないといけません" # "must be odd"
        even: "偶数にないといけません" # "must be even"
        record_invalid: "検証失敗: {{errors}}" # "Validation failed: {{errors}}"
      template:
        header:
          one: "1個のエラーが発生したため処理が失敗しました" # "1 error prohibited this {{model}} from being saved"
          other: "{{count}}個のエラーが発生したため処理が失敗しました" # "{{count}} errors prohibited this {{model}} from being saved"
        body: "以下のフィールドにエラーが発生しました" # "There were problems with the following fields:" 
    model:
      order: "注文"
    attributes:
      order:
        name: "名前"
        address: "住所"
        email: "E-mail"
        pay_type: "支払方法" 

  layout:
    title: "喜洋々ウエディングガーデン"
    side:
      home: "ホーム"
      faq:  "よくある質問"
      news: "ニュース"
      contact: "お問い合わせ"
    cart:
      title: "あなたのカート"
      total: "合計"
      checkout: "チェックアウト"
      empty_cart: "カートをクリア" 


框架自带的验证出错提示信息可参看这里

没有评论: