When working with PHP, you may need to create a locale-aware ICU message formatter. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
MessageFormatter::create: Patterns use ICU placeholders, which are different from printf percent placeholders.
What Is MessageFormatter::create in PHP?
Create a locale-aware ICU message formatter. Patterns use ICU placeholders, which are different from printf percent placeholders.
Syntax:
MessageFormatter::create(string $locale, string $pattern): ?MessageFormatter
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: Invalid ICU patterns can cause construction to fail; verify the object before formatting.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
var_dump(MessageFormatter::create("en_US", "Hello, {name}!" ) instanceof MessageFormatter);
The code focuses on the main operation. Patterns use ICU placeholders, which are different from printf percent placeholders.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$formatter = MessageFormatter::create("fr_FR", "Bonjour, {name}!");
echo $formatter->format(["name" => "Alex"]);
What changes? Invalid ICU patterns can cause construction to fail; verify the object before formatting.
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
$formatter = MessageFormatter::create("en_US", "{name} ordered {count, plural, one{# item} other{# items}}");
if ($formatter !== null) {
echo $formatter->format(["name" => "Alex", "count" => 3]);
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Patterns use ICU placeholders, which are different from printf percent placeholders.
Important Behavior and Edge Cases
- Core behavior: Patterns use ICU placeholders, which are different from printf percent placeholders.
- Inputs and results: Invalid ICU patterns can cause construction to fail; verify the object before formatting.
- Alternatives: MessageFormatter::formatMessage() creates and uses a formatter in one step.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
MessageFormatter::create | Create a locale-aware icu message formatter. |
| Related PHP approach | MessageFormatter::formatMessage() creates and uses a formatter in one step. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Invalid ICU patterns can cause construction to fail; verify the object before formatting. |
| Confusing this API with a related operation | MessageFormatter::formatMessage() creates and uses a formatter in one step. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- MessageFormatter::create helps you create a locale-aware ICU message formatter.
- Patterns use ICU placeholders, which are different from printf percent placeholders.
- Invalid ICU patterns can cause construction to fail; verify the object before formatting.
Use MessageFormatter::create when you need to create a locale-aware ICU message formatter. 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.