Custom error message for carrierwave 0.9.0 doesn&#

2019-02-28 23:24发布

问题:

I use carrierwave 0.9.0 with Rails 4 and I'm trying to make a custom error message. After doing some search, I found this answer:

en:
  errors:
    messages:
      extension_white_list_error: 'My Custom Message'

I tried this by going to config/locales/en.yml then adding the code above, but nothing changed. The error message is still the same.

There is also the same issue on github here, but no answers, I think this problem is specific just with Rails 4 but not sure, have you the same problem when you use Rails 4?

回答1:

There seem be some loading issue with Rails 4.0 with regards to i18n files

Ideally what should be I18n load paths as per 3+

/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activesupport-4.0.0/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activemodel-4.0.0/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activerecord-4.0.0/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/actionpack-4.0.0/lib/action_view/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/locale/en.yml
/Users/joshianiket22/carrierwave_tester/config/locales/en.yml

What is seen in Rails 4.0

/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activesupport-3.2.11/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activemodel-3.2.11/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activerecord-3.2.11/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/actionpack-3.2.11/lib/action_view/locale/en.yml
/Users/joshianiket22/workspace/zenjavi/carrierwave_tester/config/locales/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/validations/../locale/en.yml

One can clear see the difference between the two the carrierwave en.yml is loaded after a the application specific en.yml and there is your issue

I suggest there is no easy way unless you the change the load_paths in rails application and some how manage to change the order of load_paths of as expected

I have given a pull request over here. Completely at awe of Carrierwave guys to decide on it

Hacky Solution :

I was refraining in giving you this solution earlier but still if you want it that bad here what you can do

define a file in lib directory(let say auto_load_i18n.rb) and assign the lib path to autoload (in application.rb)

config.autoload_paths += %w(#{config.root}/lib)

Inside auto_load_i18n.rb write this

I18n.load_path.delete(Rails.root.join("config/locales/en.yml").to_s)
I18n.load_path << Rails.root.join("config/locales/en.yml").to_s

And require the lib file at the top of your application_controller.rb

require 'auto_load_i18n'
class ApplicationController < ActionController::Base

and I guess everything would work then

You can now understand as to why I was refraining in giving this as a possible solution :)

Hope this help



回答2:

It's fixed now guys:

https://github.com/carrierwaveuploader/carrierwave/pull/1264

Thanks for the patience.



回答3:

I use a rails 5.1 I created a file config/locales/carrierwave.ar.yml and wrote

ar:
  carrierwave:
  errors:
    messages:
      min_size_error: "حجم الصورة لابد أن يكون أكبر من %{min_size}"
      max_size_error: "حجم الصورة لابد أن يكون أقل من %{max_size}"
      ....

You can take a look at this file :)