Retrieves a translation from a named gettext domain and message category.
What Is dcgettext() in PHP?
Retrieves a translation from a named gettext domain and message category.
Its syntax is:
dcgettext(string $domain, string $message, int $category): string
Parameters:
$domain— Accepted as string; required argument.$message— Accepted as string; required argument.$category— Accepted as int; required argument.
Return type: string. A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
Availability: Requires the gettext extension and translation catalogs.
Example 1: Basic Usage
<?php
echo dcgettext("messages", "Welcome", LC_MESSAGES);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
Example 2: A Different Input
<?php
echo dcgettext("admin", "Save", LC_MESSAGES);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
Practical Example: Applying It in a Program
<?php
$labels = ["Save", "Cancel"]; foreach ($labels as $label) { echo dcgettext("messages", $label, LC_MESSAGES) . PHP_EOL; }
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
Important Behavior and Edge Cases
- Key behavior: A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
- Environment: Requires the gettext extension and translation catalogs.
Comparison With Related Functions
| Function | Purpose |
|---|---|
dcgettext() | Retrieves a translation from a named gettext domain and message category. |
dgettext() | dgettext() selects a translation domain; dcgettext() also chooses the message category. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: A translation requires a locale, domain binding and installed message catalog | A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned. |
Key Takeaways
- Retrieves a translation from a named gettext domain and message category.
- A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.
- Requires the gettext extension and translation catalogs.
Summary: A translation requires a locale, domain binding and installed message catalog. Without a match the source text is returned.