2012-07-04 97 views
6

我的before_filter使用的ApplicationController爲我的應用程序設置的地點:Ruby on Rails + Devise + I18n:如何設置區域設置?

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :set_locale 

    def set_locale 
    I18n.locale = request.compatible_language_from ["uk", "ru", "de", "en"] 
    end 
end 

它適用於由我寫的控制器。但是,所有設計的信息仍然是英語。

設置config.i18n.default_locale = "uk"(或其他)在config/application.rb的作品,所以我想這麻煩的是能想出的控制器不使用我的before_filter(可能是,它不繼承ApplicationController在所有的(?))。

如何解決此問題?如何使設計使用我的語言環境?

+1

devise使用您的應用程序ApplicationController。這可以通過parent_controller設置進行配置。 – phoet

+0

@Hauleth是的,我做到了。如果我將application.rb中的config.i18n.default_locale設置爲默認的非英文語言環境,它們就會工作 – frp

回答

0

您需要使用prepend_before_action(或prepend_before_filter,但它是別名prepend_before_action並且很快就要被淘汰),所以你應該是這樣的:

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    prepend_before_action :set_locale 

    private 

    def set_locale 
    I18n.locale = request.compatible_language_from [:uk, :ru, :de, :en] 
    end 
end 

注意,這可能會破壞您的視圖中的I18n.locale,因此您可能需要將其設置爲額外的before_action

0

我有這個問題,我的法語devise語言環境正在爲所有人加載,問題在於我的設計語言環境最初是在其自己的文件中構建的 - devise.en.yml。我將它們移動到en.yml文件中,並且所有內容都已修復。

希望這可以幫助未來的人!