Agile Web Development with Rails, Edition 4

15.2 Task J2: translating the store front 14.5 Playtime

15.1 Task J1: Selecting the locale

Define the default and available languages.

edit config/initializers/i18n.rb
#encoding: utf-8
I18n.default_locale = :en
 
LANGUAGES = [
  ['English',                  'en'],
  ["Español".html_safe, 'es']
]

Restart the server.

Scope selected routes based on locale. Important: move to bottom!

edit config/routes.rb
Rails.application.routes.draw do
  get 'admin' => 'admin#index'
  controller :sessions do
    get  'login' => :new
    post 'login' => :create
    delete 'logout' => :destroy
  end
 
  resources :users
  resources :products do
    get :download, :on => :member
    get :who_bought, on: :member
  end
 
  scope '(:locale)' do
    resources :orders
    resources :line_items
    resources :carts
    get 'store/index'
    root 'store#index', as: 'store', via: :all
  end
end

inspect the results

get /rails/info/routes

Routes

Routes match in priority from top to bottom

Helper HTTP Verb Path Controller#Action
Path / Url
admin_path GET /admin(.:format) admin#index
login_path GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout_path DELETE /logout(.:format) sessions#destroy
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
download_product_path GET /products/:id/download(.:format) products#download
who_bought_product_path GET /products/:id/who_bought(.:format) products#who_bought
products_path GET /products(.:format) products#index
POST /products(.:format) products#create
new_product_path GET /products/new(.:format) products#new
edit_product_path GET /products/:id/edit(.:format) products#edit
product_path GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
orders_path GET (/:locale)/orders(.:format) orders#index
POST (/:locale)/orders(.:format) orders#create
new_order_path GET (/:locale)/orders/new(.:format) orders#new
edit_order_path GET (/:locale)/orders/:id/edit(.:format) orders#edit
order_path GET (/:locale)/orders/:id(.:format) orders#show
PATCH (/:locale)/orders/:id(.:format) orders#update
PUT (/:locale)/orders/:id(.:format) orders#update
DELETE (/:locale)/orders/:id(.:format) orders#destroy
line_items_path GET (/:locale)/line_items(.:format) line_items#index
POST (/:locale)/line_items(.:format) line_items#create
new_line_item_path GET (/:locale)/line_items/new(.:format) line_items#new
edit_line_item_path GET (/:locale)/line_items/:id/edit(.:format) line_items#edit
line_item_path GET (/:locale)/line_items/:id(.:format) line_items#show
PATCH (/:locale)/line_items/:id(.:format) line_items#update
PUT (/:locale)/line_items/:id(.:format) line_items#update
DELETE (/:locale)/line_items/:id(.:format) line_items#destroy
carts_path GET (/:locale)/carts(.:format) carts#index
POST (/:locale)/carts(.:format) carts#create
new_cart_path GET (/:locale)/carts/new(.:format) carts#new
edit_cart_path GET (/:locale)/carts/:id/edit(.:format) carts#edit
cart_path GET (/:locale)/carts/:id(.:format) carts#show
PATCH (/:locale)/carts/:id(.:format) carts#update
PUT (/:locale)/carts/:id(.:format) carts#update
DELETE (/:locale)/carts/:id(.:format) carts#destroy
store_index_path GET (/:locale)/store/index(.:format) store#index
store_path /(:locale)(.:format) store#index

Default locale parameter, and set locale based on locale parameter.

edit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :set_i18n_locale_from_params
  # ...
  protected
    def default_url_options
      { locale: I18n.locale }
    end
 
    def set_i18n_locale_from_params
      if params[:locale]
        if I18n.available_locales.map(&:to_s).include?(params[:locale])
          I18n.locale = params[:locale]
        else
          flash.now[:notice] = 
            "#{params[:locale]} translation not available"
          logger.error flash.now[:notice]
        end
      end
    end
end

Verify that the routes work.

get /en

Your Downloads

CoffeeScript

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
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
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
get /es

Your Downloads

CoffeeScript

es translation not available

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

15.2 Task J2: translating the store front 14.5 Playtime