如何将 human_attribute_name 复数化为 Rails 5 和 I18n?

How to pluralize human_attribute_name with Rails 5 and I18n?

我想在我的应用程序中翻译和复数化属性名称。

因此,对于我的 api_key 属性,我有翻译 "Clef API"。

我用 Mymodel.human_attribute_name(:api_key).pluralize(2) 将属性名称复数化并得到结果: "Clef APIs"

但在法语中,"Clef API" 的复数形式是 "Clefs API"

所以我尝试在我的 YML 中提供复数翻译,但它不起作用,我仍然得到 "One" 的翻译,最后用 's' 复数。

fr.yml :

activerecord:
  attributes:
    mymodel:
      api_key:
        one: "Clef API"
        other: "Clefs API"  

我可以 Mymodel.human_attribute_name(:api_key).pluralize(2) 到 return "Clefs API" 吗?

如果没有,我该如何翻译和复数我的属性名称?

在发布我的问题后,我应该查看文档以获取 human_attribute_name 的提示。它确实解决了我的问题...

原来 human_attribute_name 直接处理复数。它接受带有键 :countoptions 散列,它确实像其他 I18n 方法一样触发复数。

所以,就这么简单 Mymodel.human_attribute_name(:api_key, count: 2)

您可以在 config/initializers/inflections.rbInflectors 中添加自定义复数词。您可以查看下面的代码示例:

ActiveSupport::Inflector.inflections(:fr) do |inflect|
  inflect.irregular 'Clef API', 'Clefs API'
end