如何将抛出的消息国际化
class Foo < ActiveRecord::Base
...
validates_presence_of :name, :message => "cannot be blank.
...
我正在寻找使消息国际化的方法,以便来自不同区域的用户可以显示本地化的消息。我该怎么做呢?
class Foo < ActiveRecord::Base
...
validates_presence_of :name, :message => "cannot be blank.
...
I am looking for ways to internationalize the message so that a localized message is shown from users from a different locale. How should I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails Guides:活动记录的翻译将为您提供暗示一下。基本上,您不必将字符串存储在模型中。您可以在区域设置文件中更改它。
Rails Guides: Translations for active records will give you a hint about it. Basically, you don't have to store the string in your model. You can change it in your locale files.
I18n 是将消息国际化的好方法之一。
但是,如果您的消息自定义级别太多,那么每次您都需要修改 yml,并且还需要为不同的用户组添加新的 yml。
在这种情况下,您可以使用“redis”,它可以为您提供更多动态控制。它将数据存储在 key => 中值哈希。真的很快。以下链接将帮助您
http://redis.io/documentation
http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html
https://github.com/jodosha/redis-store
http://www.engineyard.com/blog/2009/key-value-stores-for-ruby-part-4-to-redis-or-not-to-redis/
I18n is one of the good way to internationalize the message.
But if your message customization level is too much then every time you need to modify the ymls and also need to add new yml for different set of users.
In such case you can use 'redis' it gives you more dynamic control. It store data in key => value hash. It is really fast. Following links will help youl
http://redis.io/documentation
http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html
https://github.com/jodosha/redis-store
http://www.engineyard.com/blog/2009/key-value-stores-for-ruby-part-4-to-redis-or-not-to-redis/