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

Edit the new order page

edit app/views/orders/new.html.erb
<div class="depot_form">
  <fieldset>
    <legend><%= t('.legend') %></legend>
    <%= render 'form', order: @order %>
  </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 |msg| %>
        <li><%= msg %></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

Rails, Angular, Postgres, and Bootstrap 45,00 $US
Total 45,00 $US

Su Catálogo de Pragmatic

Dcbang

Rails, Angular, Postgres, and Bootstrap

Powerful, Effective, and Efficient Full-Stack Web Development As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with Bootstrap, building an interactive user interface with AngularJS, to storing data quickly and reliably in PostgreSQL. Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment.

45,00 $US
Adrpo

Ruby Performance Optimization

Why Ruby Is Slow, and How to Fix It You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.

46,00 $US
7apps

Seven Mobile Apps in Seven Weeks

Native Apps, Multiple Platforms Answer the question “Can we build this for ALL the devices?” with a resounding YES. This book will help you get there with a real-world introduction to seven platforms, whether you’re new to mobile or an experienced developer needing to expand your options. Plus, you’ll find out which cross-platform solution makes the most sense for your needs.

26,00 $US
post /es/line_items?product_id=2
You are being redirected.
get http://localhost:3000/es/store/index

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US

Su Catálogo de Pragmatic

Dcbang

Rails, Angular, Postgres, and Bootstrap

Powerful, Effective, and Efficient Full-Stack Web Development As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with Bootstrap, building an interactive user interface with AngularJS, to storing data quickly and reliably in PostgreSQL. Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment.

45,00 $US
Adrpo

Ruby Performance Optimization

Why Ruby Is Slow, and How to Fix It You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.

46,00 $US
7apps

Seven Mobile Apps in Seven Weeks

Native Apps, Multiple Platforms Answer the question “Can we build this for ALL the devices?” with a resounding YES. This book will help you get there with a real-world introduction to seven platforms, whether you’re new to mobile or an experienced developer needing to expand your options. Plus, you’ll find out which cross-platform solution makes the most sense for your needs.

26,00 $US

Show mixed validation errors

get /es/orders/new

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US
Por favor, introduzca sus datos




post /es/orders

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US
Por favor, introduzca sus datos

4 errors prohibited this order from being saved:

  • Name no puede quedar en blanco
  • Address no puede quedar en blanco
  • Email no puede quedar en blanco
  • Pay type no est&aacute; incluido en la lista




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 |msg| %>
        <li><%=raw msg %></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

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US
Por favor, introduzca sus datos




post /es/orders

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US
Por favor, introduzca sus datos

4 errores han impedido que este pedido se guarde.

Hay problemas con los siguientes campos:

  • Nombre no puede quedar en blanco
  • Dirección no puede quedar en blanco
  • E-mail 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(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
        OrderMailer.received(@order).deliver
        format.html { redirect_to store_index_url, notice: 
          I18n.t('.thanks') }
        format.json { render action: 'show', status: :created,
          location: @order }
      else
        format.html { render action: '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

Carrito de la Compra

Rails, Angular, Postgres, and Bootstrap 90,00 $US
Total 90,00 $US
Por favor, introduzca sus datos




post /es/orders
You are being redirected.
get http://localhost:3000/es/store/index

Gracias por su pedido

Su Catálogo de Pragmatic

Dcbang

Rails, Angular, Postgres, and Bootstrap

Powerful, Effective, and Efficient Full-Stack Web Development As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with Bootstrap, building an interactive user interface with AngularJS, to storing data quickly and reliably in PostgreSQL. Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment.

45,00 $US
Adrpo

Ruby Performance Optimization

Why Ruby Is Slow, and How to Fix It You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.

46,00 $US
7apps

Seven Mobile Apps in Seven Weeks

Native Apps, Multiple Platforms Answer the question “Can we build this for ALL the devices?” with a resounding YES. This book will help you get there with a real-world introduction to seven platforms, whether you’re new to mobile or an experienced developer needing to expand your options. Plus, you’ll find out which cross-platform solution makes the most sense for your needs.

26,00 $US

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