I somehow solved the problem (not quite sure if this is the most elegant way) by creating a redirect to run set locale:
In my .rb routes, I added the following outside the locale area:
root to: 'static_pages#redirect'
Here is the full version
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do match '', to: 'static_pages#home', :as => 'home', via: 'get'' devise_for :users, :controllers => { :registrations => :registrations } resources :blogs do resources :comments end get 'tags/:tag', to: 'blogs
Now I have only one root. I also changed root_to to match inside the locale using: as => home, so I can reference it in link_to from the views as home_path.
So, when the user enters xxxx.com - he / she will be redirected, which does the following:
class StaticPagesController < ApplicationController def redirect redirect_to home end def home end
Since the locale is empty, it now checks the locale in the http header, and if users prefer the language to really exist, he / she will be redirected to the desired page / language. For subsequent requests (for example, when the user clicks the link), the locale will be read from the URL parameter.
If there is a more elegant solution for this, feel free to answer - glasses are still needed for grabs.
Georg Keferböck
source share