Rails 3 translations within models in production

后端 未结 3 1186
太阳男子
太阳男子 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:19

    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"
    

提交回复
热议问题