PHP code example of ali-translator / buffered-translation

1. Go to this page and download the library: Download ali-translator/buffered-translation 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/ */

    

ali-translator / buffered-translation example snippets


use ALI\BufferTranslation\BufferTranslation;
use ALI\Translator\PlainTranslator\PlainTranslator;
use ALI\Translator\Languages\LanguageRepositoryInterface;

/** @var PlainTranslator $plainTranslator */
/** @var LanguageRepositoryInterface $languageRepository */

$bufferTranslation = new BufferTranslation($plainTranslator, $languageRepository);

/** @var \ALI\BufferTranslation\BufferTranslation $bufferTranslation */

<script>
    alert('<?= $bufferTranslation->add($errorText, [], [
                  BufferContentOptions::MODIFIER_CALLBACK => function (string $translation): string {
                       return Html::escapeJavaScriptStringValue($translation);
                  },
         ]) 

use ALI\BufferTranslation\BufferTranslation;

/** @var BufferTranslation $bufferTranslation */
/** @var string $html */

echo $bufferTranslation->translateBuffer($html);

$html = '<div class="test">' . $bufferTranslation->add('Hello {child}. Hi {object}', [
        'child' => [
            'content' => 'Tom and {secondName}',
            'parameters' => [
                'secondName' => [
                    'content' => 'Andrea',
                    'options' => [
                        BufferContentOptions::WITH_CONTENT_TRANSLATION => true,
                    ]
                ],
            ],
        ],
        'object' => 'sun',
    ]) . '</div>';
$translatedHtml = $bufferTranslation->translateBuffer($html);

$translatedHtml = $bufferTranslation->translateBuffer($pieceOfHtml);

/**
* @param array|null $columnsForTranslation - null means "all string columns"
* @param bool $isItBufferFragment - Choose whether you want to translate the entire buffer or only the existing keys in the text
 */
$translatedBufferedArray = $bufferTranslation->translateArrayWithBuffers($bufferedArray, $columnsForTrnasl, $columnsForTranslation);

$bufferTranslation->add('Some {text}',[
    'text' => $bufferTranslation->getTextTemplateItemByBufferKey($alreadyBufferedTextKey) 
]);

$buffer = $firstBufferTranslation->add('Some {text}',[
    'text' => $secondBufferTranslation->createAndAddTextTemplateItem('текст') 
]);

$firstBufferTranslation->preTranslateAllInsideTextTemplates();
$secondBufferTranslation->preTranslateAllInsideTextTemplates();

$buffer = $firstBufferTranslation->translateBuffer($buffer);
$result = $secondBufferTranslation->translateBuffer($buffer);
bash
docker-compose run php composer install
docker-compose run php vendor/bin/phpunit