Content · Programming · PHP

PHP dgettext() Function: Usage and Examples

Learn how PHP dgettext works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

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

FunctionPurpose
dgettext()Looks up a translated message in a specific gettext domain.
gettext()gettext() is the full name; _() is its shorthand alias.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: The supplied domain must have its catalog bound and the active locale must be configuredThe 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.

Share this resourceSend this page to someone who may find it useful.