1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TopviewDigital\TranslationHelper\Service; |
4
|
|
|
|
5
|
|
|
use Campo\UserAgent; |
6
|
|
|
use Illuminate\Bus\Queueable; |
7
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
8
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
9
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
10
|
|
|
use Illuminate\Queue\SerializesModels; |
11
|
|
|
use Stichoza\GoogleTranslate\GoogleTranslate; |
12
|
|
|
use TopviewDigital\TranslationHelper\Model\VocabTerm; |
13
|
|
|
|
14
|
|
|
class Translation implements ShouldQueue |
15
|
|
|
{ |
16
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
|
|
|
17
|
|
|
|
18
|
|
|
protected $break = 0; |
19
|
|
|
protected $term; |
20
|
|
|
protected $locales; |
21
|
|
|
protected $translation; |
22
|
|
|
|
23
|
|
|
public function __construct(VocabTerm $term = null, array $locales = []) |
24
|
|
|
{ |
25
|
|
|
$this->locales = array_unique( |
26
|
|
|
array_merge($locales, [ |
27
|
|
|
app()->getLocale(), |
|
|
|
|
28
|
|
|
config('app.locale'), |
29
|
|
|
config('app.fallback_locale'), |
30
|
|
|
config('app.faker_locale'), |
31
|
|
|
]) |
32
|
|
|
); |
33
|
|
|
$this->term = $term; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function handle() |
37
|
|
|
{ |
38
|
|
|
if (empty($this->term)) { |
39
|
|
|
sweep(); |
40
|
|
|
foreach (VocabTerm::get()->all() as $u) { |
41
|
|
|
$this->translation($u, $this->locales); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
} else { |
44
|
|
|
$this->translation($this->term); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function randomUserAgent() |
49
|
|
|
{ |
50
|
|
|
sleep(1); |
51
|
|
|
$this->called++; |
52
|
|
|
|
53
|
|
|
return [ |
54
|
|
|
'headers' => [ |
55
|
|
|
'User-Agent' => UserAgent::random(), |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function translation(VocabTerm $term) |
61
|
|
|
{ |
62
|
|
|
$row = 0; |
63
|
|
|
while ($row < 1) { |
64
|
|
|
$this->called = 0; |
|
|
|
|
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$language = new GoogleTranslate(); |
68
|
|
|
// $language->setOptions($this->randomUserAgent()) |
69
|
|
|
// ->setSource() |
70
|
|
|
// ->setTarget(config('app.locale')) |
71
|
|
|
// ->translate($term->term); |
72
|
|
|
// $default_locale = $language->setOptions($this->randomUserAgent())->getLastDetectedSource(); |
73
|
|
|
$translation = empty($term->translation) ? [] : $term->translation; |
|
|
|
|
74
|
|
|
foreach ($this->locales as $locale) { |
75
|
|
|
if (!array_key_exists($locale, $term->translation)) { |
76
|
|
|
$translation[$locale] = $language |
77
|
|
|
->setOptions($this->randomUserAgent()) |
78
|
|
|
->setSource() |
79
|
|
|
->setTarget($locale) |
80
|
|
|
->translate($term->term); |
|
|
|
|
81
|
|
|
$this->called++; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
$locale = $language->setOptions($this->randomUserAgent())->getLastDetectedSource(); |
85
|
|
|
if (!array_key_exists($locale, $term->translation)) { |
86
|
|
|
$translation[$locale] = $term->term; |
87
|
|
|
} |
88
|
|
|
$term->translation = $translation; |
89
|
|
|
$language = null; |
|
|
|
|
90
|
|
|
$term->save(); |
91
|
|
|
$row++; |
92
|
|
|
} catch (Exception $e) { |
|
|
|
|
93
|
|
|
$this->break++; |
94
|
|
|
$mins = rand( |
95
|
|
|
floor($this->called / ($row + 1)), |
96
|
|
|
floor($this->called / ($row + 1) * 2) |
97
|
|
|
) * $this->break; |
98
|
|
|
sleep($mins * 60); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|