Retrieves singular or plural translations from a named domain and category.
What Is dcngettext() in PHP?
Retrieves singular or plural translations from a named domain and category.
Its syntax is:
dcngettext(string $domain, string $singular, string $plural, int $count, int $category): string
Parameters:
$domain— Accepted as string; required argument.$singular— Accepted as string; required argument.$plural— Accepted as string; required argument.$count— Accepted as int; required argument.$category— Accepted as int; required argument.
Return type: string. The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
Availability: Requires the gettext extension and matching plural catalogs.
Example 1: Basic Usage
<?php
echo dcngettext("messages", "%d item", "%d items", 1, LC_MESSAGES);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
Example 2: A Different Input
<?php
echo dcngettext("messages", "%d item", "%d items", 3, LC_MESSAGES);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
Practical Example: Applying It in a Program
<?php
foreach ([1, 2, 5] as $itemCount) { $label = dcngettext("messages", "%d file", "%d files", $itemCount, LC_MESSAGES); echo sprintf($label, $itemCount) . PHP_EOL; }
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
Important Behavior and Edge Cases
- Key behavior: The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
- Environment: Requires the gettext extension and matching plural catalogs.
Comparison With Related Functions
| Function | Purpose |
|---|---|
dcngettext() | Retrieves singular or plural translations from a named domain and category. |
dngettext() | dngettext() chooses plural forms for a domain; dcngettext() additionally accepts a message category. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The selected plural form is locale-dependent | The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf(). |
Key Takeaways
- Retrieves singular or plural translations from a named domain and category.
- The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().
- Requires the gettext extension and matching plural catalogs.
Summary: The selected plural form is locale-dependent. Message placeholders must match the variables passed to sprintf().