Agile Web Development with Rails, Edition 5

16.3 Task K3: Translating Checkout 16.1 Task K1: Selecting the locale

16.2 Task K2: translating the store front

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>
    <%= csrf_meta_tags %>
 
    <%= stylesheet_link_tag 'application', media: 'all',
                            'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application',
                               'data-turbolinks-track': 'reload' %>
  </head>
 
  <body>
    <header class="main">
      <%= image_tag 'logo.svg', alt: 'The Pragmatic Bookshelf' %>
      <h1><%= @page_title %></h1>
    </header>
    <section class="content">
      <nav class="side_nav">
 
        <div id="cart" class="carts">
 
          <%= render_if @cart && @cart.line_items.any?, @cart %>
        </div>
 
      <%= render Order.find(session[:order_id]) if session[:order_id] -%>
 
        <ul>
          <li><a href="/"><%= t('.home') %></a></li>
          <li><a href="/questions"><%= t('.questions') %></a></li>
          <li><a href="/news"><%= t('.news') %></a></li>
          <li><a href="/contact"><%= t('.contact') %></a></li>
        </ul>
 
        <% if session[:user_id] %>
          <nav class="logged_in_nav">
            <ul>
              <li><%= link_to 'Orders',   orders_path   %></li>
              <li><%= link_to 'Products', products_path %></li>
              <li><%= link_to 'Users',    users_path    %></li>
              <li><%= button_to 'Logout', logout_path, method: :delete   %></li>
            </ul>
          </nav>
        <% end %>
      </nav>
      <main class='<%= controller.controller_name %>'>
        <%= yield %>
      </main>
    </section>
  </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:       "The Pragmatic Bookshelf"
      home:        "Home"
      questions:   "Questions"
      news:        "News"
      contact:     "Contact"
edit config/locales/es.yml
es:
 
  layouts:
    application:
      title:       "Biblioteca 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
The Pragmatic Bookshelf

Your Pragmatic Catalog

  • 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
  • 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
  • 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

Replace translatable text with calls out to translation functions.

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

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
The Pragmatic Bookshelf

Su Catálogo de Pragmatic

  • 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
  • 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
  • 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

Replace translatable text with calls out to translation functions.

edit app/views/carts/_cart.html.erb
<article>
  <% if notice %>
    <aside id="notice"><%= notice %></aside>
  <% end %>
 
  <h2><%= t('.title') %></h2>
  <table>
 
    <%= render(cart.line_items) %>
    <tfoot>
      <tr>
        <th colspan="2">Total:</th>
        <td class="price"><%= number_to_currency(cart.total_price) %></td>
      </tr>
    </tfoot>
  </table>
 
  <div class="actions">
  <%= button_to t('.empty'), cart,
                method: :delete,
                data: { confirm: 'Are you sure?' } %>
 
 
  <%= button_to t('.checkout'), new_order_path(locale: I18n.locale),
   
                method: :get,
                class: "checkout"%>
  </div>
</article>

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"

Handle remote calls too

edit app/views/store/index.html.erb
          <div class="price">
            <%= number_to_currency(product.price) %>
            <%= button_to t('.add_html'),
            line_items_path(product_id: product, locale: I18n.locale),
              remote: true %>
          </div>

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
The Pragmatic Bookshelf

Su Catálogo de Pragmatic

  • 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
  • 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
  • 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/
The Pragmatic Bookshelf

Su Catálogo de Pragmatic

  • 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
  • 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
  • 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
get /es
The Pragmatic Bookshelf

Su Catálogo de Pragmatic

  • 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
  • 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
  • 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

16.3 Task K3: Translating Checkout 16.1 Task K1: Selecting the locale