Search code examples
ruby-on-rails-3localizationpluralize

Rails 3 and Slovenian pluralization


I'd like to use t('errors', :count => 2) with slovenian translation in Rails 3.0.9 and want it to return "2 napaki" which is a special plural form for slovene language.

I have created locales/sl.yml and have this code:

sl:
  error:
    one: %{count} napaka
    two: %{count} napaki
    other: %{count} napak

But this doesn't seem to work.


Solution

  • Make sure you put your translations in config/locales/sl.yml. You'll also need to create a file config/locales/plurals.rb and put the following code inside:

    # More rules in this file: https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb
    I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
    {
      :'sl' => { :i18n => { :plural => { :rule => lambda { |n| [1].include?(n % 100) && ![11].include?(n % 100) ? :one : [2].include?(n % 100) && ![12].include?(n % 100) ? :two : [3, 4].include?(n % 100) && ![13, 14].include?(n % 100) ? :few : :other }}}}
    }
    

    In your application.rb make sure you set the default locale:

    class Application < Rails::Application
      ...
      config.i18n.default_locale = :sl
      ...
    end
    

    Make sure you restart the server after you make these changes. Besides :one, :two, :other you also have :few for numbers like 3, 4, ...

    You can have also have a look at this gist wich does exactly what you ask.