Search code examples
ruby-on-railstranslationplural

Rails: Getting a Pluralize-able Translation Without a Count


I have a model with a translation that can be pluralized:

en:
  activerecord:
    models:
      user:
        one: User
        other: Users

If I call this translation directly, I get these results:

t("activerecord.models.user", count: 1)
=> User

t("activerecord.models.user", count: 2)
=> Users

t("activerecord.models.user")
=> {:one=>"User", :other=>"Users"}

Is there a way to get this last translation to default to "User"?


Solution

  • If you are going to sometimes pass a count and other times not, you should set up separate keys for them:

    counted_user:
      one: User
      other: Users
    user: User
    
    t("activerecord.models.counted_user", count: 1)
    t("activerecord.models.counted_user", count: 2)
    t("activerecord.models.user")
    

    If the default is only used in minimal circumstances that don't warrant a separate key, just call it directly:

    t("activerecord.models.user.one")