PHP code example of yiisoft / i18n-formatter-intl

1. Go to this page and download the library: Download yiisoft/i18n-formatter-intl 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/ */

    

yiisoft / i18n-formatter-intl example snippets




declare(strict_types=1);

use Yiisoft\Translator\MessageFormatterInterface;
use Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter;

return [
    MessageFormatterInterface::class => IntlMessageFormatter::class,
];

/** @var \Yiisoft\Translator\Translator $translator **/

$categoryName = 'moduleId';
$pathToModuleTranslations = './module/messages/';
$additionalCategorySource = new Yiisoft\Translator\CategorySource(
    $categoryName, 
    new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations),
    new \Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter()
);
$translator->addCategorySource($additionalCategorySource);

$translator->translate('Test string: {str}', ['str' => 'string data'], 'moduleId', 'en');
// output: Test string: string data


/** @var \Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter $formatter */
$pattern = 'Total {count, number} {count, plural, one{item} other{items}}.';
$params = ['count' => 1];
$locale = 'en';
echo $formatter->format($pattern, $params, $locale);
// output: Total 1 item. 

$pattern = '{gender, select, female{Уважаемая} other{Уважаемый}} {firstname}';
$params = ['gender' => null, 'firstname' => 'Vadim'];
echo $formatter->format($pattern, $params, 'ru');
// output: Уважаемый Vadim 

$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!';
$params = ['name' => 'Alexander', 'gender' => 'male'];
echo $formatter->format($pattern, $params, $locale);
// output: Alexander is male and he loves Yii!