生产中模型内的 Rails 3 翻译

发布于 2024-12-12 11:46:15 字数 542 浏览 0 评论 0原文

我正在尝试将一个应用程序翻译成日语,一切都很顺利,直到我将其投入生产。

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凝望流年 2024-12-19 11:46:15

您的 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.

兮子 2024-12-19 11:46:15

不要在模型中使用 I18n.t 或转换方法。您可以这样做:

在您的模型中

使用类似的内容将国际化错误添加到模型的 name 属性中(检查文档:ActiveModel/Errors/method-i-add):

self.errors.add(:name, :your_error_key) 
  # The error key could be something like :wrong_name

注意:有时您甚至不需要使用 错误.添加 方法。例如,如果您使用如下方式在模型中添加验证:

validates :name, presence: true

Rails 将使用键 :blank 添加错误(该键取决于验证类型)。换句话说,rails 内部会

在您的语言环境中

发出 self.errors.add(:name, :blank)然后在您的 locale.jp.yml 中可以使用其中任何一个(仅一个):

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

在您的情况下将 [model_name] 替换为 timeseries_forecast,将 [attribute_name] 替换为 your_error_key

例如:

en:
  errors:
    messages:
      your_error_key: "Your error message in english"

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):

self.errors.add(:name, :your_error_key) 
  # The error key could be something like :wrong_name

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:

validates :name, presence: true

Rails will add an error with the key :blank (the key depens on the validation type). In other words rails internally will issue self.errors.add(:name, :blank)

In your locale

Then in your locale.jp.yml can use any of this (just one):

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

In your case replace [model_name] with timeseries_forecast and [attribute_name] with your_error_key

For example:

en:
  errors:
    messages:
      your_error_key: "Your error message in english"
人│生佛魔见 2024-12-19 11:46:15

不要认为通过在类中缓存名称可以提高性能。让它成为一个方法。

class TimeseriesForecast < ActiveRecord::Base
  def self.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
end

# usage
TimeseriesForecast.field_names

更好的是,只返回实际字段并在视图中进行转换,如果您要严格执行 MVC 的话(某些 Rails 方法 - 例如 collection_select - 会更难做到这一点,因此上面的建议)。

Don't think you're improving performance by caching the names in the class. Make it a method instead.

class TimeseriesForecast < ActiveRecord::Base
  def self.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
end

# usage
TimeseriesForecast.field_names

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文