在生产中的模型中进行Rails 3翻译

编程入门 行业动态 更新时间:2024-10-23 20:21:10
本文介绍了在生产中的模型中进行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.

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

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

更多推荐

在生产中的模型中进行Rails 3翻译

本文发布于:2023-11-16 07:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!