覆盖 Rails3 中模型名称的默认复数形式

发布于 2024-11-11 17:48:18 字数 265 浏览 10 评论 0原文

我的语言环境是 :de,我喜欢这样:

Sheet.model_name.human.pluralize # => Belegs

添加尾随“e”而不是“s”

Sheet.model_name.human.pluralize # => Belege

为 Sheet 类 。 我可以以某种方式将其添加到我的 config/locales/models/de.yml 中吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-11-18 17:48:18

首先,您需要停止使用 .pluralize。它使用Inflector(主要用于Rails 内部,例如猜测模型Sheet -> Sheets 的表名称)。

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

您应该做的是使用 :count 选项。

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

这要求您修改 de.yml 如下:

de:

  ...

  activerecord:

    ...

    models:
      sheet:
        one: Beleg
        other: Belege

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
写给空气的情书 2024-11-18 17:48:18

您可以通过以下方式覆盖复数形式:

在 config/initializers/inflections.rb 中

执行以下操作:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'Beleg', 'Belege'
end

You can override pluralizations this way:

In config/initializers/inflections.rb

do:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'Beleg', 'Belege'
end
孤蝉 2024-11-18 17:48:18

如果您不喜欢明确的计数(例如 2),您可以使用 :many 例如

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

或不使用哈希火箭(对于 Ruby >= 1.9):

Sheet.model_name.human(count: :many)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文