Skip to content

Commit db5e3d0

Browse files
committed
clon
0 parents  commit db5e3d0

File tree

6 files changed

+968
-0
lines changed

6 files changed

+968
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
src/.DS_Store
3+
.DS_Store

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GoogleTranslateForFree PHP Library Changelog
2+
Author: Yuri Darwin
3+
Author e-mail: [email protected]
4+
5+
## v1.0.0
6+
This is the first release of Google Translate for Free library by Yuri Darwin

LICENSE

+674
Large diffs are not rendered by default.

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# GoogleTranslateForFree
2+
Library for free use Google Translator. With attempts connecting on failure and array support.
3+
4+
## Installation
5+
6+
Install this package via [Composer](https://getcomposer.org/).
7+
8+
```
9+
composer require dejurin/php-google-translate-for-free
10+
```
11+
12+
Or edit your project's `composer.json` to require `dejurin/php-google-translate-for-free` and then run `composer update`.
13+
14+
```json
15+
"require": {
16+
"dejurin/php-google-translate-for-free": "^1.0"
17+
}
18+
```
19+
20+
## Usage
21+
22+
```php
23+
require_once ('vendor/autoload.php');
24+
use \Dejurin\GoogleTranslateForFree;
25+
```
26+
27+
## Single
28+
29+
```php
30+
$source = 'en';
31+
$target = 'ru';
32+
$attempts = 5;
33+
$text = 'Hello';
34+
35+
$tr = new GoogleTranslateForFree();
36+
$result = $tr->translate($source, $target, $text, $attempts);
37+
38+
echo $result;
39+
40+
/*
41+
string(24) "Здравствуйте"
42+
*/
43+
```
44+
45+
## Array
46+
47+
```php
48+
$source = 'en';
49+
$target = 'ru';
50+
$attempts = 5;
51+
$arr = array('hello','world');
52+
53+
$tr = new GoogleTranslateForFree();
54+
$result = $tr->translate($source, $target, $arr, $attempts);
55+
56+
echo $result;
57+
58+
/*
59+
array(2) {
60+
[0]=>
61+
string(24) "Здравствуйте"
62+
[1]=>
63+
string(6) "Мир"
64+
}
65+
66+
*/
67+
```

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "dejurin/php-google-translate-for-free",
3+
"description": "Library for free use Google Translator. With attempts connecting on failure and array support.",
4+
"version": "1.0.0",
5+
"keywords": [
6+
"google",
7+
"translator",
8+
"translate",
9+
"api",
10+
"free"
11+
],
12+
"homepage": "https://github.com/dejurin/php-google-translate-for-free",
13+
"license": "GPL-3.0+",
14+
"support":
15+
{
16+
"issues": "https://github.com/dejurin/php-google-translate-for-free/issues",
17+
"source": "https://github.com/dejurin/php-google-translate-for-free/releases"
18+
},
19+
"require":
20+
{
21+
"php": ">=5.4"
22+
},
23+
"autoload":
24+
{
25+
"psr-4":
26+
{
27+
"Dejurin\\": "src/"
28+
}
29+
}
30+
}

src/GoogleTranslateForFree.php

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace Dejurin;
4+
5+
/**
6+
* GoogleTranslateForFree.php.
7+
*
8+
* Class for free use Google Translator. With attempts connecting on failure and array support.
9+
*
10+
* @category Translation
11+
*
12+
* @author Yuri Darwin
13+
* @author Yuri Darwin <[email protected]>
14+
* @copyright 2018 Yuri Darwin
15+
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
16+
*
17+
* @version 1.0.0
18+
*/
19+
20+
/**
21+
* Main class GoogleTranslateForFree.
22+
*/
23+
class GoogleTranslateForFree
24+
{
25+
/**
26+
* @param string $source
27+
* @param string $target
28+
* @param string|array $text
29+
* @param int $attempts
30+
*
31+
* @return string|array With the translation of the text in the target language
32+
*/
33+
public static function translate($source, $target, $text, $attempts = 5)
34+
{
35+
// Request translation
36+
if (is_array($text)) {
37+
// Array
38+
$translation = self::requestTranslationArray($source, $target, $text, $attempts = 5);
39+
} else {
40+
// Single
41+
$translation = self::requestTranslation($source, $target, $text, $attempts = 5);
42+
}
43+
44+
return $translation;
45+
}
46+
47+
/**
48+
* @param string $source
49+
* @param string $target
50+
* @param array $text
51+
* @param int $attempts
52+
*
53+
* @return array
54+
*/
55+
protected static function requestTranslationArray($source, $target, $text, $attempts)
56+
{
57+
$arr = array();
58+
foreach ($text as $value) {
59+
// timeout 0.5 sec
60+
usleep(500000);
61+
$arr[] = self::requestTranslation($source, $target, $value, $attempts = 5);
62+
}
63+
64+
return $arr;
65+
}
66+
67+
/**
68+
* @param string $source
69+
* @param string $target
70+
* @param string $text
71+
* @param int $attempts
72+
*
73+
* @return string
74+
*/
75+
protected static function requestTranslation($source, $target, $text, $attempts)
76+
{
77+
// Google translate URL
78+
$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';
79+
80+
$fields = array(
81+
'sl' => urlencode($source),
82+
'tl' => urlencode($target),
83+
'q' => urlencode($text),
84+
);
85+
86+
if (strlen($fields['q']) >= 5000) {
87+
throw new \Exception('Maximum number of characters exceeded: 5000');
88+
}
89+
// URL-ify the data for the POST
90+
$fields_string = self::fieldsString($fields);
91+
92+
$content = self::curlRequest($url, $fields, $fields_string, 0, $attempts);
93+
94+
if (null === $content) {
95+
//echo $text,' Error',PHP_EOL;
96+
return '';
97+
} else {
98+
// Parse translation
99+
return self::getSentencesFromJSON($content);
100+
}
101+
}
102+
103+
/**
104+
* Dump of the JSON's response in an array.
105+
*
106+
* @param string $json
107+
*
108+
* @return string
109+
*/
110+
protected static function getSentencesFromJSON($json)
111+
{
112+
$arr = json_decode($json, true);
113+
$sentences = '';
114+
115+
if (isset($arr['sentences'])) {
116+
foreach ($arr['sentences'] as $s) {
117+
$sentences .= isset($s['trans']) ? $s['trans'] : '';
118+
}
119+
}
120+
121+
return $sentences;
122+
}
123+
124+
/**
125+
* Curl Request attempts connecting on failure.
126+
*
127+
* @param string $url
128+
* @param array $fields
129+
* @param string $fields_string
130+
* @param int $i
131+
* @param int $attempts
132+
*
133+
* @return string
134+
*/
135+
protected static function curlRequest($url, $fields, $fields_string, $i, $attempts)
136+
{
137+
++$i;
138+
$ch = curl_init();
139+
curl_setopt($ch, CURLOPT_URL, $url);
140+
curl_setopt($ch, CURLOPT_POST, count($fields));
141+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
142+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
143+
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
144+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
145+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
146+
//curl_setopt($ch, CURLOPT_HEADER, true);
147+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
148+
curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');
149+
150+
$result = curl_exec($ch);
151+
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
152+
153+
if (false === $result || 200 !== $httpcode) {
154+
// echo $i,'/',$attempts,' Aborted, trying again... ',curl_error($ch),PHP_EOL;
155+
156+
if ($i >= $attempts) {
157+
//echo 'Could not connect and get data.',PHP_EOL;
158+
return null;
159+
//die('Could not connect and get data.'.PHP_EOL);
160+
} else {
161+
// timeout 1.5 sec
162+
usleep(1500000);
163+
164+
return self::curlRequest($url, $fields, $fields_string, $i, $attempts);
165+
}
166+
} else {
167+
return $result; //self::getBodyCurlResponse();
168+
}
169+
curl_close($ch);
170+
}
171+
172+
/**
173+
* Make string with post data fields.
174+
*
175+
* @param array $fields
176+
*
177+
* @return string
178+
*/
179+
protected static function fieldsString($fields)
180+
{
181+
$fields_string = '';
182+
foreach ($fields as $key => $value) {
183+
$fields_string .= $key.'='.$value.'&';
184+
}
185+
186+
return rtrim($fields_string, '&');
187+
}
188+
}

0 commit comments

Comments
 (0)