Rails 3 translations within models in production

后端 未结 3 1109
太阳男子
太阳男子 2021-01-25 13:05

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 mod

3条回答
  •  一个人的身影
    2021-01-25 13:34

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

提交回复
热议问题