PHP code example of viniciusgava / google-translate-api
1. Go to this page and download the library: Download viniciusgava/google-translate-api 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/ */
viniciusgava / google-translate-api example snippets
ent = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');
echo $client->translate('Hello world!', 'pt-br', $sourceLanguage);
// output: Olá Mundo!
echo $sourceLanguage;
// output: en
ent = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');
$sourceLanguage = 'pt-br';
echo $client->translate('Onde estou?', 'en', $sourceLanguage);
// output: Where am I?
ent = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');
$texts = [
'¿Cómo estás?',
'あなたはどこに住んでいますか?',
'Where are you going?',
'Essa lib é muito legal!'
];
print_r($client->translate($texts, 'en', $sourceLanguage));
/* output:
Array
(
[0] => How are you?
[1] => Where do you live?
[2] => Where are you going?
[3] => This lib is really cool!
)
*/
print_r($sourceLanguage);
/* output:
Array
(
[0] => es
[1] => ja
[2] => en
[3] => pt
)
*/
ent = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');
$texts = [
'¿Cómo estás?',
'あなたはどこに住んでいますか?',
'Where are you going?',
'Essa lib é muito legal!'
];
print_r($client->detect($texts));
/* output:
Array
(
[0] => Array
(
[confidence] => 0.67241430282593
[isReliable] =>
[language] => es
)
[1] => Array
(
[confidence] => 1
[isReliable] =>
[language] => ja
)
[2] => Array
(
[confidence] => 0.67237991094589
[isReliable] =>
[language] => en
)
[3] => Array
(
[confidence] => 0.25708484649658
[isReliable] =>
[language] => pt
)
)
*/
ent = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');
print_r($client->detect('Let\'s help the community!'));
/* output:
Array
(
[confidence] => 0.26097252964973
[isReliable] =>
[language] => en
)
*/