0

After coding the app in English, I updated the language file (pt-BR.yml), the 'config/application.rb' (setting the default to pt-BR), and the 'inflections.rb'in order to have the error messages in my local language.

However, Rails now does not find my model because its logic does not pluralize in English anymore.

Is there a way to prevent Rails to use the local default language in models and controllers? Or is there a better coding practice for it? Thanks.

alainbex
  • 396
  • 4
  • 11

1 Answers1

0

You can configure your inflections.rb rather than converting the default lang. You can do that like so:

ActiveSupport::Inflector.inflections(:es) do |inflect|


inflect.plural(/$/, 's')
  inflect.plural(/([^aeéiou])$/i, '\1es')
  inflect.plural(/([aeiou]s)$/i, '\1')
  inflect.plural(/z$/i, 'ces')
  inflect.plural(/á([sn])$/i, 'a\1es')
  inflect.plural(/é([sn])$/i, 'e\1es')
  inflect.plural(/í([sn])$/i, 'i\1es')
  inflect.plural(/ó([sn])$/i, 'o\1es')
  inflect.plural(/ú([sn])$/i, 'u\1es')

  inflect.singular(/s$/, '')
  inflect.singular(/es$/, '')

  inflect.irregular('el', 'los')
end

Code taken from https://davidcel.is/posts/edge-rails-a-multilingual-inflector/

It looks like his gem also supports pt-BR https://github.com/davidcelis/inflections. I haven't personally tried it but it looks sane.

ruby_newbie
  • 3,190
  • 3
  • 18
  • 29
  • Thanks a lot! while I still don't know exactly how to select the model language, it helped me to have inflections in more than one language. – alainbex Aug 03 '16 at 19:26