|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-04-27 19:03 UTC] web-php-bugs at sklar dot com
Description:
------------
Attached is a patch to add named argument support to MessageFormatter message formatting.
With this patch, you can pass an associative array of named arguments and they are interpolated into your messages. The type of the values in the associative array determine how they are parsed for formatting. Floats, longs, and strings are converted as you would expect (floats, longs, strings). Bool is turned into an int64 (0 or 1). Null is turned into the empty string. Objects and arrays are errors, except a DateTime object is turned into a date for formatting.
Test script:
---------------
$pattern = <<<_MSG_
{gender_of_host, select,
female {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to her party.}
=2 {{host} invites {guest} and one other person to her party.}
other {{host} invites {guest} and # other people to her party.}}}
male {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to his party.}
=2 {{host} invites {guest} and one other person to his party.}
other {{host} invites {guest} and # other people to his party.}}}
other {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} and # other people to their party.}}}}
_MSG_;
$m = msgfmt_format_message('en_US', $pattern, array('gender_of_host' => 'male',
'num_guests' => 4,
'host' => 'ralph', 'guest' => 'beep'));
print "$m\n";
$m = msgfmt_format_message('en_US', $pattern, array('gender_of_host' => 'female', 'num_guests' => 4));
print "$m\n";
Patchesintl-named-arguments-2.diff (last revision 2012-05-03 18:06 UTC by web-php-bugs at sklar dot com)intl-named-arguments (last revision 2012-04-27 19:03 UTC by web-php-bugs at sklar dot com) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
Unfortunately this function still does not work completely as it should, as the following example shows: $pattern = <<<_MSG_ {num_guests} -- {num_guests, plural, offset:1 =0 {{host} does not give a party.} =1 {{host} invites {guest} to her party.} =2 {{host} invites {guest} and one other person to her party.} other {{host} invites {guest} and # other people to her party.}} _MSG_; $a = array('gender_of_host' => 'male', 'num_guests' => 4, 'host' => 'ralph', 'guest' => 'beep'); $m = msgfmt_format_message('en_US', $pattern, $a); print "$m\n"; $a['gender_of_host'] = 'female'; $a['num_guests'] = 49; $m = msgfmt_format_message('en_US', $pattern, $a); print "$m\n";