PHP code example of survos / translator-bundle

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

    

survos / translator-bundle example snippets


return [
    // ...
    Survos\TranslatorBundle\SurvosTranslatorBundle::class => ['all' => true],
];


namespace App\Controller;

use Survos\TranslatorBundle\Service\Translator; // the facade
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class DemoController extends AbstractController
{
    public function translate(Translator $translator): Response
    {
        $result = $translator->translate(
            text: 'Hello world',
            target: 'es',        // ISO 639-1 or BCP-47; engine normalizes
            source: 'en',        // optional; auto‑detect if omitted
            domain: 'ui',        // optional context tag for caching
            options: [           // engine‑specific extras
                'formality' => 'prefer_less', // DeepL example
            ]
        );

        // $result is a DTO with: text, source, target, engine, detectedSource, meta, cached
        return new Response($result->text); // "Hola mundo"
    }
}

$translated = $translator->translate('Save', 'es');

use Survos\TranslatorBundle\Message\TranslateText;
use Symfony\Component\Messenger\MessageBusInterface;

$bus->dispatch(new TranslateText('Hello world', 'es', source: 'en', domain: 'ui'));


namespace App\Command;

use Survos\TranslatorBundle\Service\Translator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;

#[AsCommand('translator:demo')]
final class TranslatorDemoCommand
{
    public function __construct(private Translator $translator) {}

    public function __invoke(
        SymfonyStyle $io,
        #[Argument('Phrase to translate')] string $text,
        #[Option('to')] ?string $to = null,
        #[Option('from')] ?string $from = null,
        #[Option('engine')] ?string $engine = null,
    ): int {
        $res = $this->translator->translate($text, $to ?? 'es', $from, options: ['engine' => $engine]);
        $io->success(sprintf('[%s] %s → %s: %s', $res->engine, $res->source ?? 'auto', $res->target, $res->text));
        return Command::SUCCESS;
    }
}

try {
    $res = $translator->translate('Hello', 'fr', options: ['engine' => 'deepl']);
} catch (\Throwable $e) {
    // Fallback to Libre
    $res = $translator->translate('Hello', 'fr', options: ['engine' => 'libre']);
}

#[Route('/api/translate', name: 'api_translate', methods: ['POST'])]
public function api(Request $req, Translator $translator): JsonResponse
{
    $text = (string) $req->request->get('text', '');
    $to   = (string) $req->request->get('to', 'es');
    $from = $req->request->get('from');
    $engine = $req->request->get('engine');

    $res = $translator->translate($text, $to, $from, options: ['engine' => $engine]);

    return $this->json([
        'text' => $res->text,
        'source' => $res->source ?? $res->detectedSource,
        'target' => $res->target,
        'engine' => $res->engine,
        'cached' => (bool)($res->meta['cached'] ?? false),
    ]);
}

final class AcmeEngine implements EngineInterface
{
    public function name(): string { return 'acme'; }
    public function translate(TranslationInput $in): TranslationResult { /* ... */ }
}
bash
php bin/console messenger:consume translator -vv
bash
php bin/console translator:demo "Hello" --to=es --from=en --engine=deepl