

First it was set to webapp but later google added gtx value which results in a better translation quality in terms of grammar and overall meaning of sentences. Google Translate has a parameter named client which defines quality of translation. Public function generateToken( string $source, string $target, string $text): stringĪnd use: $tr-> setTokenProvider( new MyTokenGenerator) Translation Client (Quality) use Stichoza\ GoogleTranslate\ Tokens\ TokenProviderInterface Ĭlass MyTokenGenerator implements TokenProviderInterface Generator must implement Stichoza\GoogleTranslate\Tokens\TokenProviderInterface. You can override the token generator class by passing a generator object as a fourth parameter of constructor or just use setTokenProvider method. Set proxy to socks5://localhost:1080 $tr-> setOptions()-> translate( 'World') įor more information, see Creating a Client section in Guzzle docs. ]) // Set proxy to tcp://localhost:8090 $tr-> setOptions()-> translate( 'Hello') 'User-Agent' => 'Foo/5.0 Lorem Ipsum Browser' You can configure proxy, user-agent, default headers, connection timeout and so on using this options. You can pass an array of guzzle client configuration options as a third parameter to GoogleTranslate constructor, or just use setOptions method. This package uses Guzzle for HTTP requests. Useful for some countries $tr-> setUrl( '') HTTP Client Configuration You can override the default Google Translate url by setUrl method. $responseArray = $tr-> getResponse( 'Hello world!') Custom URL Using Raw Responseįor advanced usage, you might need the raw results that Google Translate provides. Supported languages are listed in Google API docs. Return value will be null if the language couldn't be detected.

$text = $tr-> translate( 'Hello World!') Įcho $tr-> getLastDetectedSource() // Output: en

Use getLastDetectedSource() to get detected language: $tr = new GoogleTranslate( 'fr') To detect language automatically, just set the source language to null: $tr = new GoogleTranslate( 'es', null) // Or simply do not pass the second parameter $tr-> setSource() // Another way Or call a shorthand static method trans echo GoogleTranslate:: trans( 'Hello again', 'ka', 'en') Advanced Usage Language Detection Translate sentences echo $tr-> translate( 'Hello World!') Īlso, you can also use method chaining echo $tr-> setSource( 'en')-> setTarget( 'ka')-> translate( 'Goodbye') PHP setSource( 'en') // Translate from English $tr-> setSource() // Detect language automatically $tr-> setTarget( 'ka') // Translate to Georgian Use following versions of this package for older PHP versions: Package version composer require stichoza/google-translate-php
