2011-12-18 56 views
6

我創建了新的Rails 3項目。我嘗試用翻譯在我的看法是這樣的:如何在視圖中啓用Rails I18n翻譯錯誤?

= t('.translate_test') 

在我的瀏覽器我看起來"translate_test"代替"my test translation"巫婆我en.yml設置。

我的主要問題 - 爲什麼我看不到像"Missing translation: en ..."這樣的錯誤?

+0

可能重複[Rails:如何處理在測試期間作爲錯誤丟失的語言環境轉換](http://stackoverflow.com/questions/8066901/rails-how-to-treat-locale-translation-missing-as-error -during-test) – Besi

回答

8

在Rails 3中,它們不再向您顯示此文本。如果您檢查html源代碼中的元素,您將看到翻譯缺失消息。

您可以打開關閉回退,儘量把你的環境中或處於初始化以下內容:

config.i18n.fallbacks = false 
11

我創建了這個初始化器raise一個例外 - ARG遊戲過去了,所以你就會知道哪些國際化關鍵不見了!

# only for development and test 
if Rails.env.development? || Rails.env.test? 

    # raises exception when there is a wrong/no i18n key 
    module I18n 
    class JustRaiseExceptionHandler < ExceptionHandler 
     def call(exception, locale, key, options) 
     if exception.is_a?(MissingTranslationData) 
      raise exception.to_exception 
     else 
      super 
     end 
     end 
    end 
    end 

    I18n.exception_handler = I18n::JustRaiseExceptionHandler.new 

end 

Source

+0

非常感謝這個系統檢查所有正在運行測試的_missing翻譯是非常容易的。如果你有一個很好的假設,當然。 – fguillen

3

我用最簡單和查看具體的解決方案在視圖中顯示錯誤時,該翻譯是通過在application.css.scss或任何全局樣式表添加這種風格丟失:

.translation_missing{ 
    font-size: 30px; 
    color: red; 
    font-family: Times; 

    &:before{ 
    content: "Translation Missing :: "; 
    font-size: 30px; 
    font-family: Times; 
    color: red; 
} 
} 
+0

不錯,務實。上述其他方法在Rails 4.x中不適用於我。我創建了一個'debug.css',並有條件地將它包含在我的模板中:'= stylesheet_link_tag'debug',除非Rails.env.production?' – Besi