Search code examples
ruby-on-railsinternationalizationrails-i18n

Custom pluralization in Ruby on Rails


I have a month range string, and want to pluralize it that way:

I18n.t :range, min: min_date, max: max_date

If min_date and max_date are equal, it should translate to %{min_date} to %{max_date}, but if they are different I want to show just %{min_date}.

How can I do it having just this at my locale file:

range: one: "%{min_date}" other: "%{min_date} to %{max_date}"


Solution

  • This is not really a job for the i18n interpolation. You are probably much better of to just use a method (e.g. in a helper module). That way, you are being explicit about what you want to achieve and don't need to abuse unrelated mechanisms.

    def render_range(min_date, max_date)
      if min_date == max_date
        min_date.to_s
      else
        I18n.t :range, min: min_date.to_s, max: max_date.to_s
      end
    end
    

    Then, in your locate file, you can specify the range key like this:

    range: "%{min} to %{max}"