Agile Web Development with Rails, Edition 4

15.4 Task J4: Add a locale switcher. 15.2 Task J2: translating the store front

15.3 Task J3: Translating Checkout

<(?-mix:4 errores han impedido que este pedido se guarde)> expected but was
<1 error prohibited this line_item from being saved:

      
        Order translation missing: es.activerecord.errors.models.line_item.attributes.order.required>.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:397:in `block in <class:DepotTest>'

Edit the new order page

edit app/views/orders/new.html.erb
<div class="depot_form">
  <fieldset>
    <legend><%= t('.legend') %></legend>
    <%= render 'form' %>
  </fieldset>
</div>

Edit the form used by the new order page

edit app/views/orders/_form.html.erb
<%= form_for(order) do |f| %>
  <% if order.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(order.errors.count, "error") %>
      prohibited this order from being saved:</h2>
 
      <ul>
      <% order.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div class="field">
    <%= f.label :name, t('.name') %><br>
    <%= f.text_field :name, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :address, t('.address_html') %><br>
    <%= f.text_area :address, rows: 3, cols: 40 %>
  </div>
  <div class="field">
    <%= f.label :email, t('.email') %><br>
    <%= f.email_field :email, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :pay_type, t('.pay_type') %><br>
    <%= f.select :pay_type, Order::PAYMENT_TYPES,
                  prompt: t('.pay_prompt_html') %>
  </div>
  <div class="actions">
    <%= f.submit t('.submit') %>
  </div>
<% end %>

Define some translations for the new order.

edit config/locales/en.yml
en:
 
  orders:
    new:
      legend:       "Please Enter Your Details"
    form:
      name:         "Name"
      address_html: "Address"
      email:        "E-mail"
      pay_type:     "Pay with"
      pay_prompt_html: "Select a payment method"
      submit:       "Place Order"
edit config/locales/es.yml
es:
 
  orders:
    new:
      legend:       "Por favor, introduzca sus datos"
    form:
      name:         "Nombre"
      address_html: "Direcci&oacute;n"
      email:        "E-mail"
      pay_type:     "Forma de pago"
      pay_prompt_html: "Seleccione un m&eacute;todo de pago"
      submit:       "Realizar Pedido"

Add to cart

get /es

Carrito de la Compra

CoffeeScript 36,00 $US
Total 36,00 $US

Su Catálogo de Pragmatic

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

36,00 $US
Ruby

Programming Ruby 1.9 & 2.0

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

49,95 $US
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

34,95 $US
post /es/line_items?product_id=2

Carrito de la Compra

CoffeeScript 36,00 $US
Total 36,00 $US

New Line Item

1 error prohibited this line_item from being saved:

  • Order translation missing: es.activerecord.errors.models.line_item.attributes.order.required


Back

Show mixed validation errors

get /es/orders/new

NameError in Orders#new

Showing /home/rubys/git/awdwr/edition4/work-220/depot/app/views/orders/_form.html.erb where line #1 raised:

undefined local variable or method `order' for #<#<Class:0x0000000527dd28>:0x0000000527b550>
Extracted source (around line #1):
1
2
3
4
5
6
              
<%= form_for(order) do |f| %>
<% if order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(order.errors.count, "error") %>
prohibited this order from being saved:</h2>

Trace of template inclusion: app/views/orders/new.html.erb

Rails.root: /home/rubys/git/awdwr/edition4/work-220/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Response

Headers:

None

Translate the errors to human names.

edit config/locales/es.yml
es:
 
  activerecord:
    errors:
      messages:
        inclusion: "no est&aacute; incluido en la lista"
        blank:     "no puede quedar en blanco"
  errors:
    template:
      body:        "Hay problemas con los siguientes campos:"
      header:
        one:       "1 error ha impedido que este %{model} se guarde"
        other:     "%{count} errores han impedido que este %{model} se guarde"

Display messages in raw form, and translate error messages

edit app/views/orders/_form.html.erb
<%= form_for(order) do |f| %>
  <% if order.errors.any? %>
    <div id="error_explanation">
      <h2><%=raw t('errors.template.header', count: @order.errors.count,
        model: t('activerecord.models.order')) %>.</h2>
      <p><%= t('errors.template.body') %></p>
 
      <ul>
      <% order.errors.full_messages.each do |message| %>
        <li><%=raw message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<!-- ... -->
 
  <div class="field">
    <%= f.label :name, t('.name') %><br>
    <%= f.text_field :name, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :address, t('.address_html') %><br>
    <%= f.text_area :address, rows: 3, cols: 40 %>
  </div>
  <div class="field">
    <%= f.label :email, t('.email') %><br>
    <%= f.email_field :email, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :pay_type, t('.pay_type') %><br>
    <%= f.select :pay_type, Order::PAYMENT_TYPES,
                  prompt: t('.pay_prompt_html') %>
  </div>
  <div class="actions">
    <%= f.submit t('.submit') %>
  </div>
<% end %>

Translate the model names to human names.

edit config/locales/es.yml
es:
 
  activerecord:
    models:
      order:       "pedido"
    attributes:
      order:
        address:   "Direcci&oacute;n"
        name:      "Nombre"
        email:     "E-mail"
        pay_type:  "Forma de pago"

Show validation errors

get /es/orders/new

NameError in Orders#new

Showing /home/rubys/git/awdwr/edition4/work-220/depot/app/views/orders/_form.html.erb where line #2 raised:

undefined local variable or method `order' for #<#<Class:0x000000050a6608>:0x000000050a4e48>
Extracted source (around line #2):
1
2
3
4
5
6
              
<!-- START:explanation -->
<%= form_for(order) do |f| %>
<% if order.errors.any? %>
<div id="error_explanation">
<!-- START_HIGHLIGHT -->
<h2><%=raw t('errors.template.header', count: @order.errors.count,

Trace of template inclusion: app/views/orders/new.html.erb

Rails.root: /home/rubys/git/awdwr/edition4/work-220/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Response

Headers:

None

Replace translatable text with calls out to translation functions.

edit app/controllers/orders_controller.rb
  def create
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)
 
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        session[:order_id] = @order.id
        OrderNotifier.received(@order).deliver_later
        format.html { redirect_to store_url, notice: 
          I18n.t('.thanks') }
        format.json { render :show, status: :created,
          location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors,
          status: :unprocessable_entity }
      end
    end
  end

Define some translations for the flash.

edit config/locales/en.yml
en:
 
  thanks:          "Thank you for your order"
edit config/locales/es.yml
es:
 
  thanks:          "Gracias por su pedido"

Place the order

get /es/orders/new

NameError in Orders#new

Showing /home/rubys/git/awdwr/edition4/work-220/depot/app/views/orders/_form.html.erb where line #2 raised:

undefined local variable or method `order' for #<#<Class:0x007f3fb94ef160>:0x007f3fb94ee1e8>
Extracted source (around line #2):
1
2
3
4
5
6
              
<!-- START:explanation -->
<%= form_for(order) do |f| %>
<% if order.errors.any? %>
<div id="error_explanation">
<!-- START_HIGHLIGHT -->
<h2><%=raw t('errors.template.header', count: @order.errors.count,

Trace of template inclusion: app/views/orders/new.html.erb

Rails.root: /home/rubys/git/awdwr/edition4/work-220/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Response

Headers:

None

15.4 Task J4: Add a locale switcher. 15.2 Task J2: translating the store front