|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aitor24\Laralang\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use Aitor24\Laralang\Models\DB_Translation; |
|
6
|
|
|
use App; |
|
7
|
|
|
|
|
8
|
|
|
class Translation |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Setup public vars. |
|
12
|
|
|
*/ |
|
13
|
|
|
public $translation; |
|
14
|
|
|
public $translator; |
|
15
|
|
|
public $string; |
|
16
|
|
|
public $debug; |
|
17
|
|
|
public $from; |
|
18
|
|
|
public $to; |
|
19
|
|
|
public $save; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Setup default values. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($string) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->translator = config('laralang.default.translator'); |
|
29
|
|
|
$this->debug = config('laralang.default.debug'); |
|
30
|
|
|
$this->save = config('laralang.default.autosave'); |
|
31
|
|
|
$this->from = config('laralang.default.from_lang'); |
|
32
|
|
|
$this->to = config('laralang.default.to_lang'); |
|
33
|
|
|
$this->string = $string; |
|
34
|
|
|
$this->translation = $string; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
// Checking whether from_lang or to_lang are set as app_locale. |
|
38
|
|
|
|
|
39
|
|
|
if ($this->from == 'app_locale') { |
|
40
|
|
|
$this->from = App::getLocale(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($this->to == 'app_locale') { |
|
44
|
|
|
$this->to = App::getLocale(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Setup debug value. |
|
50
|
|
|
* |
|
51
|
|
|
* @param bool $debug |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setDebug($debug) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->debug = $debug; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Setup fromLang value. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $lang |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setFrom($lang) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->from = $lang; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Setup tolang value. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $lang |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setTo($lang) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->to = $lang; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Setup translator. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $translator |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setTranslator($translator) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->translator = $translator; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Setup save option. |
|
98
|
|
|
* |
|
99
|
|
|
* @param bool $save |
|
100
|
|
|
*/ |
|
101
|
|
|
public function Save($save) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->save = $save; |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function loadIfExists() |
|
109
|
|
|
{ |
|
110
|
|
|
$existing = DB_Translation::where('string', '=', $this->string) |
|
111
|
|
|
->where('from_lang', '=', $this->from) |
|
112
|
|
|
->where('to_lang', '=', $this->to) |
|
113
|
|
|
->where('translator', '=', $this->translator)->get(); |
|
114
|
|
|
|
|
115
|
|
|
if (count($existing) == 0) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
if ($this->debug === true) { |
|
119
|
|
|
$this->translation = "<font style='color:#00CC00;'>Translation loaded from DB</font>"; |
|
120
|
|
|
} else { |
|
121
|
|
|
$this->translation = $existing[0]->translation; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Function to save translations to DB. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function checkSave() |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->save === true) { |
|
133
|
|
|
$trans = new DB_Translation(); |
|
134
|
|
|
$trans->string = $this->string; |
|
135
|
|
|
$trans->from_lang = $this->from; |
|
136
|
|
|
$trans->to_lang = $this->to; |
|
137
|
|
|
$trans->translator = $this->translator; |
|
138
|
|
|
$trans->translation = $this->translation; |
|
139
|
|
|
$trans->save(); |
|
140
|
|
|
|
|
141
|
|
|
// Checking debug setting to determinate how to output translation |
|
142
|
|
|
|
|
143
|
|
|
if ($this->debug === true) { |
|
144
|
|
|
$this->translation = "<font style='color:#00CC00;'> Translation saved on DB </font>"; |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
if ($this->debug === true) { |
|
148
|
|
|
$this->translation = "<font style='color:orange;'> Translation not saved on DB </font>"; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* This fuction is called to know the status of host, and it would set translation if debug is true. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $host |
|
157
|
|
|
*/ |
|
158
|
|
|
public function checkHost($host) |
|
159
|
|
|
{ |
|
160
|
|
|
$socket = @fsockopen($host, 80, $errno, $errstr, 30); |
|
161
|
|
|
|
|
162
|
|
|
if ($socket) { |
|
163
|
|
|
return true; |
|
164
|
|
|
fclose($socket); |
|
|
|
|
|
|
165
|
|
|
} else { |
|
166
|
|
|
if ($this->debug === true) { |
|
167
|
|
|
$this->translation = "<font style='color:red;'>$this->translator host is down! </font>"; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* This fuction is called by trans() function of Fadade Laralang |
|
176
|
|
|
* It would call run() function of this class and returns the translation |
|
177
|
|
|
* |
|
178
|
|
|
*/ |
|
179
|
|
|
public function __toString() |
|
180
|
|
|
{ |
|
181
|
|
|
if (!$this->loadIfExists()){ |
|
182
|
|
|
$this->main(); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $this->translation; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.