如何使“存在/存在N个对象"复数? [英] How to pluralize "There is/are N object/objects"?

查看:109
本文介绍了如何使“存在/存在N个对象"复数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对单个单词进行复数很简单:

Pluralizing a single word is simple:

pluralize(@total_users, "user")

但是,如果我要打印有个/一个 N个用户",该怎么办?

But what if I want to print "There is/are N user/users":

有0个用户
有1位用户
有2位用户

There are 0 users
There is 1 user
There are 2 users

,即如何使句子 复数?

, i.e., how to pluralize a sentence?

推荐答案

您可以为其添加自定义变形.默认情况下,Rails会将inflections.rb添加到config/initializers.您可以在其中添加:

You can add a custom inflection for it. By default, Rails will add an inflections.rb to config/initializers. There you can add:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular "is", "are"
end

然后,您将能够使用pluralize(@total_users, "is")返回与用户相同的规则.

You will then be able to use pluralize(@total_users, "is") to return is/are using the same rules as user/users.

编辑:您澄清了有关如何使句子复数的问题.一般而言,这要困难得多,但是如果要这样做,则必须深入NLP .

EDIT: You clarified the question on how to pluralize a sentence. This is much more difficult to do generically, but if you want to do it, you'll have to dive into NLP.

正如评论所建议的,如果您只想用几句话来做,就可以使用I18n进行操作,您可以构建如下内容:

As the comment suggests, you could do something with I18n if you just want to do it with a few sentences, you could build something like this:

  def pluralize_sentence(count, i18n_id, plural_i18n_id = nil)
    if count == 1
      I18n.t(i18n_id, :count => count)
    else
      I18n.t(plural_i18n_id || (i18n_id + "_plural"), :count => count)
    end
  end

  pluralize_sentence(@total_users, "user_count")

config/locales/en.yml中:

  en:
    user_count: "There is %{count} user."
    user_count_plural: "There are %{count} users."

这篇关于如何使“存在/存在N个对象"复数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆