Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#ordinal and #ordinalize now support I18n #32168

Merged

Conversation

christianblais
Copy link
Contributor

@christianblais christianblais commented Mar 5, 2018

ActiveSupport::Inflector#ordinal and ActiveSupport::Inflector#ordinalize now support translations through I18n.

        {
          fr: {
            number: {
              nth: {
                ordinals: lambda do |_key, number:, **_options|
                  if number.to_i.abs == 1
                    'er'
                  else
                    'e'
                  end
                end,

                ordinalized: lambda do |_key, number:, **_options|
                  "#{number}#{ActiveSupport::Inflector.ordinal(number)}"
                end
              }
            }
          }
        }

Summary

The current implementation of ActiveSupport::Inflector#ordinal and ActiveSupport::Inflector#ordinalize only work in English. The st, nd, and th logic is all hardcoded, and obviously doesn't work in other languages. This patch moves that logic to I18n so it can be defined on a per-language basis.

This is technically a noop; English should still work as usual, and no other language was added.

Other Information

I found one previous attempt here, dating back to 2014. Unfortunately, the approach wasn't flexible enough to support most languages.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kamipo (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

…nalize`

now support translations through I18n.

        {
          fr: {
            number: {
              nth: {
                ordinals: lambda do |_key, number:, **_options|
                  if number.to_i.abs == 1
                    'er'
                  else
                    'e'
                  end
                end,

                ordinalized: lambda do |_key, number:, **_options|
                  "#{number}#{ActiveSupport::Inflector.ordinal(number)}"
                end
              }
            }
          }
        }
@christianblais
Copy link
Contributor Author

@kamipo any update on this?

@olimart
Copy link
Contributor

olimart commented Dec 19, 2018

Salut @christianblais
I can't seem to make it work.
I've added config/locales/fr.rb as per your example, restarted the server.
My view keeps rendering English default values
<%= @lease.day_of_payment.ordinalize %>
Current locale is indeed :fr
What am I missing?

@christianblais
Copy link
Contributor Author

What am I missing?

This will only be released with Rails 6 - it's not out yet. If you are on Rails 6 and still have some problems, feel free to contact me in private and we can walk through it together.

@asecondwill
Copy link

What if you already have a huge en.yml translation file. Have to convert it to ruby hash to have ordinalized dates when doing

<%= l(Date.today, :format => :short, locale: :'en') %>

@asecondwill
Copy link

asecondwill commented Nov 1, 2019

Silly me, you can just have en.yml and en.rb

{
  :en => {
    :date => {
      :formats => {
        :short => lambda { |date, _| "#{date.day.ordinalize}  %b %Y" }
      }
    }
  }
}

Lovely, thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants