sign_in_and_redirect使用全能身份验证-Facebook到特定语言



我正在使用带有 rails 3.2 和 designise 2.0 omniauth-facebook gem。

我有一个包含两种语言的网站,英语和西班牙语。

http://localhost:3000/enhttp://localhost:3000/es

gem 对英语用户来说效果很好,因为在 omniauth_callbacks_controller.rb 中重定向到http://localhost:3000/en

这是我的Facebook omniauth_callbacks_controller.rb

def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

那么问题就出在西班牙用户身上。 如果他们使用http://localhost:3000/es则从回调重定向转到http://localhost:3000/en

我希望从回调重定向到使用该用户的特定语言。

我该怎么做?

谢谢!

我遇到了类似的问题,并且没有找到更改回调 url 的方法,但是当回调发生时,我可以在方法中设置区域设置。

原始 URL(具有原始正确的区域设置)存储在 request.env['omniauth.origin']

因此,facebook方法中从原始URL中选择区域设置,其中域部分后面的两个字母的方式相似。

我在facebook方法的开头添加了:

I18n.locale = exctract_locale_from_url(request.env['omniauth.origin']) if request.env['omniauth.origin']

exctract_locale_from_url是一个看起来很可怕的正则表达式:)

def exctract_locale_from_url(url)
  url[/^([^/]*//)?[^/]+/(w{2})(/.*)?/,2]
end

我认为您必须将全身份验证配置提取到 yaml 文件并在回调链接的末尾插入"#{i18n.locale}"。

最新更新