Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure Rails routes to accommodate i18n
    text
    copied!<p>I'm in the process of adding internationalization to a Rails app, and have more-or-less followed the relevant <a href="http://guides.rubyonrails.org/i18n.html">Rails Guide</a> and <a href="http://railscasts.com/episodes/138-i18n-revised">Railscast</a>.</p> <p>I've run into two issues:</p> <ol> <li>How can I add a "home" link that redirects to the current locale. Currently I have root_path links, but these are failing due to the line in routes.rb designed to pick up unscoped roots. This means that root_path always directs to default locale, not current locale.</li> <li>I have everything set up and working locally (except the issue above) but deploying to Heroku all urls appear to be dropping through my routes file and getting caught by one of the catch alls. They are redirecting to '/' under the defulat locale.</li> </ol> <p>My set up is as follows</p> <p>application_controller.rb</p> <pre><code>before_filter :set_locale def default_url_options(options = {}) {locale: I18n.locale} end private def set_locale I18n.locale = params[:locale] if params[:locale].present? end </code></pre> <p>routes.rb</p> <pre><code>scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do all_my_routes # handles /valid-locale root to: 'home#index', as: "localized_root" # handles /valid-locale/fake-path match '*path', to: redirect { |params, request| "/#{params[:locale]}" } end # handles / root to: redirect("/#{I18n.default_locale}") # handles /bad-locale|anything/valid-path match '/*locale/*path', to: redirect("/#{I18n.default_locale}/%{path}") # handles /anything|valid-path-but-no-locale match '/*path', to: redirect("/#{I18n.default_locale}/%{path}") </code></pre> <p>My home link:</p> <pre><code>&lt;%= link_to "Home", root_path %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload