note 63387 added to function.ngettext
| From: | nikolai dot zujev at gmail dot com | Date: | Tue, 21 Mar 2006 21:36:01 +0000 |
| Subject: | note 63387 added to function.ngettext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106181@lists.php.net to get a copy of this message | ||
This is an implementation for a word ending in russian lang. Also as I know polish and similar langs
use same rules:
<?php
/**
* Returns:
* 0, if $n == 1, 21, 31, 41, ...
* 1, if $n == 2..4, 22..24, 32..34, ...
* 2, if $n == 5..20, 25..30, 35..40, ...
*/
function plural( $n )
{
if ( $n % 10 == 1 && $n % 100 != 11 )
{
return 0;
}
if ( $n % 10 >= 2 && $n % 10 <= 4 && ( $n % 100 < 10 || $n % 100 >=
20 ) )
{
return 1;
}
return 2;
}
// usage
for ( $i = 0; $i < 100; $i++ )
{
$x = plural( $i );
printf(
"%d ÑеÑÑад%s<br/>\n", $i,
( 0 == $x ? 'Ñ' : ( 1 == $x ? 'и' : 'ей' ) )
);
}
?>
Output:
0 ÑеÑÑадей
1 ÑеÑÑадÑ
2 ÑеÑÑади
3 ÑеÑÑади
4 ÑеÑÑади
5 ÑеÑÑадей
6 ÑеÑÑадей
7 ÑеÑÑадей
8 ÑеÑÑадей
9 ÑеÑÑадей
10 ÑеÑÑадей
...
Also here is short version:
<?php
$n = 17;
print ($n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 &&
($n%100<10 || $n%100>=20) ? 1 : 2));
?>
output: 2
----
Server IP: 81.94.227.69
Probable Submitter: 217.199.126.2 (proxied: unknown)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.ngettext.php
Edit -- http://master.php.net/note/edit/63387
Del: integrated -- http://master.php.net/note/delete/63387/integrated
Del: useless -- http://master.php.net/note/delete/63387/useless
Del: bad code -- http://master.php.net/note/delete/63387/bad+code
Del: spam -- http://master.php.net/note/delete/63387/spam
Del: non-english -- http://master.php.net/note/delete/63387/non-english
Del: in docs -- http://master.php.net/note/delete/63387/in+docs
Del: other reasons-- http://master.php.net/note/delete/63387
Reject -- http://master.php.net/note/reject/63387
Search -- http://master.php.net/manage/user-notes.php