When working with PHP, you may need to get a human-readable locale name. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
Locale::getDisplayName: ICU can translate both the language and any included territory or script into a display label.
What Is Locale::getDisplayName in PHP?
Get a human-readable locale name. ICU can translate both the language and any included territory or script into a display label.
Syntax:
Locale::getDisplayName(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: Display labels depend on ICU data; do not compare or persist them as stable identifiers.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo Locale::getDisplayName("fr_CA", "en_US");
The code focuses on the main operation. ICU can translate both the language and any included territory or script into a display label.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo Locale::getDisplayName("ja_JP", "fr_FR");
What changes? Display labels depend on ICU data; do not compare or persist them as stable identifiers.
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
$choices = ["en_US", "fr_CA", "de_DE"];
foreach ($choices as $choice) {
echo Locale::getDisplayName($choice, "en_US") . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. ICU can translate both the language and any included territory or script into a display label.
Important Behavior and Edge Cases
- Core behavior: ICU can translate both the language and any included territory or script into a display label.
- Inputs and results: Display labels depend on ICU data; do not compare or persist them as stable identifiers.
- Alternatives: Locale::getDisplayLanguage() returns only the language part.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
Locale::getDisplayName | Get a human-readable locale name. |
| Related PHP approach | Locale::getDisplayLanguage() returns only the language part. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Display labels depend on ICU data; do not compare or persist them as stable identifiers. |
| Confusing this API with a related operation | Locale::getDisplayLanguage() returns only the language part. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- Locale::getDisplayName helps you get a human-readable locale name.
- ICU can translate both the language and any included territory or script into a display label.
- Display labels depend on ICU data; do not compare or persist them as stable identifiers.
Use Locale::getDisplayName when you need to get a human-readable locale 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.