Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

ngettext

(PHP 4 >= 4.2.0, PHP 5)

ngettext -- Версия gettext для множественного числа

Описание

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

Возвращает сообщение в соответствующей числу форме. В английском языке есть только два варианта - msgid1 и msgid2: первый только когда n=1. В русском языке множественых форм три (1, 21, 31, ...; 2, 3, 4, ...; 5, 6, 8, ...), следовательно в каталоге сообщений будет три строчки.

Пример 1. Пример

<?php

setlocale
(LC_ALL, 'ru_RU');
printf(ngettext("%d window", "%d windows", 21), 21); // 21 окно
printf(ngettext("%d window", "%d windows", 22), 22); // 22 окна
printf(ngettext("%d window", "%d windows", 25), 25); // 25 окон

?>



textdomain> <gettext
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
ngettext
kontakt at somplatzki dot de
25-May-2007 02: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 03: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 01: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 12: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: Sat, 27 Jan 2007
 
 
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571