The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Unicode::ICU::MessageFormat - ICU’s MessageFormat class

SYNOPSIS

    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() });

DESCRIPTION

This module facilitates formatting of ICU message pattern strings.

For a description of the message pattern format, see ICU’s documentation.

COMPATIBILITY

Named arguments require ICU 4.8 or later.

CONSTANTS

CAN_TAKE_NAMED_ARGUMENTS

A boolean that indicates whether format() can accept named arguments as well as positional.

METHODS

$obj = CLASS->new( [$LOCALE] )

Returns an instance of this class. If $LOCALE is not given then we’ll use ICU’s default.

$str = OBJ->format( $PATTERN [, \@ARGUMENTS | \%ARGUMENTS ] );

Formats the given $PATTERN with the given @ARGUMENTS and returns the resulting string.

Note the following important caveats:

Dates & Times

Unlike in ICU, dates & times are expressed in seconds rather than milliseconds.

Arguments

Arguments may be positional or (assuming a recent enough ICU) named.

Positional Arguments

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.

Named Arguments

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' })

$locale = OBJ->get_locale()

Returns OBJ’s configured locale.