PHP 中的复数形式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1534127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pluralize in PHP
提问by menardmam
I have a lot of information to display from a database. Some are french, other are english. Some are a unique, some a list...
我有很多信息要从数据库中显示出来。有的是法语,有的是英语。有些是独一无二的,有些是清单……
The question : how do you manage all that different option in PHP
问题:您如何管理 PHP 中的所有不同选项
IF ELSE SWITCH ARRAY (with all the text)
IF ELSE SWITCH ARRAY(带有所有文本)
other method ...
其他方法...
now the problem rise on a list of odors, some have one (odor) other have many (odors) putting the s or not is a pain..... help !
现在问题出现在一系列气味上,有些有一种(气味),另一些有很多(气味),是否放 s 是一种痛苦..... 帮助!
thanks
谢谢
采纳答案by Jesse Kochis
The best way IMO is to have an array of all your pluralization rules for each language, i.e. array('man'=>'men', 'woman'=>'women');and write a pluralize() function for each singular word.
IMO 的最佳方法是为每种语言设置一个包含所有复数规则的数组,即为array('man'=>'men', 'woman'=>'women');每个单数词编写一个复数()函数。
You may want to take a look at the CakePHP inflector for some inspiration.
您可能想看看 CakePHP 的 inflector 以获得一些灵感。
https://github.com/cakephp/cakephp/blob/master/src/Utility/Inflector.php
https://github.com/cakephp/cakephp/blob/master/src/Utility/Inflector.php
回答by mpen
You can try this function I wrote:
你可以试试我写的这个函数:
/**
* Pluralizes a word if quantity is not one.
*
* @param int $quantity Number of items
* @param string $singular Singular form of word
* @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
* @return string Pluralized word if quantity is not one, otherwise singular
*/
public static function pluralize($quantity, $singular, $plural=null) {
if($quantity==1 || !strlen($singular)) return $singular;
if($plural!==null) return $plural;
$last_letter = strtolower($singular[strlen($singular)-1]);
switch($last_letter) {
case 'y':
return substr($singular,0,-1).'ies';
case 's':
return $singular.'es';
default:
return $singular.'s';
}
}
Usage:
用法:
pluralize(4, 'cat'); // cats
pluralize(3, 'kitty'); // kitties
pluralize(2, 'octopus', 'octopii'); // octopii
pluralize(1, 'mouse', 'mice'); // mouse
There's obviously a lot of exceptional words that this function will not pluralize correctly, but that's what the $pluralargument is for :-)
显然,这个函数不能正确复数化有很多特殊的词,但这就是$plural参数的用途:-)
Take a look at Wikipediato see just how complicated pluralizing is!
看看维基百科,看看复数有多复杂!
回答by mpen
You might want to look at the gettext extension. More specifically, it sounds like ngettext()will do what you want: it pluralises words correctly as long as you have a number to count from.
您可能想查看gettext 扩展名。更具体地说,它听起来像ngettext()你想要的那样:只要你有一个数字可以计算,它就会正确地将单词复数化。
print ngettext('odor', 'odors', 1); // prints "odor"
print ngettext('odor', 'odors', 4); // prints "odors"
print ngettext('%d cat', '%d cats', 4); // prints "4 cats"
You can also make it handle translated plural forms correctly, which is its main purpose, though it's quite a lot of extra work to do.
你也可以让它正确处理翻译的复数形式,这是它的主要目的,尽管它有很多额外的工作要做。
回答by Gerry
Enjoy: https://github.com/ICanBoogie/Inflector
享受:https: //github.com/ICanBoogie/Inflector
Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.
多语言变形器,将单词从单数转换为复数,将下划线转换为驼峰式大小写等等。
回答by Reality
If you're going to go down the route of writing your own pluralize function then you might find this algorithmic description of pluralisation helpful:
如果您打算继续编写自己的复数函数,那么您可能会发现以下对复数的算法描述很有帮助:
http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
Or the much easier approach would probably be to use one of the ready-made pluralize functions available on the Internet:
或者更简单的方法可能是使用 Internet 上现成的复数函数之一:
回答by K.Alex
Custom, transparent and extension-free solution. Not sure about its speed.
自定义、透明和无扩展的解决方案。不确定它的速度。
/**
* Custom plural
*/
function splur($n,$t1,$t2,$t3) {
settype($n,'string');
$e1=substr($n,-2);
if($e1>10 && $e1<20) { return $n.' '.$t3; } // "Teen" forms
$e2=substr($n,-1);
switch($e2) {
case '1': return $n.' '.$t1; break;
case '2':
case '3':
case '4': return $n.' '.$t2; break;
default: return $n.' '.$t3; break;
}
}
Usage in Ukrainian / Russian:
乌克兰语/俄语用法:
splur(5,'стор?нка','стор?нки','стор?нок') // 5 стор?нок
splur(4,'стор?нка','стор?нки','стор?нок') // 4 стор?нки
splur(1,'стор?нка','стор?нки','стор?нок') // 1 стор?нка
splur(12,'стор?нка','стор?нки','стор?нок') // 12 стор?нок
splur(5,'страница','страницы','страниц') // 5 страниц
splur(4,'страница','страницы','страниц') // 4 страницы
splur(1,'страница','страницы','страниц') // 1 страница
splur(12,'страница','страницы','страниц') // 12 страниц
回答by ya.teck
Symfony now offers a the Inflector component converts English words between their singular and plural forms. https://symfony.com/doc/master/components/inflector.html
Symfony 现在提供了一个 Inflector 组件,可以在单数和复数形式之间转换英语单词。 https://symfony.com/doc/master/components/inflector.html

