在生产中的模型中进行Rails 3翻译 [英] Rails 3 translations within models in production

查看:55
本文介绍了在生产中的模型中进行Rails 3翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将应用翻译成日语,直到我将其投入生产之前,一切都进行得很顺利.

I'm trying to translate an app into Japanese and everything was going smoothly until I put it into production.

由于cache_classes现在为true,因此模型中的任何转换都将恢复为默认语言环境.

As cache_classes is now true any translation within a model reverts to the default locale.

我知道我应该直接在yml文件中定位翻译,但是我不确定如何对以下简化代码进行翻译:

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

非常感谢

推荐答案

您的I18n.t()调用是在编译时评估的,因为您正在定义类变量,而不是实例变量.您需要致电I18n.t,以便在运行时对其进行评估.

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.

但是,如果要翻译ActiveRecord字段名称,请使用human_attribute_name并通过YML提供翻译.您无需手动提供翻译,Rails会自动为您处理所有翻译.

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.

相关文档位于 http://guides.rubyonrails.org/i18n.html 第5.1章.

The respective documentation is at http://guides.rubyonrails.org/i18n.html Chapter 5.1.

这篇关于在生产中的模型中进行Rails 3翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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