Backo
Backo

Reputation: 18881

How to properly translate Paperclip error messages?

I am using Ruby on Rails 3.2.2 and the Paperclip plugin. Since I would like to translate Paperclip error messages, I use the following code:

class Article < ActiveRecord::Base
  validates_attachment_size :picture,
    :less_than => 4.megabytes,
    :message   => I18n.t('trl_too_big_file_size', :scope => 'activerecord.errors.messages')
end

My .yml files are:

# <APP_PATH>/config/locales/en.yml (english language)
activerecord:
  errors:
    messages:
      trl_too_big_file_size: is too big

# <APP_PATH>/config/locales/it.yml (italian language)
activerecord:
  errors:
    messages:
      trl_too_big_file_size: è troppo grande

My ApplicationController is:

class ApplicationController < ActionController::Base
  before_filter :set_locale

  def set_locale
    # I am using code from http://guides.rubyonrails.org/i18n.html#setting-and-passing-the-locale.
    I18n.locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  end
  # BTW: My `I18n.default_locale` is `:en` (english).
end

However, when I submit the form related to the Paperclip attachment my application generate a not translated text - that is, it generates (and outputs) a not properly translated string because it refers always to the default locale languages error string en.activerecord.errors.messages.trl_too_big_file_size even if the I18n.locale is :it. I made some research (for example, 1, 2), but I still cannot figure out how to properly translate error messages related to the Paperclip gem in model files (see the code above). It seems a bug of the Parperclip gem (also because that seems that I am not the only one to get this issue)...

How can I solve the issue?


P.S.: I think the problem is related to some "file loading order process" of ApplicationController-Paperclip gem... but I do not know how to solve the problem.

Upvotes: 2

Views: 2997

Answers (2)

Peter Goldstein
Peter Goldstein

Reputation: 4555

Instead of using a lambda in the message parameter can't you simply use the appropriate key in the YAML file? Specifically, wouldn't something like:

class Article < ActiveRecord::Base
  validates_attachment_size :picture,
    :less_than => 4.megabytes
end

with an entry in the Italian locale YAML file that looks like

it:
  activerecord:
    errors:
      models:
        article:
          attributes:
            picture:
              less_than: è troppo grande

with a similar entry in the English local YAML file

en:
  activerecord:
    errors:
      models:
        article:
          attributes:
            picture:
              less_than: is too big

Assuming your locale is getting set correctly, and that other ActiveRecord localized messages are getting displayed correctly, then I'd expect this to work as Paperclip's validators use the underlying ActiveRecord message bundles via the I18n.t method.

Upvotes: 3

house9
house9

Reputation: 20624

you might need to use a lambda for the message?

see https://github.com/thoughtbot/paperclip/pull/411

class Article < ActiveRecord::Base
  validates_attachment_size :picture,
    :less_than => 4.megabytes,
    :message => lambda { 
      I18n.t('trl_too_big_file_size', :scope => 'activerecord.errors.messages') 
    }
end

Upvotes: 0

Related Questions