如何使“存在/存在N个对象"复数?

编程入门 行业动态 更新时间:2024-10-19 18:27:22
本文介绍了如何使“存在/存在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个对象"复数?

本文发布于:2023-11-30 04:21:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复数   对象   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!