2013-11-22 93 views
0

我有這樣的代碼顯示本地化的文本:翻譯simple_form錯誤信息

<%= f.input :full_name, :label => _('registration.form.label.name') + ':', :required => false -%> 
    <%= f.input :company, :label => _('registration.form.label.company') + ':', :required => false -%> 

正如你所看到的,標籤正在使用的按鍵和外部系統翻譯。標籤部分正常工作。但是,錯誤消息(「不能爲空」等)不會被翻譯。最簡單的方法是通過將它們包裝在_()標籤中來自定義錯誤消息,但我似乎無法找到它的位置。

什麼是翻譯錯誤信息的最簡單方法?最好使用當前翻譯系統。

+1

請問[this](http://stackoverflow.com/questions/12955208/translating-custom-error-messages)能幫助你嗎? – PinnyM

+0

謝謝你的回答,但這對我沒有多大的幫助。我正在使用統一的翻譯管理系統,並且更改用於翻譯的yml文件是不可能的。最簡單的解決方案是找到正確的變量來包裝_()標籤,一切都會好的。 – Lightheaded

回答

-1

解決方案是創建自定義錯誤消息。 (這裏的例子:Customize error message with simple_form

validates_length_of :name, :minimum => 5, :message => "blah blah blah" 

,並利用它們是這樣的:

<%= f.input :name, :error_html => { :id => "name_error"} %> 
+0

這是真正的「THE」解決方案嗎?我認爲我們的目標是將郵件放在簡單的yaml文件中... – Matthias

+0

對不起,這是我發現的唯一解決方法。 – Lightheaded

+0

當然,不用擔心。你對這種方法有什麼看法? http://stackoverflow.com/a/36410506/5521564 – Matthias

0

你可以在錯誤信息簡單地添加到activerecord YAML

en: 
    activerecord: 
    models: 
     mymodel: 
     attributes: 
      name: 
      too_short: "%{attribute} is too short." 

這種方式,您也可以參考您的yaml中的attribute名稱。

當然,理想情況下,您可以將標籤和驗證放在simple_form i18n yaml中,但我無法弄清楚。任何建議,這是受歡迎的。