2015-03-25 49 views
1

我有這樣的自定義異常處理程序:的I18n定義異常處理程序不調用`t`,只能用`I18n.translate`

module I18n 
    class MissingTanslationsCollectorExceptionHandler < I18n::ExceptionHandler 
    # Handles exceptions from I18n 
    def call(exception, locale, key, options) 
     if exception.is_a?(I18n::MissingTranslation) 
     binding.pry 
     missing_translations << I18n.normalize_keys(locale, key, options[:scope]) 
     super 
     end 
    end 
    end 
end 

我給你這樣的:

I18n.exception_handler = I18n::MissingTanslationsCollectorExceptionHandler.new 

當使用控制檯,它似乎工作:

$ rails c 
Loading development environment (Rails 4.2.0) 

[1] base » I18n.exception_handler 
=> #<I18n::MissingTanslationsCollectorExceptionHandler:0x00000101c121e8> 
[2] base » I18n.translate 'unknown' 

From: /Users/josh/Documents/Work/MuheimWebdesign/base/src/lib/missing_i18n_exception_handler.rb @ line 11 I18n::MissingTanslationsCollectorExceptionHandler#call: 

    10: if exception.is_a?(I18n::MissingTranslation) 
=> 11:  binding.pry 
    12:  missing_translations << I18n.normalize_keys(locale, key, options[:scope]) 

[1] base(#<I18n::MissingTanslationsCollectorExceptionHandler>) » c 
=> "translation missing: en.unknown" 

但啓動服務器,並擊中缺少的翻譯時,不調用,binding.pry。只有在執行I18n.translate未知時,纔會被調用。爲什麼?

也許它與該功能規格使用水豚自己的進程中運行的事實呢?

更新

這裏是有問題的Rails應用程序: https://github.com/jmuheim/base/tree/features/custom-i18n-exception-handler

我已經在這裏添加了自定義的國際化異常處理程序: https://github.com/jmuheim/base/commit/f2aff30046c7a9f38c4a1faed0953e474099120c

而且我已經添加代碼,應該表現出問題在這裏: https://github.com/jmuheim/base/commit/af484b6b96f41194043e0ad0668a5c288d4a0af3

只需轉到root_path,然後觸發它(一次,不是兩次!)。

+0

你把'class MissingTanslationsCollectorExceptionHandler'放在哪裏? – 2015-04-06 09:31:07

回答

-1

如果你正在使用你的應用程序也可能是一個語法錯誤在{}區域設置文件.yml區域設置語言。

您可以在下面的鏈接中驗證您的yml文件。

http://www.yamllint.com/

我希望它可以幫助。

1

這是因爲的ActionView的t不是一個簡單的別名I18n.translate。除此之外,它會將任何MissingTranslation異常轉換爲跨度在HTML中。

code瞭解詳情。

我想例外泡了,但我不想有指定rescue_format: true每次我叫我的觀點t時間,所以我推翻的Rails的助手總是通過rescue_format: true作爲一個選項。

另一個要注意的是:翻譯助手已經改變了一點從一個Rails的版本到下一個,所以一定要閱讀的實際代碼爲你的版本,看看該怎麼辦。

相關問題