Custom error message for carrierwave 0.9.0 doesn't work for Rails 4

前端 未结 3 2031
遥遥无期
遥遥无期 2021-01-25 13:38

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:
            


        
相关标签:
3条回答
  • 2021-01-25 14:00

    It's fixed now guys:

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

    Thanks for the patience.

    0 讨论(0)
  • 2021-01-25 14:03

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

    0 讨论(0)
  • 2021-01-25 14:05

    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

    0 讨论(0)
提交回复
热议问题