Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

15.4 Task I4: Add a locale switcher. 15.2 Task I2: translating the store front

15.3 Task I3: Translating Checkout

Ticket 4518 : assert_select doesn't work with utf-8 on Ruby 1.9.2

Replace translatable text with calls out to translation functions.

edit app/views/orders/new.html.erb
<div class="depot_form">
  <fieldset>
    <legend><%= t('.legend') %></legend>
<%= render 'form' %>
  </fieldset>
</div>
edit app/views/orders/_form.html.erb
<%= form_for @order do |f| %>
  <% if @order.errors.any? %>
    <div id="error_explanation">
      <%= t('errors.template.header', :count=>@order.errors.size,
        :model=>t('activerecord.models.order')) %>:
      <ul>
      <% @order.errors.full_messages.each do |msg| %>
        <li><%=raw msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div>
    <%= f.label :name, t('.name') + ":" %>
    <%= f.text_field :name, :size => 40 %>
  </div>
 
  <div>
    <%= f.label :address, t('.address') + ":" %>
    <%= f.text_area :address, :rows => 3, :cols => 40 %>
  </div>
 
  <div>
    <%= f.label :email, t('.email') + ":" %>
    <%= f.text_field :email, :size => 40 %>
  </div>
 
  <div>
    <%= f.label :pay_type, t('.pay_type') + ":" %>
    <%=
      f.select :pay_type,
                   Order::PAYMENT_TYPES, 
                  :prompt => t('.pay_prompt')
    %>
  </div>
 
  <%= f.submit t('.submit') %>
<% 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:     "Address"
      email:       "E-mail"
      pay_type:    "Pay with"
      pay_prompt:  "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:     "Direcci&oacute;n"
      email:       "E-mail"
      pay_type:    "Pagar con"
      pay_prompt:  "Seleccione un m&eacute;todo de pago"
      submit:      "Realizar Pedido"

Add to cart

get /es
Carrito de la Compra
Web Design for Developers 42,95 $US
Total 42,95 $US
Inicio
Preguntas
Noticias
Contacto

Orders
Products
Users

Su Catálogo de Pragmatic

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

34,95 $US
Ruby

Programming Ruby 1.9

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,50 $US
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

42,95 $US
post /es/line_items?product_id=2
You are being redirected.
get http://localhost:3000/es
Carrito de la Compra
Web Design for Developers 85,90 $US
Total 85,90 $US
Inicio
Preguntas
Noticias
Contacto

Orders
Products
Users

Su Catálogo de Pragmatic

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

34,95 $US
Ruby

Programming Ruby 1.9

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,50 $US
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

42,95 $US

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"

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"

Show validation errors

get /es/orders/new
Por favor, introduzca sus datos
post /es/orders
Por favor, introduzca sus datos
5 errores han impedido que este pedido se guarde:
  • Nombre no puede quedar en blanco
  • Dirección no puede quedar en blanco
  • E-mail no puede quedar en blanco
  • Forma de pago no puede quedar en blanco
  • Forma de pago no está incluido en la lista

Replace translatable text with calls out to translation functions.

edit app/controllers/orders_controller.rb
  def create
    @order = Order.new(params[:order])
    @order.add_line_items_from_cart(find_or_create_cart)
 
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        Notifier.order_received(@order).deliver
        format.html { redirect_to(store_url, :notice => 
          I18n.t('.thanks')) }
        format.xml  { render :xml => @order, :status => :created,
          :location => @order }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @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
Por favor, introduzca sus datos
post /es/orders
You are being redirected.
get http://localhost:3000/es
Inicio
Preguntas
Noticias
Contacto

Orders
Products
Users

Gracias por su pedido

Su Catálogo de Pragmatic

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

34,95 $US
Ruby

Programming Ruby 1.9

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,50 $US
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

42,95 $US

15.4 Task I4: Add a locale switcher. 15.2 Task I2: translating the store front