RailsによるアジャイルWebアプリケーション開発

第15章 タスクJ:国際化

せっかくなので、言語はjaで。
errorの文言はrails-i18nから拝借。

svenfuchs/rails-i18n · GitHub

  • config/locales/ja.yml
ja:
  confirm:  "本当に良いですか?"

  layouts:
    application:
      title:      "Pragmatic Bookshelf 日本語サイト"
      home:       "ホーム"
      questions:  "よくある質問"
      news:       "ニュース"
      contact:    "お問い合わせ"
      orders:     "注文"
      products:   "商品"
      users:      "ユーザ"
      logout:     "ログアウト"

  store:
    index:
      title_html: "Pragmatic カタログ"
      add_html:   "カートに入れる"

  carts:
    cart:
      title:    "カート"
      empty:    "カートを空にする"
      checkout: "チェックアウト"
      total:    "合計"

  orders:
    new:
      legend: "お客様の情報を入力して下さい"
    create:
      thanks:   "ご注文ありがとうございました"

    form:
      pay_prompt_html:  "お支払い方法を選択してください"
      submit:           "注文する"

  number:
    currency:
      format:
        unit:         "ドル"
        precision:    2
        separator:    "."
        delimiter:    ","
        format:       "%n %u"

  errors: &errors
    format: ! '%{attribute}%{message}'
    messages:
      accepted: を受諾してください。
      blank: を入力してください。
      present: は入力しないでください。
      confirmation: と%{attribute}の入力が一致しません。
      empty: を入力してください。
      equal_to: は%{count}にしてください。
      even: は偶数にしてください。
      exclusion: は予約されています。
      greater_than: は%{count}より大きい値にしてください。
      greater_than_or_equal_to: は%{count}以上の値にしてください。
      inclusion: は一覧にありません。
      invalid: は不正な値です。
      less_than: は%{count}より小さい値にしてください。
      less_than_or_equal_to: は%{count}以下の値にしてください。
      not_a_number: は数値で入力してください。
      not_an_integer: は整数で入力してください。
      odd: は奇数にしてください。
      record_invalid: バリデーションに失敗しました。 %{errors}
      restrict_dependent_destroy: ! '%{record}が存在しているので削除できません。'
      taken: はすでに存在します。
      too_long: は%{count}文字以内で入力してください。
      too_short: は%{count}文字以上で入力してください。
      wrong_length: は%{count}文字で入力してください。
      other_than: "は%{count}以外の値にしてください。"
    template:
      body: 次の項目を確認してください。
      header:
        one: ! '%{model}にエラーが発生しました。'
        other: ! '%{model}に%{count}個のエラーが発生しました。'

  activerecord:
    errors:
      <<: *errors
    models:
      order: 注文
    attributes:
      order:
        address:  "住所"
        name:     "氏名"
        email:    "E-mail"
        pay_type: "支払方法"

言語を選択するform_forには、method: :getを指定しないとrouteがない、となる。

  • app/views/layouts/application.html.haml
!!!
%html
  %head
    %title Depot
    = stylesheet_link_tag    'application', media: 'all'
    = javascript_include_tag 'application'
    = csrf_meta_tags
  %body{ class: controller.controller_name }
    #banner
      = form_tag store_path, method: :get, class: "locale" do
        = select_tag "set_locale", options_for_select(LANGUAGES, I18n.locale.to_s),
          onchange: "this.form.submit()"
        = submit_tag "submit"
        = javascript_tag "$('.locale input').hide()"
      = image_tag("logo.png")
      = @page_title || t(".title")

    #columns
      #side
        - if @cart
          = hidden_div_if(@cart.line_items.empty?, id: "cart") do
            = render @cart

        %ul
          %li= link_to t(".home"), "#"
          %li= link_to t(".questions"), "#"
          %li= link_to t(".news"), "#"
          %li= link_to t(".contact"), "#"

        - if session[:user_id]
          %ul
            %li= link_to t(".orders"), orders_path
            %li= link_to t(".products"), products_path
            %li= link_to t(".users"), users_path
          = button_to t(".logout"), logout_path, method: :delete

      #main
        = yield