Application controller does not run when accessing a domain without redirecting to router.rb by default

Using locale in my rails application. I have the following routes. Rb

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do root to: 'static_pages#home' devise_for :users, :controllers => { :registrations => :registrations } resources :blogs do resources :comments end get 'tags/:tag', to: 'blogs#index', as: :tag resources :users get '/users/subregion_options' => 'users#subregion_options' resources "contacts", only: [:new, :create] match '/crew', to: 'static_pages#crew', via: 'get' .... end match '*path', to: redirect("/#{I18n.locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }, via: 'get' 

I would like the user who accessed the page to be redirected to the language depending on the language browser settings (http-header). It all works - basically. However, access to localhost: 3000 / application_controller.rb is not called - therefore set_locale is not executed, which means that the user ends the welcome rails page

How can I get set_locale to be called?

 class ApplicationController < ActionController::Base before_filter :set_locale def set_locale if params[:locale].blank? redirect_to "/#{extract_locale_from_accept_language_header}" else I18n.locale = params[:locale] end end private def extract_locale_from_accept_language_header case request.env['HTTP_ACCEPT_LANGUAGE'].try(:scan, /^[az]{2}/).try(:first).try(:to_sym) when 'en' 'en' when 'de' 'de' when 'de-at' 'de' when 'de-ch' 'de' else 'en' end end def default_url_options(options = {}) {locale: I18n.locale} end 
0
source share
1 answer

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#index', as: :tag resources :users get '/users/subregion_options' => 'users#subregion_options' resources "contacts", only: [:new, :create] match '/crew', to: 'static_pages#crew', via: 'get' .... end 

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.

+1
source

All Articles