单数词或复数词的最佳实践?
这是关于一般的最佳实践,而不是特定于单一语言、数据库或其他内容
我们都必须处理生成的输出,您可以在其中报告“一种产品”或“两种产品”。读起来不太好......有些人只是通过使用“一个产品”或“产品数量:(1)”来解决这个问题,而其他人可能有其他解决方案。
在不同的口语中,事情可能会更加复杂!在法语中,当你有零个产品时,你会使用单数形式,而不是复数形式! (零乘积)其他语言(中文、日语)甚至可能缺乏这些语法差异,或者有两个以上不同的单词来表示有关乘积数量的信息。 (例如,复数和更大的复数。)
但为了简单起见,让我们关注同时具有单数和复数单词的语言。
当建立一个新项目时,还需要生成报告,您如何处理单数和复数单词?您是否在数据库中添加两个单数和复数形式的名称字段?您是否在代码中添加额外的规则来将单词从单数转换为复数?你还使用其他技巧吗?
当您从事需要跟踪单数和复数形式的项目时,您如何处理这个问题?
This is about best practices in general, not specific for a single language, database or whatever
We all have to deal with generated output where you can be reporting "one products" or "two product". Doesn't read very well... Some just solve this by using "one product(s)" or "number of products: (1)" and others might have other solutions.
Things could be even more complex in different spoken languages! In French, when you have zero products, you would use the singular form, not the plural form! (Zero product) Other languages (Chinese, Japanese) might even lack these grammatical differences or have more than two different words to indicate something about the number of products. (A plural and a greater plural, for example.)
But to keep this simple, let's focus on the languages that have both singular and plural words.
When setting up a new project, which also has to generate reports, how do you deal with singular and plural words? Do you add two name fields in your database for singular and plural form? Do you add additional rules in the code to transform words from singular to plural? Do you use other tricks?
When working on a project that needs to track singular and plural forms, how do you deal with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议总体查看
gettext
,特别是ngettext
。即使您不打算翻译您的申请,也许也是如此。只需前往文档的这一部分。它或多或少地实现了所有语言,即使您选择的语言缺乏这种支持,也没有什么可以阻止您借用这些想法。I'd recommend taking a look at
gettext
in general andngettext
in particular. Maybe even if you're not going to translate your application. Just head to this part of the documentation. It has implementation for more or less all languages and even if your language of choice lacks this support, nothing stops you from borrowing the ideas.在 Perl 中,Lingua 全面解决了这个问题: :EN::变形。它使用一个大字典并仔细处理规则的所有例外情况。它还执行诸如“a”或“an”之类的操作,并处理比较!
请参阅论文了解详细信息。
In Perl, this is comprehensively solved by Lingua::EN::Inflect. It uses a large dictionary and carefully handles all the exceptions to rules. It also does things like 'a' or 'an', and handles comparisons as well!
See the paper for the gory details.
通常,我会通过某种格式化程序发送文本,该格式化程序将您想要显示的值重新格式化为人类可读的文本。这也可能会修改您的“产品”文本。 Java 为此提供了 MessageFormat 类,它支持此类修改。请参阅 [1] 中的示例。
[1] http://java.sun .com/j2se/1.5.0/docs/api/java/text/MessageFormat.html
Normally I would send my text through some kind of formatter, which reformats the values you want to display to a human readable text. This also could modify your "product" text. Java has the MessageFormat class for this, which supports such modifications. See the examples at [1].
[1] http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html
阅读并实施此;完成后(几年后)报告。就我个人而言,我对 (s) 方法感到满意;)(尽管不言而喻这并不适用于所有语言)。
Read and implement this; report back when you're done (in a few years). Personally, I'm satisfied with the (s) approach ;) (though it goes without saying this doesn't work for all languages).
只是更新,CLDR 现在对语言和 ICU 有一个实现。
Just an update, CLDR now has plural rules for languages and ICU has an implementation.
尝试使用自然语言来报告定量数据实在是太麻烦了。
Trying to use natural language for reporting quantitative data is just too much of a hassle.
对于英语应用程序,通常最简单、最有效的方法是使用一组 if 语句来存储单数并创建复数。
With english applications it's usually easiest and most efficient to store the singular and create the plural by using a group of if statements.