downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

textdomain> <gettext
Last updated: Fri, 27 Nov 2009

view this page in

ngettext

(PHP 4 >= 4.2.0, PHP 5)

ngettextPlural version of gettext

Description

string ngettext ( string $msgid1 , string $msgid2 , int $n )

The plural version of gettext(). Some languages have more than one form for plural messages dependent on the count.

Parameters

msgid1

msgid2

n

Return Values

Returns correct plural form of message identified by msgid1 and msgid2 for count n .

Examples

Example #1 ngettext() example

<?php

setlocale
(LC_ALL'cs_CZ');
printf(ngettext("%d window""%d windows"1), 1); // 1 okno
printf(ngettext("%d window""%d windows"2), 2); // 2 okna
printf(ngettext("%d window""%d windows"5), 5); // 5 oken

?>



textdomain> <gettext
Last updated: Fri, 27 Nov 2009
 
add a note add a note User Contributed Notes
ngettext
hek at theeks dot net
19-Aug-2009 06:07
Beware of one difference between the GNU gettext API and the PHP binding of it, which is that the GNU gettext functions that accept a $count parameter all expect (indeed, being compiled C, require) that $count be unsigned, while the PHP binding does not.

Thus, the PHP gettext functions will happily accept negative numbers. The one potentially irritating consequence of this is that -1 is treated as plural, which sits well with some people and not so well with others.  (As a picky native speaker of English, my personal opinion is that both "the temperature is minus one degree Fahrenheit" and "four apples minus five apples leaves minus one apple" but others may feel that "four apples minus five apples leaves minus one apples" sounds better.)

The upshot: You may want to abs($count) before passing numbers to gettext.

Bonus points: If your application includes user preferences, you might offer a "treat -1 as singular" option to your users, then choose $count or abs($count) to pass to gettext based on each user's preference.
peter at ints dot net
14-Dec-2008 07:26
Example for russian lang:
file.po:
    ...
    "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"
    ...
    msgid "File"
    msgid_plural "Files"
    msgstr[0] "Файл"
    msgstr[1] "Файла"
    msgstr[2] "Файлов"
    ...

file.php
    ...
    echo ngettext("File", "Files", $number);
    ...
stnt at ukr dot net
13-Jan-2008 08:51
Another ready to use function for russian plural implementation:

<?php
function plural_str ($i, $str1, $str2, $str3){
    function
plural ($a){
        if (
$a%10==1 && $a%100!=11){ return 0; }
        elseif(
$a%10>=2 && $a%10<=4 && ($a%100<10 || $a%100>=20)) { return 1; }
        else {
        return 
2;
        }
    }
   
$plural= plural ($i);
    switch (
$plural) {
        case
0:
           
$out_str = $str1;
            break;
        case
1:
           
$out_str = $str2;
            break;
        default:
$out_str = $str3;
    }
    return
$out_str;
}

//// Usage:
$n = 3;
echo
$n .' '. plural_str($n, 'тетрадь', 'тетради', 'тетрадей');
//// Output: '3 тетради'
?>
kontakt at somplatzki dot de
25-May-2007 09:47
It's useful to know how the .po-file has to look like when using ngettext:

msgid "item"
msgid_plural "items"
msgstr[0] "Produkt"
msgstr[1] "Produkte"

In php:

echo ngettext('item', 'items', $number);
tokul at users dot sourceforge dot net
10-Dec-2006 11:56
According to GNU gettext manual third argument is unsigned long integer. It must be positive number. If n is negative, it might be evaluated incorrectly in some languages.
nikolai dot zujev at gmail dot com
21-Mar-2006 09:36
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
mike-php at emerge2 dot com
03-Nov-2004 08:53
Section 10.2.5 in the GNU gettext manual explains the ngettext function:

http://www.gnu.org/software/gettext/manual/

(Sorry, but the Add Note function prevents me from including a long URL which points right to that section of the manual.)

textdomain> <gettext
Last updated: Fri, 27 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites