Agile Web Development with Rails 7: Suggestion to simplify Chapter 15 Task J: Internationalization

@rubys

A few suggestions for Chapter 15, Task J internationalization which will solve a few of the other open errata and simplify things slightly:

  1. Add the following code to application_controller.rb so that the locale is added by default when navigating:
  def default_url_options
    { locale: I18n.locale }
  end

and the corresponding code to test_helper.rb:

module ActionDispatch::Integration
  class Session
    def default_url_options
      { locale: I18n.locale }
    end
  end
end

We could then get rid of the locale: I18n.locale argument on the following pages:
Pg. 234: <%= button_to t('.checkout'), new_order_path(locale: I18n.locale),
Pg. 243: format.html { redirect_to store_index_url(locale: I18n.locale),
Pg. 243: assert_redirected_to store_index_url(locale: 'en')
Pg. 420: line_items_path(product_id: product, locale: I18n.locale),

It would also fix a couple of the open errata’s regarding the missing locale argument on other pages.

  1. Update the home link in the sidebar to be <li><%= link_to t('.home'), store_index_path %></li> so that Rails keeps the selected localization when this link is clicked.

  2. Modify the localization parameter in routes.rb as follows: scope '(:locale)', locale: /en|es/ do so that the other (not yet valid) questions / news / contacts links in the sidebar don’t get interpreted as locales.

  3. In Section J4, add the following CSS to application.css (or suitable file) so the language selector appears in the same spot as in the book’s image:

.locale {
  float: right;
  margin: 1em;
}