When working with PHP, you may need to get a translated language name. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Locale::getDisplayLanguage: The first argument identifies the language being described; the second chooses the language of the resulting label.
What Is Locale::getDisplayLanguage in PHP?
Get a translated language name. The first argument identifies the language being described; the second chooses the language of the resulting label.
Syntax:
Locale::getDisplayLanguage(string $locale, ?string $displayLocale = null): string|false
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: A display language is a human-readable label, not an identifier suitable for saving as a locale key.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo Locale::getDisplayLanguage("es_MX", "en_US");
The code focuses on the main operation. The first argument identifies the language being described; the second chooses the language of the resulting label.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo Locale::getDisplayLanguage("de_DE", "fr_FR");
What changes? A display language is a human-readable label, not an identifier suitable for saving as a locale key.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$supported = ["en_US", "fr_FR", "ja_JP"];
foreach ($supported as $localeCode) {
echo $localeCode . ": " . Locale::getDisplayLanguage($localeCode, "en_US") . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The first argument identifies the language being described; the second chooses the language of the resulting label.
Important Behavior and Edge Cases
- Core behavior: The first argument identifies the language being described; the second chooses the language of the resulting label.
- Inputs and results: A display language is a human-readable label, not an identifier suitable for saving as a locale key.
- Alternatives: Locale::getDisplayName() renders the full locale name, possibly including region and script.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Locale::getDisplayLanguage | Get a translated language name. |
| Related PHP approach | Locale::getDisplayName() renders the full locale name, possibly including region and script. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A display language is a human-readable label, not an identifier suitable for saving as a locale key. |
| Confusing this API with a related operation | Locale::getDisplayName() renders the full locale name, possibly including region and script. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Locale::getDisplayLanguage helps you get a translated language name.
- The first argument identifies the language being described; the second chooses the language of the resulting label.
- A display language is a human-readable label, not an identifier suitable for saving as a locale key.
Use Locale::getDisplayLanguage when you need to get a translated language name. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.