Skip to content

Commit

Permalink
Merge pull request #8 from viniciusgava/hotfix/multiple-strings-bug-a…
Browse files Browse the repository at this point in the history
…nd-add-examples-at-readme.md

Examples of usage and fix multiple string input
  • Loading branch information
viniciusgava authored Oct 8, 2017
2 parents c033aae + a546491 commit f3f7d12
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 23 deletions.
244 changes: 244 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,247 @@

This project abstract the google translate api versio 2.0 in PHP.
The lib has been refactored to support unit tests, composer, and a better architecture.

## Installation
Use composer to install the lib
```
composer require viniciusgava/google-translate-api
```

## Examples of Usage
###Translate with source language detection
```php
<?php
require_once 'vendor/autoload.php';

$client = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');

echo $client->translate('Hello world!', 'pt-br', $sourceLanguage);
// output: Olá Mundo!

echo $sourceLanguage;
// output: en
```

###Translate without source language detection
```php
<?php
require_once 'vendor/autoload.php';

$client = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');

$sourceLanguage = 'pt-br';
echo $client->translate('Onde estou?', 'en', $sourceLanguage);
// output: Where am I?
```

###Translate bundle of texts with language detection
```php
<?php
require_once 'vendor/autoload.php';

$client = 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
)
*/
```

### Detect language of a bundle of text
```php
<?php
require_once 'vendor/autoload.php';

$client = 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
)

)
*/
```
### Detect language of a text
```php
<?php
require_once 'vendor/autoload.php';

$client = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');

print_r($client->detect('Let\'s help the community!'));
/* output:
Array
(
[confidence] => 0.26097252964973
[isReliable] =>
[language] => en
)
*/
```
### List supported languages with name of language translated for a specific language
```php
<?php
require_once 'vendor/autoload.php';

$client = new \GoogleTranslate\Client('GOOGLE ACCESS KEY HERE');

print_r($client->languages('pt-br'));
/* output:
Array
(
[0] => Array
(
[language] => af
[name] => Africâner
)

[1] => Array
(
[language] => sq
[name] => Albanês
)

[2] => Array
(
[language] => de
[name] => Alemão
)

[3] => Array
(
[language] => ar
[name] => Árabe
)

[4] => Array
(
[language] => hy
[name] => Armênio
)
[5] => Array
(
[language] => zh
[name] => Chinês (simplificado)
)
[6] => Array
(
[language] => fr
[name] => Francês
)
.
.
.
)
*/
```
### List supported languages
```php
<?php
require_once 'vendor/autoload.php';

$client = new \GoogleTranslate\Client('GOOGLE ACCESS KEY');

print_r($client->languages());
/* output:
Array
(
[0] => Array
(
[language] => af
)

[1] => Array
(
[language] => am
)

[2] => Array
(
[language] => ar
)

[3] => Array
(
[language] => az
)

[4] => Array
(
[language] => be
)

[5] => Array
(
[language] => bg
)

[6] => Array
(
[language] => bn
)
.
.
.
)
*/
```
30 changes: 16 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,25 @@ private function getHttpClient()
*/
public function translate($text, $targetLanguage, &$sourceLanguage = null)
{
// validate if required fields has being filled.
if (!$text) {
throw new Exception\InvalidTextException();
}

// used to return the same type of variable used in the text
$onceResult = !is_array($text);

// prepare the string
$text = $this->prepareText($text);

// validate if required fields has being filled.
if (!$text) {
throw new Exception\InvalidTextException();
}

if (!$this->isValidLanguage($targetLanguage)) {
throw new Exception\InvalidTargetLanguageException();
}

// query params
$query = [
'key' => $this->accessKey,
'q' => $text,
'target' => $targetLanguage,
'target' => $targetLanguage
];

// validate if is necessary to pass the source language.
Expand All @@ -97,6 +96,9 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null)
$query['source'] = $sourceLanguage;
}

// add access key
$query['key'] = $this->accessKey;

try {
// send request
$query = $this->httpBuildQuery($query);
Expand Down Expand Up @@ -186,21 +188,21 @@ public function languages($targetLanguage = null)
*/
public function detect($text)
{
// validate if required fields has being filled.
if (!$text) {
throw new Exception\InvalidTextException();
}

// used to return the same type of variable used in the text
$onceResult = !is_array($text);

// prepare the string
$text = $this->prepareText($text);

// validate if required fields has being filled.
if (!$text) {
throw new Exception\InvalidTextException();
}

// query params
$query = [
'q' => $text,
'key' => $this->accessKey,
'q' => $text
];

try {
Expand Down Expand Up @@ -272,7 +274,7 @@ private function httpBuildQuery($params)
private function prepareText($text)
{
// convert no array text to array
if (is_array($text)) {
if (!is_array($text)) {
$text = [$text];
}

Expand Down
Loading

0 comments on commit f3f7d12

Please sign in to comment.