在我的 Ruby on Rails 应用程序中,我设置了 OAuth 以使用omniauth gem。一切顺利;但是,将 I18n 添加到应用程序后,我无法让它工作。具体来说,问题在于回调。如何调整我的 routes.rb 代码以使用 I18n 正确处理回调?
路线.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