PHP code example of siilike / messageformat

1. Go to this page and download the library: Download siilike/messageformat library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

siilike / messageformat example snippets




use Magneds\MessageFormat\MessageFormatter;

$formatter = new MessageFormatter('en', 'Found {count, plural, =0 {no result} =1 {one result} other {# results}}');

print $formatter->format(['count' => 0]);  //  Found no result
print $formatter->format(['count' => 1]);  //  Found one result
print $formatter->format(['count' => 2]);  //  Found 2 results



use Magneds\MessageFormat\MessageFormatter;

$formatter = new MessageFormatter('en', 'Found {0, plural, =0 {no result} =1 {one result} other {# results}}');

print $formatter->format([0]);  //  Found no result
print $formatter->format([1]);  //  Found one result
print $formatter->format([2]);  //  Found 2 results


MessageFormatter::__construct(string $locale , string $pattern)



use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Hello {audience}');
$es = new MessageFormatter('es', 'Hola {audience}');
$de = new MessageFormatter('de', 'Hallo {audience}');

static MessageFormatter::create(string $locale, string $pattern)



use Magneds\MessageFormat\MessageFormatter;

$en = MessageFormatter::create('en', 'Hello {audience}');
$es = MessageFormatter::create('es', 'Hola {audience}');
$de = MessageFormatter::create('de', 'Hallo {audience}');

static MessageFormatter::formatMessage(string $locale, string $pattern, array $args)



use Magneds\MessageFormat\MessageFormatter;

print MessageFormatter::formatMessage('en', 'Hello {audience}', ['audience' => 'universe']); //  Hello universe

string MessageFormatter::format (array $args)



use Magneds\MessageFormat\MessageFormatter;

$es = new MessageFormatter('es-ES', 'Por el pequeño precio de {price, number, currency} puedes comprar apps.');

print $es->format(['price' => 0.99]); //  Por el pequeño precio de 0,99 € puedes comprar apps.

string MessageFormatter::getLocale(void)



use Magneds\MessageFormat\MessageFormatter;

$enNZ = new MessageFormatter('en-NZ', 'Hello {audience}');
$nlBE = new MessageFormatter('nl-BE', 'Hallo {audience}');

print $enNZ->getLocale();  //  'en_NZ'
print $nlBE->getLocale();  //  'nl_BE'

string MessageFormatter::getPattern([bool $compatible=false])



use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Welcome back {name}, you have {count, plural, =0{no unread messages} one{one unread message} other{# unread messages}}');

print $en->getPattern();      //  Welcome back {name}, you have {count, plural, =0{no unread messages} one{one unread message} other{# unread messages}}
print $en->getPattern(true);  //  Welcome back {0}, you have {1, plural, =0{no unread messages} one{one unread message} other{# unread messages}}

static array MessageFormatter::parseMessage(string $locale, string $pattern, string $source)



use Magneds\MessageFormat\MessageFormatter;

print MessageFormatter::parseMessage(
    'en_US', 
    '{monkeys,number,integer} monkeys on {trees,number,integer} trees make {distribution,number} monkeys per tree',
    '4,560 monkeys on 123 trees make 37.073 monkeys per tree'
);  //  ['monkeys' => 4560, 'trees' => 123, 'distribution' => 37.073],

array MessageFormatter::parse(string $value)



use Magneds\MessageFormat\MessageFormatter;

$nl = new MessageFormatter('nl', 'De {animal} {action} de {result} van de {target}');
$message = 'De kat krabt de krullen van de trap';

print $nl->parse($message);  //  ['animal' => 'kat', 'action' => 'krabt', 'result' => 'krullen', 'target' => 'trap']

bool MessageFormatter::setPattern(string $pattern)



use Magneds\MessageFormat\MessageFormatter;

$en = new MessageFormatter('en', 'Initial {value}');

print $en->getPattern();      //  Initial {value}
print $en->getPattern(true);  //  Initial {0}

$en->setPattern('Override {value, number, percentage}');

print $en->getPattern();      //  Override {value, number, percentage}
print $en->getPattern(true);  //  Override {0, number, percentage}