PHP code example of tobento / app-machine-translator

1. Go to this page and download the library: Download tobento/app-machine-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/ */

    

tobento / app-machine-translator example snippets


use Tobento\App\AppFactory;
use Tobento\Service\MachineTranslator\MachineTranslatorInterface;
use Tobento\Service\MachineTranslator\MachineTranslatorsInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Booting:
$app->boot(\Tobento\App\MachineTranslator\Boot\MachineTranslator::class);
$app->booting();

// Implemented interfaces
$machineTranslator = $app->get(MachineTranslatorInterface::class);
$machineTranslators = $app->get(MachineTranslatorsInterface::class);

// Run the app
$app->run();

'features' => [
    new \Tobento\App\MachineTranslator\Feature\Translate(
        withAcl: false, // WARNING: Disables permission checks. Only use in development.
        localizeRoute: true, // Localize the route per locale
    ),
],

'aliases' => [
    // Log translation failures using the "daily" logger:
    \Tobento\App\MachineTranslator\Feature\Translate::class => 'daily',
    
    // Or disable logging entirely:
    \Tobento\App\MachineTranslator\Feature\Translate::class => 'null',
],

$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);


use Tobento\Service\Tag\Attributes;

$mta = new Attributes([
    'class' => 'button',

    'data-machine-translator' => [
        // The translation endpoint (Translate Feature route):
        'url' => (string)$view->routeUrl('machine-translate'),

        // Use the first matching field inside the nearest [data-field] container:
        'from_first' => '[data-field]',

        // Explicit source field(s) (optional):
        //'from' => 'text.en', // or 'text[en]' - same result
        //'from' => ['text.en', 'text.fr'],

        // Override target locale (optional):
        //'locale' => 'de', // otherwise inferred from last segment of "to"

        // Target field is set individually on each button below:
        //'to' => 'text.de',

        // Select a specific translator (optional):
        //'translator' => 'deepl', // or 'google', 'openai', etc.
        
        // Optional messages:
        'empty_message' => 'No text found to translate from.',
        'target_not_empty_message' => 'Target already contains text.',
    ],
]);

$form = $view->form();

$app->boot(\Tobento\App\Event\Boot\Event::class);
json
POST /machine-translate
{
    "text": "Hello world",
    "locale": "de"
}
app/config/logging.php