Dave says..

Well I haven’t put the PAYMENT_TYPES from the Order model class into the database, so this one was quite an easy task to do:

in models/order.rb:


...
  PAYMENT_TYPES = [
    # Displayed                                     stored in db
    [ I18n.t("order.payment_types.cheque"),         "check" ],
    [ I18n.t("order.payment_types.credit_card"),    "cc" ],
    [ I18n.t("order.payment_types.purchase_order"), "po" ]
  ]
...

Note that the I18n.t(””) formula cannot be replaced with t(””) in the Order class

Mark says..

Need a mirgation too, right?

class LocalizeOrdersPaymentType < ActiveRecord::Migration
  def self.up
    say_with_time "Updating payment type..." do
      Order.all.each do |order|
        new_type = case order.pay_type
                     when "Credit Card" then
                       "cc" 
                     when "Check" then
                       "check" 
                     when "Purchase Order" then
                       "po" 
                   end
        order.update_attribute :pay_type, new_type
      end
    end
  end

  def self.down
    say_with_time "Updating payment type..." do
      Order.all.each do |order|
        new_type = case order.pay_type
                     when "cc" then
                       "Credit Card" 
                     when "check" then
                       "Check" 
                     when "po" then
                       "Purchase Order" 
                   end
        order.update_attribute :pay_type, new_type
      end
    end
  end
end

I also changed the yml hash to Orders prefix instead of Order and update orders tests.

Andriy says

in models/order.rb:


...
if I18n.locale.to_s == 'en'
    PAYMENT_TYPES = PayType.find(:all).collect {|type| [type.name]}
  else if I18n.locale.to_s == 'es'
      PAYMENT_TYPES = [
        [ "Check_transl",              "Check" ],
        [ "Credit_transl",       "Credit card" ],
        [ "Purchase_transl",  "Purchase order" ]
      ]
     end
   end
  validates :name, :address, :email, :pay_type, :presence => true
  validates :pay_type, :inclusion => [ "Check" , "Credit card" , "Purchase order" ]
...

Lani says…

There are probably many ways to solve this. The hint however tells us to only translate what is shown, not what is stored in the database. The hint also suggests that we can use options_for_select. So I took the following approach:

1. Create a helper in /app/helpers/orders_helper.rb:

  def payment_type_options
    options_for_select(Hash [ Order::PAYMENT_TYPES.map {|x|  [ I18n.t('orders.payment_types.' + x.parameterize.underscore), x ] } ] )    
  end

This creates a hash from the PAYMENT_TYPES array. The same should work if you have stored the payment types in the database. This hash is then passed as an option to the options_for_select method.

2. Update the languages files (example for /config/locales/en.yml):

en:
  ...
  orders:
    ...
    payment_types:
      check: "Check" 
      credit_card: "Credit Card" 
      purchase_order: "Purchase Order" 
3. Update the f.select call to use our new helper method in /app/views/orders/_form.html.erb:

<%= f.select :pay_type, payment_type_options, prompt: t('.pay_prompt') %>