【问题标题】:sign_in_and_redirect with omniauth-facebook to specific language使用omniauth-facebook 将sign_in_and_redirect 重定向到特定语言
【发布时间】:2012-04-19 11:34:42
【问题描述】:

我正在使用 omniauth-facebook gem 和 rails 3.2 并设计 2.0。

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

http://localhost:3000/en http://localhost:3000/es

gem 适用于英语用户,因为在 omniauth_callbacks_controller.rb 中重定向到 http://localhost:3000/en

这是我的 omniauth_callbacks_controller.rb 用于 facebook:

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

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

我该怎么做?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise omniauth


    【解决方案1】:

    我有类似的问题,并没有找到改变回调 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
    

    【讨论】:

      【解决方案2】:

      我认为您必须将您的omniauth 配置提取到yaml 文件并在回调链接的末尾插入“#{i18n.locale}”。

      【讨论】:

      猜你喜欢
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多