Search code examples
ruby-on-railsrails-i18n

Why does `1.ordinalize` work in locales :en & :fr but not in :de in Ruby on Rails with rails-i18n gem?


In Ruby on Rails the ordinalize method does not work in the :de locale. The error given is:

 > 1.ordinalize
=> "translation missing: de.number.nth.ordinalized"

It is easy to reproduce this:

  1. Create a brand new rails application with the command rails new test1
  2. Install the rails-i18n gem by adding it to the Gemfile and running bundle
  3. Copy and edit the config/locale/en.yml to create fr.yml and de.yml with the following edits:
    ==> config/locales/de.yml <==
    de:
      hello: "Hallo Welt"
    
    ==> config/locales/en.yml <==
    en:
      hello: "Hello world"
    
    ==> config/locales/fr.yml <==
    fr:
      hello: "Bonjour le monde"

Then try and use ordinalize in rails console:

test1 git:(main) ✗ rails console
Loading development environment (Rails 7.0.4.3)
3.1.2 :001 > I18n.locale
 => :en
3.1.2 :002 > 1.ordinalize
 => "1st"
3.1.2 :003 > I18n.locale = :fr
 => :fr
3.1.2 :004 > 1.ordinalize
 => "1er"
3.1.2 :005 > I18n.locale = :de
 => :de
3.1.2 :006 > 1.ordinalize
 => "translation missing: de.number.nth.ordinalized"

Looking at the rails-i18n source there appears to be code there to handle the number.nth.ordinalized translation key in German and French

If I manually add a number.nth.ordinalized key into my de.yml file then that translation is found.

Please can someone help me understand why this is not working and ideally explain what needs to change to make it work.

Thanks very much in advance.


Solution

  • The last release (7.0.6) of the rails-i18n gem was in November last year. The German ordinal code was added in February of this year.

    You can wait for a release or switch to the main branch by changing the line in your Gemfile to:

    gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'master'
    

    After which you should be able to get:

    [2] pry(main)> I18n.locale = :de
    => :de
    [3] pry(main)> 1.ordinalize
    => "1."