PHP code example of marketforce-info / azure-translator
1. Go to this page and download the library: Download marketforce-info/azure-translator 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/ */
marketforce-info / azure-translator example snippets
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory as GuzzleFactory;
use MarketforceInfo\AzureTranslator\Translator;
$factory = new GuzzleFactory();
$client = new Translator\Client(
new GuzzleClient(),
new Translator\RequestFactory($factory, $factory, [Translator\Language::french])
);
$translator = new Translator($client);
$translator->onTranslate(static function (Translator\Translation $translation) {
// do something with the message
})
->begin(static function (Translator\Delegate $translator) use ($messages) {
foreach ($messages as $message) {
$translator->translate($message);
}
});
use \MarketforceInfo\AzureTranslator\Builder;
use \MarketforceInfo\AzureTranslator\MessageFormatter\BasicFormatter;
use \MarketforceInfo\AzureTranslator\Translator;
$factory = new GuzzleFactory();
$translator = (new Builder())
->withBaseUrl(Translator\RequestFactory::BASE_URL_US)
->withHttp(new GuzzleClient(), $factory, $factory)
->withLanguages([Translator\Language::arabic], Translator\Language::french)
->withBearerToken('<bearer-token>')
->withMessageFormatter(new BasicFormatter('[', ']'))
->withTraceIdCallback(fn () => 'xxxx-XXXX-xxxx-XXXX-xxxx')
->when(
$_ENV['delete_profanity'] === true,
fn (Builder $builder) => $builder->withProfanityDeleted(),
fn (Builder $builder) => $builder->withProfanityMarked(static fn (string $word) => '*censored*')
)
->create();
$translator->begin(static function (Delegate $translator) use ($untranslatedMessages) {
foreach ($untranslatedMessages as $message) {
$translator->translate($message);
}
});