Unicode::ICU::MessageFormat - ICU’s MessageFormat class
use utf8; my $formatter = Unicode::ICU::MessageFormat->new('en'); my $chars = $formatter->format('The time is {0, time} on {0, date}.', [time]); # Named arguments are also acceptable: my $chars = $formatter->format('The time is {now, time} on {now, date}.', { now => time() });
This module facilitates formatting of ICU message pattern strings.
For a description of the message pattern format, see ICU’s documentation.
Named arguments require ICU 4.8 or later.
A boolean that indicates whether format() can accept named arguments as well as positional.
format()
Returns an instance of this class. If $LOCALE is not given then we’ll use ICU’s default.
Formats the given $PATTERN with the given @ARGUMENTS and returns the resulting string.
Note the following important caveats:
Unlike in ICU, dates & times are expressed in seconds rather than milliseconds.
Arguments may be positional or (assuming a recent enough ICU) named.
If you give arguments as an array reference, then $PATTERN’s arguments MUST be a continuous sequence starting at 0. A “missing” argument will trigger an exception.
Note that ICU positional arguments are zero-indexed. This differs from Locale::Maketext and other systems that use 1-indexing. So if you do this:
# bad: $formatter->format('My name is {1}.', ['Jonas'])
… you’ll get an exception. ({0} is what you want, not {1}.) If you really want, though, you could use named arguments; see below.
{0}
{1}
NOTE: This library cannot handle named arguments for all ICU versions.
The above is probably easier to read and maintain as:
$formatter->format('My name is {name}.', { name => 'Jonas' })
You can also give named arguments for a positional-argument $PATTERN; in fact, if you do that, you can have “missing” arguments, e.g.:
# ok: $formatter->format('My name is {1}.', { 1 => 'Jonas' })
Returns OBJ’s configured locale.
To install Unicode::ICU, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Unicode::ICU
CPAN shell
perl -MCPAN -e shell install Unicode::ICU
For more information on module installation, please visit the detailed CPAN module installation guide.