PHP code example of themismin / google-translate-php
1. Go to this page and download the library: Download themismin/google-translate-php 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/ */
themismin / google-translate-php example snippets
use Stichoza\GoogleTranslate\GoogleTranslate;
$tr = new GoogleTranslate('en'); // Translates into English
$tr = new GoogleTranslate(); // Translates to 'en' from auto-detected language by default
$tr->setSource('en'); // Translate from English
$tr->setSource(); // Detect language automatically
$tr->setTarget('ka'); // Translate to Georgian
$tr = new GoogleTranslate('es', null); // Or simply do not pass the second parameter
$tr->setSource(); // Another way
$tr = new GoogleTranslate('fr');
$text = $tr->translate('Hello World!');
echo $tr->getLastDetectedSource(); // Output: en
$tr = new GoogleTranslate('de');
$text = $tr->translate('Page :current of :total'); // Seite :aktuell von :gesamt
$text = $tr->preserveParameters()
->translate('Page :current of :total'); // Seite :current von :total
$text = $tr->preserveParameters('/\{\{([^}]+)\}\}/')
->translate('Page {{current}} of {{total}}'); // Seite {{current}} von {{total}}
// Set proxy to tcp://localhost:8090
$tr->setOptions(['proxy' => 'tcp://localhost:8090'])->translate('Hello');
// Set proxy to socks5://localhost:1080
$tr->setOptions(['proxy' => 'socks5://localhost:1080'])->translate('World');
use Stichoza\GoogleTranslate\Tokens\TokenProviderInterface;
class MyTokenGenerator implements TokenProviderInterface
{
public function generateToken(string $source, string $target, string $text): string
{
// Your code here
}
}
$tr->setTokenProvider(new MyTokenGenerator);
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.