Looks up a translated message in a specific gettext domain.
What Is dgettext() in PHP?
Looks up a translated message in a specific gettext domain.
Its syntax is:
dgettext(string $domain, string $message): string
Parameters:
$domain— Accepted as string; required argument.$message— Accepted as string; required argument.
Return type: string. The supplied domain must have its catalog bound and the active locale must be configured.
Availability: Requires gettext and configured translation files.
Example 1: Basic Usage
<?php
echo dgettext("messages", "Home");
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The supplied domain must have its catalog bound and the active locale must be configured.
Example 2: A Different Input
<?php
echo dgettext("admin", "Settings");
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The supplied domain must have its catalog bound and the active locale must be configured.
Practical Example: Applying It in a Program
<?php
$menuItems = ["Home", "Profile", "Logout"]; foreach ($menuItems as $menuItem) { echo dgettext("messages", $menuItem) . PHP_EOL; }
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The supplied domain must have its catalog bound and the active locale must be configured.
Important Behavior and Edge Cases
- Key behavior: The supplied domain must have its catalog bound and the active locale must be configured.
- Environment: Requires gettext and configured translation files.
Comparison With Related Functions
| Function | Purpose |
|---|---|
dgettext() | Looks up a translated message in a specific gettext domain. |
gettext() | gettext() is the full name; _() is its shorthand alias. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The supplied domain must have its catalog bound and the active locale must be configured | The supplied domain must have its catalog bound and the active locale must be configured. |
Key Takeaways
- Looks up a translated message in a specific gettext domain.
- The supplied domain must have its catalog bound and the active locale must be configured.
- Requires gettext and configured translation files.
Summary: The supplied domain must have its catalog bound and the active locale must be configured.