生产中模型内的 Rails 3 翻译
我正在尝试将一个应用程序翻译成日语,一切都很顺利,直到我将其投入生产。
由于 cache_classes 现在为 true,模型中的任何翻译都会恢复为默认语言环境。
我知道我可能应该直接在 yml 文件中定位翻译,但我不确定如何针对以下简化代码执行此操作:
class TimeseriesForecast < ActiveRecord::Base
@@field_names = {
:location_name => I18n.t('forecast_timeseries.location_name'),
:local_date_time => I18n.t('forecast_timeseries.local_date_time'),
:zulu_date_time => I18n.t('forecast_timeseries.zulu_date_time'),
:temp_mean => I18n.t('forecast_timeseries.temp_mean')
}
end
非常感谢
I'm trying to translate an app into Japanese and everything was going smoothly until I put it into production.
As cache_classes is now true any translation within a model reverts to the default locale.
I know I'm probably supposed to target the translations directly in the yml file but I'm not sure how I would do that for the following simplified code:
class TimeseriesForecast < ActiveRecord::Base
@@field_names = {
:location_name => I18n.t('forecast_timeseries.location_name'),
:local_date_time => I18n.t('forecast_timeseries.local_date_time'),
:zulu_date_time => I18n.t('forecast_timeseries.zulu_date_time'),
:temp_mean => I18n.t('forecast_timeseries.temp_mean')
}
end
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
I18n.t()
调用会在编译时进行评估,因为您定义的是类变量,而不是实例变量。您需要将调用放入 I18n.t 中,它们将在运行时进行评估。但如果您想翻译 ActiveRecord 字段名称,请使用
human_attribute_name 并通过 YML 提供您的翻译。您不需要手动提供翻译,Rails 会自动为您处理这一切。
相应的文档位于 http://guides.rubyonrails.org/i18n.html 第 5.1 章。
Your
I18n.t()
call is evaluated at compile time since you are defining class variables, not instance variables. You need to put your call to I18n.t where they will be evaluated at runtime.But if you want to translate ActiveRecord field names, use
human_attribute_name
and provide your translations via YML. You do not need to manually provide translations, Rails handles it all for you automatically.The respective documentation is at http://guides.rubyonrails.org/i18n.html Chapter 5.1.
不要在模型中使用 I18n.t 或转换方法。您可以这样做:
在您的模型中
使用类似的内容将国际化错误添加到模型的
name
属性中(检查文档:ActiveModel/Errors/method-i-add):注意:有时您甚至不需要使用
错误.添加
方法。例如,如果您使用如下方式在模型中添加验证:Rails 将使用键
:blank
添加错误(该键取决于验证类型)。换句话说,rails 内部会在您的语言环境中
发出
self.errors.add(:name, :blank)
然后在您的 locale.jp.yml 中可以使用其中任何一个(仅一个):在您的情况下将
[model_name]
替换为timeseries_forecast
,将[attribute_name]
替换为your_error_key
例如:
Don't use I18n.t or translate method in your models. You can do this instead:
In your model
Use something like this to add internationalized errors to the
name
attribute of your model (Check documentation: ActiveModel/Errors/method-i-add):NOTE: Sometimes you won't even need to add errors with
errors.add
method. For example if you add validations in your model with somethind like this:Rails will add an error with the key
:blank
(the key depens on the validation type). In other words rails internally will issueself.errors.add(:name, :blank)
In your locale
Then in your locale.jp.yml can use any of this (just one):
In your case replace
[model_name]
withtimeseries_forecast
and[attribute_name]
withyour_error_key
For example:
不要认为通过在类中缓存名称可以提高性能。让它成为一个方法。
更好的是,只返回实际字段并在视图中进行转换,如果您要严格执行 MVC 的话(某些 Rails 方法 - 例如
collection_select
- 会更难做到这一点,因此上面的建议)。Don't think you're improving performance by caching the names in the class. Make it a method instead.
Better yet, return just the actual fields and do the translation in the view, if you're gonna be strict MVC about it (some Rails methods - like
collection_select
- make it harder to do that though, hence the suggestion above).