Agile Web Development with Rails, Edition 4

15.3 Task J3: Translating Checkout 15.1 Task J1: Selecting the locale

15.2 Task J2: translating the store front

</Su Cat(.|&#?\w+;)logo de Pragmatic/> expected but was
<"Your Pragmatic Catalog">.

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

Replace translatable text with calls out to translation functions.

edit app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag    "depot", media: "all",
    "data-turbolinks-track" => true %>
  <%= javascript_include_tag "depot", "data-turbolinks-track" => true %>
  <%= csrf_meta_tag %>
</head>
<body class="<%= controller.controller_name %>">
  <div id="banner">
    <%= image_tag("logo.png") %>
    <%= @page_title || t('.title') %>
  </div>
  <div id="columns">
    <div id="side">
      <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
          <%= render @cart %>
        <% end %>
      <% end %>
 
      <ul>
        <li><a href="http://www...."><%= t('.home') %></a></li>
        <li><a href="http://www..../faq"><%= t('.questions') %></a></li>
        <li><a href="http://www..../news"><%= t('.news') %></a></li>
        <li><a href="http://www..../contact"><%= t('.contact') %></a></li>
      </ul>
 
      <% if session[:user_id] %>
        <ul>
          <li><%= link_to 'Orders',   orders_path   %></li>
          <li><%= link_to 'Products', products_path %></li>
          <li><%= link_to 'Users',    users_path    %></li>
        </ul>
        <%= button_to 'Logout', logout_path, method: :delete   %>
      <% end %>
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
</body>
</html>

Replace translatable text with calls out to translation functions.

cp -r /home/rubys/git/awdwr/edition4/data/i18n/*.yml config/locales

Define some translations for the layout.

edit config/locales/en.yml
en:
 
  layouts:
    application:
      title:       "Pragmatic Bookshelf"
      home:        "Home"
      questions:   "Questions"
      news:        "News"
      contact:     "Contact"
edit config/locales/es.yml
es:
 
  layouts:
    application:
      title:       "Publicaciones de Pragmatic"
      home:        "Inicio"
      questions:   "Preguntas"
      news:        "Noticias"
      contact:     "Contacto"

Server needs to be restarted when introducting a new language

Restart the server.

See results

get /es

Your Pragmatic Catalog

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

Replace translatable text with calls out to translation functions.

edit app/views/store/index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
 
<h1><%= t('.title_html') %></h1>
 
<% @products.each do |product| %>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%= product.title %></h3>
    <%= sanitize(product.description) %>
    <div class="price_line">
      <span class="price"><%= number_to_currency(product.price) %></span>
          <%= button_to t('.add_html'), line_items_path(product_id: product),
            remote: true %>
    </div>
  </div>
<% end %>

Define some translations for the main page.

edit config/locales/en.yml
en:
 
  store:
    index:
      title_html:  "Your Pragmatic Catalog"
      add_html:    "Add to Cart"
edit config/locales/es.yml
es:
 
  store:
    index:
      title_html:  "Su Cat&aacute;logo de Pragmatic"
      add_html:    "A&ntilde;adir al Carrito"

See results

get /es

NameError in Store#index

Showing /home/rubys/git/awdwr/edition4/work-192-30/depot/app/views/store/index.html.erb where line #6 raised:

uninitialized constant I18n::RESERVED_KEYS

Extracted source (around line #6):

3: <% end %>
4: 
5: <!-- START_HIGHLIGHT -->
6: <h1><%= t('.title_html') %></h1>
7: <!-- END_HIGHLIGHT -->
8: 
9: <% @products.each do |product| %>

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

Application Trace | Framework Trace | Full Trace
app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb___2376477419595695665_70128293177340_558081496217810729'

Request

Parameters:

{"locale"=>"es"}

Show session dump

Show env dump

Response

Headers:

None

Replace translatable text with calls out to translation functions.

edit app/views/carts/_cart.html.erb
<h2><%= t('.title') %></h2>
<table>
  <%= render(cart.line_items) %>
 
  <tr class="total_line">
    <td colspan="2">Total</td>
    <td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
  </tr>
 
</table>
 
<%= button_to t('.checkout'), new_order_path, method: :get %>
<%= button_to t('.empty'), cart, method: :delete,
    data: { confirm: 'Are you sure?' } %>

Define some translations for the cart.

edit config/locales/en.yml
en:
 
  carts:
    cart:
      title:       "Your Cart"
      empty:       "Empty cart"
      checkout:    "Checkout"
edit config/locales/es.yml
es:
 
  carts:
    cart:
      title:       "Carrito de la Compra"
      empty:       "Vaciar Carrito"
      checkout:    "Comprar"

Format the currency.

edit config/locales/es.yml
es:
 
  number:
    currency:
      format:
        unit:      "$US"
        precision: 2
        separator: ","
        delimiter: "."
        format:    "%n&nbsp;%u"

Add to Cart

get /es

NameError in Store#index

Showing /home/rubys/git/awdwr/edition4/work-192-30/depot/app/views/store/index.html.erb where line #6 raised:

uninitialized constant I18n::RESERVED_KEYS

Extracted source (around line #6):

3: <% end %>
4: 
5: <!-- START_HIGHLIGHT -->
6: <h1><%= t('.title_html') %></h1>
7: <!-- END_HIGHLIGHT -->
8: 
9: <% @products.each do |product| %>

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

Application Trace | Framework Trace | Full Trace
app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb___2376477419595695665_70128301171060_558081496217810729'

Request

Parameters:

{"locale"=>"es"}

Show session dump

Show env dump

Response

Headers:

None

15.3 Task J3: Translating Checkout 15.1 Task J1: Selecting the locale