PHP code example of tobiaswolf / machine-translation

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

    

tobiaswolf / machine-translation example snippets


// config/config.php
return [
  'tobiaswolf.machine-translation.deepl.authKey' => '279a2e9d-83b3-c416-7e2d-f721593e42a0:fx',
];

$pageId = 'test';

kirby()->api()->call('machine-translate/pages/' . $pageId, 'POST', [
	'query' => [
		'language' => 'en',
	],
	'body' => [
		'sourceLang' => 'de',
		'forceOverwrite' => true,
	],
]);

$targetLang = 'de'
$text = $page->text(); // e.g. Hello World
$translatedText = $text->translate($targetLang); // returns the field with the translated text (Hallo Welt)
$page->update([
  'text' => $translatedText,
], $targetLang);

use Tobiaswolf\MachineTranslation\Translate;

$sourceTexts = ['Hello World', 'Greetings Earthlings'];
$translatedTexts = Translate::translate($sourceTexts, 'es');

var_dump($translatedTexts);
// array(2) { [0]=> array(2) { ["detected_source_language"]=> string(2) "EN" ["text"]=> string(10) "Hola Mundo" } [1]=> array(2) { ["detected_source_language"]=> string(2) "EN" ["text"]=> string(19) "Saludos terrícolas" } }

// config/config.php
return [
	'cache.tobiaswolf.machine-translation.translate' => false, // default true
]