PHP code example of yiisoft / translator-message-php

1. Go to this page and download the library: Download yiisoft/translator-message-php 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 / translator-message-php example snippets


use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\IntlMessageFormatter;
use Yiisoft\Translator\Message\Php\MessageSource;

$categorySource = new CategorySource(
    'my-category',
    new MessageSource('/path/to/messages'),
    new IntlMessageFormatter(),
);

/** @var string $path - full path to your translations */
$messageSource = new \Yiisoft\Translator\Message\Php\MessageSource($path);

/** 
 * @var \Yiisoft\Translator\Message\Php\MessageSource $messageSource
 * @var ?string $translatedString
 */
$id = 'messageIdentifier';
$category = 'messageCategory';
$language = 'de-DE';

$translatedString = $messageSource->getMessage($id, $category, $language);

/** 
 * @var \Yiisoft\Translator\Message\Php\MessageSource $messageSource
 */
$category = 'messageCategory';
$language = 'de-DE';
$data = [
    'test.id1' => [
        'message' => 'Nachricht 1', // translated string
        'comment' => 'Comment for message 1', // is optional parameter for save extra metadata
    ],
    'test.id2' => [
        'message' => 'Nachricht 2',
    ],
    'test.id3' => [
        'message' => 'Nachricht 3',
    ],
];

$messageSource->write($category, $language, $data);

📁 path_to_your_storage
  📁 de-DE
     🗎 messageCategory.php