2012-04-19 60 views
0

我使用的是omniauth-facebook gem與rails 3.2和devise 2.0。用omniauth-facebook將sign_in_and_redirect重定向到特定語言

我有一個網站有2種語言,英語和西班牙語。

http://localhost:3000/en 的http://本地主機:因爲在omniauth_callbacks_controller.rb重定向到http://localhost:3000/en

這是我omniauth_callbacks_controller 3000/ES

創業板工作正常英語用戶。 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

我想從回調重定向到使用該用戶的特定語言。

我該怎麼辦?

謝謝!

回答

4

我有類似的問題,並沒有找到一種方法來改變回調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 
+0

謝謝你的工作很好:D – hyperrjas 2012-05-19 11:05:52

0

我認爲你必須將你的omniauth配置提取到yaml文件並在回調鏈接的末尾插入「#{i18n.locale}」。

+0

謝謝您的答覆,我也有同樣的問題,這鏈接http://stackoverflow.com/questions/7279387/devise-doesnt-redirect-properly-to-stored-location-when-using-omniauth-provider。你能寫一個解決方案的例子嗎?謝謝! – hyperrjas 2012-04-19 17:14:27