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

有帮助吗?

解决方案

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")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top