1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Translate; |
4
|
|
|
|
5
|
|
|
use JsonSchema\Exception\RuntimeException; |
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
trait TraitTranslate |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $dir |
14
|
|
|
* create dir |
15
|
|
|
*/ |
16
|
|
|
private function create(string $dir): void |
17
|
|
|
{ |
18
|
|
|
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) { |
19
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* create dir defaults |
25
|
|
|
*/ |
26
|
|
|
protected function dir(): void |
27
|
|
|
{ |
28
|
|
|
$this->create($this->getPath()); |
|
|
|
|
29
|
|
|
$this->create("{$this->getPath()}/{$this->getSource()}"); |
|
|
|
|
30
|
|
|
$this->create("{$this->getPath()}/{$this->getSource()}/public"); |
31
|
|
|
$this->create("{$this->getPath()}/{$this->getTarget()}"); |
|
|
|
|
32
|
|
|
$this->create("{$this->getPath()}/{$this->getTarget()}/public"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* create files .translate |
37
|
|
|
*/ |
38
|
|
|
protected function file(string $module = null): void |
39
|
|
|
{ |
40
|
|
|
if ($module) { |
41
|
|
|
$modulePath = dirname(__DIR__, 4)."/modules/{$module}/translate"; |
42
|
|
|
$this->create("{$modulePath}/{$this->getSource()}"); |
43
|
|
|
$this->create("{$modulePath}/{$this->getSource()}/public"); |
44
|
|
|
$this->create("{$modulePath}/{$this->getTarget()}"); |
45
|
|
|
$this->create("{$modulePath}/{$this->getTarget()}/public"); |
46
|
|
|
$this->setSourceFile("{$modulePath}/{$this->getSource()}/{$this->getData()->file}.translate"); |
|
|
|
|
47
|
|
|
$this->setTargetFile("{$modulePath}/{$this->getTarget()}/{$this->getData()->file}.translate"); |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
$this->setSourceFile("{$this->getPath()}/{$this->getSource()}/{$this->getData()->file}.translate"); |
50
|
|
|
$this->setTargetFile("{$this->getPath()}/{$this->getTarget()}/{$this->getData()->file}.translate"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!is_file($this->getSourceFile())) { |
|
|
|
|
54
|
|
|
file_put_contents($this->getSourceFile(), $this->getData()->text . PHP_EOL); |
55
|
|
|
} else if(!in_array($this->getData()->text . PHP_EOL, array_filter(file($this->getSourceFile())), true)){ |
56
|
|
|
file_put_contents($this->getSourceFile(), $this->getData()->text . PHP_EOL, FILE_APPEND); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!is_file($this->getTargetFile())) { |
|
|
|
|
60
|
|
|
file_put_contents($this->getTargetFile(), $this->translate(file_get_contents($this->getSourceFile())). PHP_EOL); |
61
|
|
|
} else { |
62
|
|
|
$source = array_filter(file($this->getSourceFile())); |
63
|
|
|
$target = array_filter(file($this->getTargetFile())); |
64
|
|
|
$result = array_diff_key($source, $target); |
65
|
|
|
$implode = implode("", $result); |
66
|
|
|
if(count(array_filter(file($this->getSourceFile()))) > count(array_filter(file($this->getTargetFile())))){ |
67
|
|
|
file_put_contents($this->getTargetFile(), $this->translate($implode) . PHP_EOL, FILE_APPEND); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $text |
76
|
|
|
* @return mixed |
77
|
|
|
* send server translate |
78
|
|
|
*/ |
79
|
|
|
private function translate(string $text): mixed |
80
|
|
|
{ |
81
|
|
|
$url = TRANSLATE_API_URL; |
82
|
|
|
$ch = curl_init($url); |
83
|
|
|
curl_setopt_array($ch, [ |
84
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
85
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
86
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
87
|
|
|
"key" => TRANSLATE_API_KEY, |
|
|
|
|
88
|
|
|
"source" => "en", |
89
|
|
|
"target" => $this->getTarget(), |
90
|
|
|
"text" => $text, |
91
|
|
|
"route" => $this->getData()->file |
92
|
|
|
]) |
93
|
|
|
]); |
94
|
|
|
$response = curl_exec($ch); |
95
|
|
|
curl_close($ch); |
96
|
|
|
if (!$response) { |
97
|
|
|
return $text; |
98
|
|
|
} |
99
|
|
|
$data = json_decode($response); |
|
|
|
|
100
|
|
|
if ($data->status === "success") { |
101
|
|
|
return $data->translate; |
102
|
|
|
} |
103
|
|
|
return $text; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths