ruby on rails - Why default locale does not work? -
in application controller, forcing locale handling before filter:
before_filter :set_locale def set_locale #i18n.default_locale en i18n.locale = extract_locale_from_tld || i18n.default_locale end def extract_locale_from_tld parsed_locale = params[:locale] || ((lang = request.env['http_accept_language']) && lang[/^[a-z]{2}/]) #so, english default language. parsed_locale= 'en' if parsed_locale.nil? i18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil end
however, if tried visit http://localhost:3000/ko/lab
result routing error error 404
eventually.
any locale out of following locates result routing error:
any advice?
edit
my route.er:
# -*- encoding : utf-8 -*- myapp::application.routes.draw mount alchemy::engine => 'blog' rails.application.routes.draw filter :pagination, :uuid, :locale end devise_for :users, :controllers => { :omniauth_callbacks => "callbacks", :sessions => "users/sessions", :registrations => "users/registrations"} resources :authentications resources :experiments 'experiments/:id/info_edit' => 'experiments#info_edit',:constraints => { :id => /\d+/ } 'sitemap.xml', :to => 'sitemap#index', :defaults => {:format => 'xml'} match 'lang' => 'home#set_lang', :via => [:get] #match 'free_trial' => 'home#free_trial', :via => [:get] match 'useful_links' => 'home#useful_links', :via => [:get] match 'home' => 'home#home', :via => [:get] match 'contact-us' => 'contact#new', :as => 'contact_us', :via => :get match 'contact' => 'contact#create', :as => 'contact', :via => :post match 'dashboard' => 'experiments#index', :via => [:get] post 'notifications' => 'subscriptions#instant_payment_notification' match 'users_experiments' => 'experiments#users_experiments', :via => [:get] match 'hire_us' => 'home#hire_us', :via => [:get] 'lab' => 'experiments#lab' 'experiments/:id/review-edit' => 'experiments#review', :as => :review 'experiments/:id/cancel-edit' => 'experiments#cancel_edit', :as => :cancel_edit 'experiments/:id/approve-edit' => 'experiments#approve', :as => :approve 'experiments/:id/reject-edit' => 'experiments#reject', :as => :reject 'experiments/:id/profile' => 'experiments#profile', :as => :profile match 'amazon' => 'experiments#amazon', :via => [:get] match 'trial_account' => 'home#trial_account', :via => [:get] match 'student_account' => 'home#student_account', :via => [:get] match 'school_account' => 'home#school_account', :via => [:get] match 'create_account' => 'home#registration_redirect', :via => [:get] match 'users/create_account' => 'home#registration_entrance', :via => [:get] match 'registration_redirect' => 'home#registration_redirect', :via=>[:post] # can have root of site routed "root" # remember delete public/index.html. root :to => 'home#home' "/auth/oauth2/callback" => "auth0#callback" "/auth/verification_complete" => "auth0#verification_complete" "/auth/failure" => "auth0#failure" end
my server log:
started "/ko/lab" 127.0.0.1 @ 2017-09-12 08:58:56 +0300 processing applicationcontroller#routing_error html
parameters: {"path"=>"ko/lab"} user load (202.1ms) select "users".* "users" "users"."id" = $1 order "users"."id" asc limit 1 [["id", 12899]] completed 404 not found in 205ms ko/lab excluded capture: dsn not setactioncontroller::routingerror (ko/lab):
app/controllers/application_controller.rb:35:inrouting_error'
call'
lib/rack/seoredirect.rb:20:in
default local work don't have route matches /:locale/lab
. simple.
you can create localized routes using scope
- rails not you.
scope '(:locale)', locale: /#{i18n.available_locales.join('|')}/ '/' => 'home#home' # map /en home#home 'lab' => 'experiments#lab' # ... end
this time refactor messy routes file using resources helper full extent:
resources :experiments member :info_edit # ... end end
see http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
Comments
Post a Comment