Content · Programming · PHP

PHP dcgettext() Function: Usage and Examples

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

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

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

FunctionPurpose
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

MistakeCorrect Approach
Ignoring this behavior: A translation requires a locale, domain binding and installed message catalogA 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.

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