Skip to content

Commit

Permalink
clon
Browse files Browse the repository at this point in the history
  • Loading branch information
dejurin committed Mar 2, 2018
0 parents commit db5e3d0
Show file tree
Hide file tree
Showing 6 changed files with 968 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

src/.DS_Store
.DS_Store
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# GoogleTranslateForFree PHP Library Changelog
Author: Yuri Darwin
Author e-mail: gkhelloworld@gmail.com

## v1.0.0
This is the first release of Google Translate for Free library by Yuri Darwin
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# GoogleTranslateForFree
Library for free use Google Translator. With attempts connecting on failure and array support.

## Installation

Install this package via [Composer](https://getcomposer.org/).

```
composer require dejurin/php-google-translate-for-free
```

Or edit your project's `composer.json` to require `dejurin/php-google-translate-for-free` and then run `composer update`.

```json
"require": {
"dejurin/php-google-translate-for-free": "^1.0"
}
```

## Usage

```php
require_once ('vendor/autoload.php');
use \Dejurin\GoogleTranslateForFree;
```

## Single

```php
$source = 'en';
$target = 'ru';
$attempts = 5;
$text = 'Hello';

$tr = new GoogleTranslateForFree();
$result = $tr->translate($source, $target, $text, $attempts);

echo $result;

/*
string(24) "Здравствуйте"
*/
```

## Array

```php
$source = 'en';
$target = 'ru';
$attempts = 5;
$arr = array('hello','world');

$tr = new GoogleTranslateForFree();
$result = $tr->translate($source, $target, $arr, $attempts);

echo $result;

/*
array(2) {
[0]=>
string(24) "Здравствуйте"
[1]=>
string(6) "Мир"
}

*/
```
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "dejurin/php-google-translate-for-free",
"description": "Library for free use Google Translator. With attempts connecting on failure and array support.",
"version": "1.0.0",
"keywords": [
"google",
"translator",
"translate",
"api",
"free"
],
"homepage": "https://github.com/dejurin/php-google-translate-for-free",
"license": "GPL-3.0+",
"support":
{
"issues": "https://github.com/dejurin/php-google-translate-for-free/issues",
"source": "https://github.com/dejurin/php-google-translate-for-free/releases"
},
"require":
{
"php": ">=5.4"
},
"autoload":
{
"psr-4":
{
"Dejurin\\": "src/"
}
}
}
188 changes: 188 additions & 0 deletions src/GoogleTranslateForFree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Dejurin;

/**
* GoogleTranslateForFree.php.
*
* Class for free use Google Translator. With attempts connecting on failure and array support.
*
* @category Translation
*
* @author Yuri Darwin
* @author Yuri Darwin <gkhelloworld@gmail.com>
* @copyright 2018 Yuri Darwin
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
*
* @version 1.0.0
*/

/**
* Main class GoogleTranslateForFree.
*/
class GoogleTranslateForFree
{
/**
* @param string $source
* @param string $target
* @param string|array $text
* @param int $attempts
*
* @return string|array With the translation of the text in the target language
*/
public static function translate($source, $target, $text, $attempts = 5)
{
// Request translation
if (is_array($text)) {
// Array
$translation = self::requestTranslationArray($source, $target, $text, $attempts = 5);
} else {
// Single
$translation = self::requestTranslation($source, $target, $text, $attempts = 5);
}

return $translation;
}

/**
* @param string $source
* @param string $target
* @param array $text
* @param int $attempts
*
* @return array
*/
protected static function requestTranslationArray($source, $target, $text, $attempts)
{
$arr = array();
foreach ($text as $value) {
// timeout 0.5 sec
usleep(500000);
$arr[] = self::requestTranslation($source, $target, $value, $attempts = 5);
}

return $arr;
}

/**
* @param string $source
* @param string $target
* @param string $text
* @param int $attempts
*
* @return string
*/
protected static function requestTranslation($source, $target, $text, $attempts)
{
// Google translate URL
$url = 'https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=uk-RU&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e';

$fields = array(
'sl' => urlencode($source),
'tl' => urlencode($target),
'q' => urlencode($text),
);

if (strlen($fields['q']) >= 5000) {
throw new \Exception('Maximum number of characters exceeded: 5000');
}
// URL-ify the data for the POST
$fields_string = self::fieldsString($fields);

$content = self::curlRequest($url, $fields, $fields_string, 0, $attempts);

if (null === $content) {
//echo $text,' Error',PHP_EOL;
return '';
} else {
// Parse translation
return self::getSentencesFromJSON($content);
}
}

/**
* Dump of the JSON's response in an array.
*
* @param string $json
*
* @return string
*/
protected static function getSentencesFromJSON($json)
{
$arr = json_decode($json, true);
$sentences = '';

if (isset($arr['sentences'])) {
foreach ($arr['sentences'] as $s) {
$sentences .= isset($s['trans']) ? $s['trans'] : '';
}
}

return $sentences;
}

/**
* Curl Request attempts connecting on failure.
*
* @param string $url
* @param array $fields
* @param string $fields_string
* @param int $i
* @param int $attempts
*
* @return string
*/
protected static function curlRequest($url, $fields, $fields_string, $i, $attempts)
{
++$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (false === $result || 200 !== $httpcode) {
// echo $i,'/',$attempts,' Aborted, trying again... ',curl_error($ch),PHP_EOL;

if ($i >= $attempts) {
//echo 'Could not connect and get data.',PHP_EOL;
return null;
//die('Could not connect and get data.'.PHP_EOL);
} else {
// timeout 1.5 sec
usleep(1500000);

return self::curlRequest($url, $fields, $fields_string, $i, $attempts);
}
} else {
return $result; //self::getBodyCurlResponse();
}
curl_close($ch);
}

/**
* Make string with post data fields.
*
* @param array $fields
*
* @return string
*/
protected static function fieldsString($fields)
{
$fields_string = '';
foreach ($fields as $key => $value) {
$fields_string .= $key.'='.$value.'&';
}

return rtrim($fields_string, '&');
}
}

0 comments on commit db5e3d0

Please sign in to comment.