Agile Web Development with Rails, Edition 4

16.2 Task K2: translating the store front 15.5 Playtime

16.1 Task K1: 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
    post 'store/index'
    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 POST (/:locale)/store/index(.:format) store#index
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 Pragmatic Catalog

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

es translation not available

Your Pragmatic Catalog

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

16.2 Task K2: translating the store front 15.5 Playtime