2

I would like to use named arguments in a string passed to MessageFormater, like that

$fmt = new MessageFormatter("en_US", "My name id {my_name}");
$fmt->format(array('my_name' => 'John'));

When I running this code, I obtain the error message :

$fmt->getErrorMessage() returns

Number formatting failed: U_ILLEGAL_ARGUMENT_ERROR

Whereas

$fmt = new MessageFormatter("en_US", "My name id {0}");
$fmt->format(array(0 => 'John'));

works well.

icu-project website report that named arguments are supported since ICU 3.8 (Seen here), and I use the 4.2.1

Where is the unicorn? PHP doesn't support named arguments for MessageFormatter? Maybe there is an alternative?

j0k
  • 22,600
  • 28
  • 79
  • 90
Tristan
  • 41
  • 4

2 Answers2

1

This is fixed in a future (as of today) version of intl. See https://bugs.php.net/bug.php?id=61871 .

0

Yes, seems so. What do you want to hear?

Update: After the OP edited his question

echo vsprintf('My name id %s', array('John'));

See sprintf()

Of course it does not any local-based things.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • I know this way. My texts are sent too translators. I want to avoid %d, %s. It's simplest to translate "My name is {my_name}" than "My name is %s". It's a bad exemple because it's easy to understand the meaning of this sentence, but sometimes it's not the case. – Tristan Feb 23 '12 at 11:45