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

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

问题描述

我具有特殊验证的属性,我使用message子句来显示仅用于该验证的特殊消息。这里有一个例子:

 验证:电子邮件,存在:true,length:{maximum:60},
格式:{with:valid_email_regex,message:不是有效的电子邮件地址格式。 },
唯一性:{case_sensitive:false}

我想在这里翻译消息但是我不知道该怎么做。



我已经看到了类似这样的例子:message:t(some_value_here)。我不知道这个名字。我尝试过这样的消息:t(:bad_email)。我在我的yaml文件中做了以下内容只是为了尝试一些东西。

  activemodel:
errors:
bad_email :不是有效的电子邮件地址格式。

当我尝试访问我的Rails应用程序时,我遇到以下错误:

  ActionView :: Template :: Error(未定义的方法`t'for#< Class:0x007fefc1b709e0>)
我还在我的yaml文件中尝试过:





  activemodel:
错误:
用户:
bad_email:不是有效的电子邮件地址格式。

我一直在研究这个,一整天。我可以找到的是将内置的错误哈希替换为空白或空白。有没有办法让我自定义错误散列,并在模型中替换它们?在这一点上,我不能像编码一样工作。我希望问题是我如何设置我的yaml文件。我看到了不同版本的如何设置这个。我不知道我应该把它放在积极的模式还是积极的方面。我假设有activemodel,因为那是自定义消息是我要翻译的地方。



任何帮助将不胜感激。这是我第一次翻译应用程序之前需要弄清楚的最后一件事。



更新7/29/2013 7:30 pm CDT



bgates 给我一个非常好的开始,如何设置我的模型文件在YAML文件中接收自定义消息。但是我最终不得不在我的yaml文件中进行以下设置,以找到自定义消息。

  activerecord:
错误:
模型:
用户:
属性:
生物:
no_links:不能包含电子邮件地址或网站链接(URL)。
电子邮件:
bad_email:不是有效的电子邮件地址格式。
用户名:
bad_username:只能包含数字和字母,无特殊字符或空格。


解决方案

使用消息的符号:

 验证:电子邮件,存在:true,length:{maximum:60},
格式:{with:valid_email_regex,message: bad_email},
唯一性:{case_sensitive:false}

然后在yaml文件中p>

  [lang]:
activerecord:
错误:
消息:
bad_email: 只是不正确

如果有一个特定于此模型的翻译,它将覆盖一般一个以上:

  [lang]:
activerecord:
errors:
models:
model_name:#或命名空间/ model_name
属性:
电子邮件:
bad_email:针对无效电子邮件的特定于模型的消息
pre>

如果您编写自定义验证, add_error(:email, bad_email)将执行上述查找,但 errors [:email]<< :bad_email 不会。


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.

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."

When I tried to access my Rails application I got the following error:

ActionView::Template::Error (undefined method `t' for #<Class:0x007fefc1b709e0>)

I also tried this in my yaml file:

activemodel:
  errors:
    user:
      bad_email: "is not a valid email address format."

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.

UPDATE 7/29/2013 7:30 pm CDT

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."

解决方案

Use a symbol for the message:

validates :email, presence:   true, length: { maximum: 60 },
            format:     { with: valid_email_regex, message: :bad_email },
            uniqueness: { case_sensitive: false } 

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"

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天全站免登陆