2017-06-22 15 views
0

複数の言語を並行して翻訳するために私のビューではI18n翻訳を手作業で行う必要がありますが、Deviseは現在のロケールに基づいて内部的にフラッシュメッセージを翻訳し、結果の文字列をflashに設定します。DeviseController#find_messageをどのように上書きできますか?

は、私は私が

zh = t(key, locale: 'zh-CN') 
en = t(key, locale: 'en') 

それは問題のコードのように見えるDeviseController#find_messageあるような何かを行うことができるように、それはちょうどflashに国際化キーやオプションを追加する必要がありますが、私はどのようにを把握することはできません...私はconfig/initializers/devise.rb

DeviseController.class_eval do 
    # Get message for given 
    def find_message(kind, options = {}) 
    options[:scope] ||= translation_scope 
    options[:default] = Array(options[:default]).unshift(kind.to_sym) 
    options[:resource_name] = resource_name 
    options = devise_i18n_options(options) 

    # return the i18n key itself to facilitate multiple translations 
    # I18n.t("#{options[:resource_name]}.#{kind}", options) 
    {key: "#{options[:resource_name]}.#{kind}", options: options} 
    end 
end 

を試してみたが、効果がないようだオーバーライド/モンキーパッチ。

どのようにこの動作を上書きまたは修正できますか?

私はDevits 4.3.0(最新の書き込み時)とRails 5.1.1を使っています。

ありがとうございます。

答えて

0

私はそれを理解しました。探している人は誰ですか:Devise::FailureApp#i18n_messageを上書きします。

これが働いた:

Devise::FailureApp.class_eval do 
    protected 
    def i18n_message(default = nil) 
     message = warden_message || default || :unauthenticated 

     if message.is_a?(Symbol) 
     options = {} 
     options[:resource_name] = scope 
     options[:scope] = "devise.failure" 
     options[:default] = [message] 
     auth_keys = scope_class.authentication_keys 
     keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key) } 
     options[:authentication_keys] = keys.join(I18n.translate(:"support.array.words_connector")) 
     options = i18n_options(options) 

     # I18n.t(:"#{scope}.#{message}", options) 
     {key: :"#{scope}.#{message}", options: options} 
     else 
     message.to_s 
     end 
    end 
end 
関連する問題