使用omniauth-facebook将sign_in_and_redirect重定向到特定语言 [英] sign_in_and_redirect with omniauth-facebook to specific language

查看:77
本文介绍了使用omniauth-facebook将sign_in_and_redirect重定向到特定语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 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

这是我的 facebook的 omniauth_callbacks_controller.rb :

  def facebook@user = User.find_for_facebook_oauth(request.env ["omniauth.auth"],current_user)如果@ user.persisted?flash [:notice] = I18n.t"devise.omniauth_callbacks.success",:kind =>Facebook"sign_in_and_redirect @user,:event =>:验证别的session ["devise.facebook_data"] = request.env ["omniauth.auth"]redirect_to new_user_registration_url结尾结尾 

然后是西班牙用户的问题.如果他们使用 http://localhost:3000/es 从回调的重定向转到 http://localhost:3000/en

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

我该怎么做?

谢谢!

解决方案

我遇到了类似的问题,并且没有找到更改回调URL的方法,但是我可以在发生回调时在方法中设置语言环境.

原始网址(具有正确的原始语言环境)存储在 request.env ['omniauth.origin']

因此,在 facebook 方法中,从原始网址中选择语言环境,这与域部分后面两个字母的位置类似.

我在 facebook 方法的开头添加了

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

exctract_locale_from_url 是一个令人讨厌的正则表达式:)

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

I'm using omniauth-facebook gem with rails 3.2 and devise 2.0.

I have a website with 2 languages, english and spanish.

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

The gem works fine for english users because in omniauth_callbacks_controller.rb the redirect go to http://localhost:3000/en

This is my omniauth_callbacks_controller.rb for 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

Then problem is for spanish users. that if they use http://localhost:3000/es the redirect from callback go to http://localhost:3000/en

I want that redirect from callback go to specific language that is using that user.

How can I do it?

Thank you!

解决方案

I had similar issue, and did not find a way to change the callback url, but I could set the locale in the method when the callback happens.

The original url (which has the orignal correct locale) is in stored in request.env['omniauth.origin']

So in facebook method pick the locale from the original url, where it is in similar way the two letters after the domain part.

I added in the beginning of the facebook method:

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

Where the exctract_locale_from_url is a horrible looking regexp :)

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

这篇关于使用omniauth-facebook将sign_in_and_redirect重定向到特定语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆