Ruby on Rails i18n - 想要在模型中翻译自定义消息 [英] Ruby on Rails i18n - Want To Translate Custom Messages in Models

查看:16
本文介绍了Ruby on Rails i18n - 想要在模型中翻译自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有特殊验证的属性,我使用消息子句来显示仅用于该验证的特殊消息.这是一个例子:

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.

更新 2013 年 7 月 29 日下午 7:30 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文件中

[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆