Question

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"?

Was it helpful?

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top