在Ruby on Rails中使用I18n设置OAuth回调

时间:2012-07-16 08:39:24

标签: ruby-on-rails ruby-on-rails-3 oauth internationalization routes

在我的Ruby on Rails应用程序中,我已经使用omniauth gem设置了OAuth。一切顺利;但是,一旦我将I18n添加到应用程序中,我无法将其工作。具体来说,问题在于回调。如何调整我的routes.rb代码以使用I18n正确处理回调?

的routes.rb

 scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
   resources :sessions, only: [:new, :create, :destroy]
   root to: 'static_pages#home'

   match 'auth/:provider/callback', to: 'sessions#create'
   match 'auth/failure', to: redirect('/')
   match 'signout', to: 'sessions#destroy', as: 'signout'

   match '/signin',  to: 'sessions#new'
   match '/signout', to: 'sessions#destroy', via: :delete
 end

  match '*path', to: redirect {|params| "/#{I18n.default_locale}/#{CGI::unescape(params[:path])}" }, constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
  match '', to: redirect("/#{I18n.default_locale}")

application_controller.rb

   before_filter :set_locale
     def set_locale
       I18n.locale = params[:locale] if params[:locale].present?
     end

     def default_url_options(options = {})
       {locale: I18n.locale}
     end

1 个答案:

答案 0 :(得分:0)

可以找到这个问题的答案here。 :locale需要在括号中:scope "(:locale)"