ngettext

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

ngettext β€” ВСрсия Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ gettext для сообщСний Π²ΠΎ мноТСствСнном числС

ОписаниС

function ngettext(string $singular, string $plural, int $count): string

Ѐункция β€” вСрсия Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ gettext() для сообщСний Π²ΠΎ мноТСствСнном числС. ΠžΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Π΅ языки содСрТат большС ΠΎΠ΄Π½ΠΎΠΉ Ρ„ΠΎΡ€ΠΌΡ‹ сообщСний Π²ΠΎ мноТСствСнном числС, Π² зависимости ΠΎΡ‚ количСства.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

singular

Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ сообщСния Π² СдинствСнном числС.

plural

Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ сообщСния Π²ΠΎ мноТСствСнном числС.

count

Число (Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, количСство элСмСнтов), Π½Π° основС ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ функция ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ Π³Ρ€Π°ΠΌΠΌΠ°Ρ‚ΠΈΡ‡Π΅ΡΠΊΡƒΡŽ Ρ„ΠΎΡ€ΠΌΡƒ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄Π°.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΡƒΡŽ Ρ„ΠΎΡ€ΠΌΡƒ сообщСния Π²ΠΎ мноТСствСнном числС, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΡ†ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΏΠΎ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π°ΠΌ singular ΠΈ plural для количСства count.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ngettext()

<?php

setlocale
(LC_ALL, 'ru_RU');

printf(ngettext("%d ΠΎΠΊΠ½ΠΎ\n", "%d ΠΎΠΊΠΎΠ½\n", 1), 1); // 1 ΠΎΠΊΠ½ΠΎ
printf(ngettext("%d ΠΎΠΊΠ½ΠΎ\n", "%d ΠΎΠΊΠ½Π°\n", 2), 2); // 2 ΠΎΠΊΠ½Π°
printf(ngettext("%d ΠΎΠΊΠ½ΠΎ\n", "%d ΠΎΠΊΠΎΠ½\n", 5), 5); // 5 ΠΎΠΊΠΎΠ½

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
9
peter at ints dot net ΒΆ
17 years ago
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);
    ...
up
6
Mike Robinson ΒΆ
12 years ago
Even though "hek at theeks dot net"'s answer is valid, I would not recommend using the abs() hack recommended. Even though it is by far the most common, not all languages treat (n != 1) as plural. Other languages are much more complex, for example, here is how you determine plurals in Macedonian.

n==1 || n%10==1 ? 0 : 1

In Arabic there are actually 5 different types of plurals:

n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5

If you are using only specific languages that use the (n != 1) format AND -1 is singular, by all means, use abs(), but be careful and don't forget that you have done this when adding a new language to your project 3 years down the road.
up
6
kontakt at somplatzki dot de ΒΆ
19 years ago
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);
up
2
mike-php at emerge2 dot com ΒΆ
21 years ago
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.)
up
2
hek at theeks dot net ΒΆ
16 years ago
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.
up
1
tokul at users dot sourceforge dot net ΒΆ
19 years ago
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.