Ruby on Rails i18n - 想要翻译模型中的自定义消息 [英] Ruby on Rails i18n - Want To Translate Custom Messages in Models
问题描述
我有带有特殊验证的属性,我使用 message 子句来显示仅用于该验证的特殊消息.下面是一个例子:
I have attributes with special validation where I use the message clause to display a special message just for that validation. Here is one example:
validates :email, presence: true, length: { maximum: 60 },
format: { with: valid_email_regex, message: "is not a valid email address format." },
uniqueness: { case_sensitive: false }
我想在这里翻译消息,但我不知道如何翻译.
I would like to translate the message here but I am not sure how to do it.
我见过这样的例子:message: t("some_value_here").我不确定这个名称.我尝试过类似这样的消息:t(:bad_email).我在我的 yaml 文件中做了以下操作只是为了尝试一些东西.
I have seen examples where they type something like this: message: t("some_value_here"). I'm not sure about the designation. I tried something like this message: t(:bad_email). I did the following in my yaml file just to try something.
activemodel:
errors:
bad_email: "is not a valid email address format."
当我尝试访问我的 Rails 应用程序时,出现以下错误:
When I tried to access my Rails application I got the following error:
ActionView::Template::Error (undefined method `t' for #<Class:0x007fefc1b709e0>)
我也在我的 yaml 文件中尝试过这个:
I also tried this in my yaml file:
activemodel:
errors:
user:
bad_email: "is not a valid email address format."
我整天都在断断续续地研究这个.我能找到的只是替换内置的错误哈希,如空白或空.有没有办法让我拥有自定义错误哈希并在模型中替换它们?在这一点上,我无法让 t 按照编码工作.我希望问题在于我如何设置 yaml 文件.我已经看到了如何设置它的不同版本.我不确定是否应该将它放在 activemodel 或 activerecord 下.我假设为 activemodel,因为这是我要翻译的自定义消息.
I have been researching this off and on all day long. All I can find is to replace built-in error hashes like blank or empty. Is there a way for me to have custom error hashes and replace them in the model? At this point I cannot get the t to work as coded. My hope is that the problem is how I have my yaml file set up. I have seen varying versions of how to set this up. I am not sure if I should put this under activemodel or activerecord. I assumed activemodel since that is where the custom message is that I want to translate.
任何帮助将不胜感激.这是我在启动应用程序的第一次翻译之前需要弄清楚的最后一部分.
Any help would be appreciated. This is the last piece I need to figure out before launching my first translation of the application.
更新 7/29/2013 7:30 pm CDT
bgates 在如何设置模型文件以接收 YAML 文件中的自定义消息方面给了我一个很好的开端.但是,我最终不得不在我的 yaml 文件中进行以下设置才能找到自定义消息.
bgates gave me a very good start with how to setup my model files to receive the custom message in the YAML file. However I ended up having to do the following setup in my yaml file for the custom messages to be found.
activerecord:
errors:
models:
user:
attributes:
bio:
no_links: "cannot contain email addresses or website links (URLs)."
email:
bad_email: "is not a valid email address format."
username:
bad_username: "can only contain numbers and letters. No special characters or spaces."
推荐答案
使用符号表示消息:
validates :email, presence: true, length: { maximum: 60 },
format: { with: valid_email_regex, message: :bad_email },
uniqueness: { case_sensitive: false }
然后在 yaml 文件中
then in the yaml file
[lang]:
activerecord:
errors:
messages:
bad_email: "just ain't right"
如果有特定于此模型的翻译,它将覆盖上面的通用翻译:
If there's a translation specific to this model, it will override the general one above:
[lang]:
activerecord:
errors:
models:
model_name: # or namespace/model_name
attributes:
email:
bad_email: "model-specific message for invalid email"
如果您编写自定义验证,add_error(:email, :bad_email)
将执行上面的查找,但 errors[:email] <<:bad_email
不会.
If you write custom validations, add_error(:email, :bad_email)
will do the lookup above, but errors[:email] << :bad_email
will not.
这篇关于Ruby on Rails i18n - 想要翻译模型中的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!