ali-translator / translator-js-integrate
Integrate ALI translator to frontend js
v0.3.1
2022-10-02 08:25 UTC
Requires
- php: >=7.1
- ext-json: *
- ali-translator/translator: ^0
Requires (Dev)
- php: >=7.3.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-10-30 01:48:12 UTC
README
Additional packet for ali-translator/translator which help integrate this tanslator to frontend js.
Installation
$ composer require ali-translator/translator-js-integrate
Init
Since this package extended from ali-translator/translator,
at first you need create $translator
and wrapper, with vector of his translation - $plaiTranslator
Than include src/assets/js/ali-translator.js
script in your html code.
After that, create instance of ALIAbsTranslatorJs
:
use ALI\TranslatorJsIntegrate\ALIAbcTranslatorJs; use ALI\Translator\PlainTranslator\PlainTranslator; /** @var PlainTranslator $plainTranslator */ $ALIAbcTranslatorJs = new ALIAbcTranslatorJs($plainTranslator);
Basic Usage
php:
use \ALI\TranslatorJsIntegrate\ALIAbcTranslatorJs; /** @var ALIAbcTranslatorJs $aLIAbsTranslatorJs */ // $aliAbc->saveTranslate('Hello {object}!', 'Привіт {object}!'); $aLIAbsTranslatorJs->addOriginals(['Hello {object}!']); $startUpJsCode = $aLIAbsTranslatorJs->generateStartupJs('__t');
html:
<html> <head> <script src="/js/ali-translator.js"></script> <script><?= $startUpJsCode ?></script> </head> <body>...</body> </html>
js:
alert(__t('Hello {object}!',{ 'object' : 'sun' }))
Tests
In packet exist docker-compose file, with environment for testing.
docker-compose run php composer install docker-compose run php vendor/bin/phpunit