28

my locale is :de and I like to get this:

Sheet.model_name.human.pluralize # => Belegs

to add me a trailing "e" instead of "s"

Sheet.model_name.human.pluralize # => Belege

just for the Sheet-class. Can I add it somehow in my config/locales/models/de.yml ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
toy
  • 543
  • 1
  • 6
  • 16

3 Answers3

54

First of all, you need to stop using .pluralize. It uses the Inflector (which is mainly used for Rails internals, e.g. guessing table names for model Sheet -> sheets).

Sheet.model_name.human # => "Beleg"
"Beleg".pluralize # => "Belegs"

What you should do is to use the :count option.

Sheet.model_name.human(:count => 2) # => "Belege"

This requires that you have modified your de.yml as such:

de:

  ...

  activerecord:

    ...

    models:
      sheet:
        one: Beleg
        other: Belege
Marcel Jackwerth
  • 53,948
  • 9
  • 74
  • 88
  • looks good, I tried but seems to not work for model-class-names. – toy May 31 '11 at 20:23
  • 1
    I added an explanation why you can't use pluralize. The inflector isn't meant to solve i18n problems. – Marcel Jackwerth Jun 01 '11 at 10:57
  • 2
    Model name in the locale file should be singular, not plural ("sheet", not "sheets"). – Jari Jokinen Aug 28 '11 at 10:58
  • 1
    @Marcel Jackwerth : Just curious. In the YAML file, you use "one" and "other". Is there an equivalent to "other" in the `human` function (eg `Sheet.model_name.human(:count => lots)`? It just seems odd to put 2 in there if there can be hundreds of entries.. – Shawn Sep 29 '11 at 03:20
  • 3
    @Shawn Actually you can use `100000` or anything you want. Using `:count => "foo"` will lookup the `other` translation as well. The current implementation only checks `:zero if count == 0` and `count == 1 ? :one : :other`. Here is the [full code](https://github.com/svenfuchs/i18n/blob/v0.6.0/lib/i18n/backend/base.rb#L132). – Marcel Jackwerth Sep 29 '11 at 11:34
  • this works and looks quite good (if a bit hacky), but is it the recommended standard way to do it? any references? – tokland Apr 29 '12 at 10:10
15

You can override pluralizations this way:

In config/initializers/inflections.rb

do:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'Beleg', 'Belege'
end
bbonamin
  • 30,042
  • 7
  • 40
  • 49
2

If you don't like explicit count number (like 2) you could use :many e.g.

Sheet.model_name.human(count => :many)

or without hash rocket (for Ruby >= 1.9):

Sheet.model_name.human(count: :many)
jmarceli
  • 19,102
  • 6
  • 69
  • 67