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
|
|
|
public $vars; |
21
|
|
|
public $load; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Setup default values. |
25
|
|
|
* |
26
|
|
|
* @param string $string |
27
|
|
|
*/ |
28
|
|
|
public function __construct($string, $vars = []) |
29
|
|
|
{ |
30
|
|
|
$this->translator = config('laralang.default.translator'); |
31
|
|
|
$this->debug = config('laralang.default.debug'); |
32
|
|
|
$this->save = config('laralang.default.autosave'); |
33
|
|
|
$this->load = config('laralang.default.autoload'); |
34
|
|
|
$this->from = config('laralang.default.from_lang'); |
35
|
|
|
$this->to = config('laralang.default.to_lang'); |
36
|
|
|
$this->string = $string; |
37
|
|
|
$this->translation = $string; |
38
|
|
|
$this->vars = $vars; |
39
|
|
|
|
40
|
|
|
// Checking whether from_lang or to_lang are set as app_locale. |
41
|
|
|
|
42
|
|
|
if ($this->from == 'app_locale') { |
43
|
|
|
$this->from = App::getLocale(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($this->to == 'app_locale') { |
47
|
|
|
$this->to = App::getLocale(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Setup debug value. |
53
|
|
|
* |
54
|
|
|
* @param bool $debug |
55
|
|
|
*/ |
56
|
|
|
public function debug($debug) |
57
|
|
|
{ |
58
|
|
|
$this->debug = $debug; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Setup fromLang value. |
65
|
|
|
* |
66
|
|
|
* @param string $lang |
67
|
|
|
*/ |
68
|
|
|
public function from($lang) |
69
|
|
|
{ |
70
|
|
|
$this->from = $lang; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Setup tolang value. |
77
|
|
|
* |
78
|
|
|
* @param string $lang |
79
|
|
|
*/ |
80
|
|
|
public function to($lang) |
81
|
|
|
{ |
82
|
|
|
$this->to = $lang; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setup translator. |
89
|
|
|
* |
90
|
|
|
* @param string $translator |
91
|
|
|
*/ |
92
|
|
|
public function translator($translator) |
93
|
|
|
{ |
94
|
|
|
$this->translator = $translator; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Setup save option. |
101
|
|
|
* |
102
|
|
|
* @param bool $save |
103
|
|
|
*/ |
104
|
|
|
public function Save($save) |
105
|
|
|
{ |
106
|
|
|
$this->save = $save; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Setup load option. |
113
|
|
|
* |
114
|
|
|
* @param bool $load |
115
|
|
|
*/ |
116
|
|
|
public function load($load) |
117
|
|
|
{ |
118
|
|
|
$this->load = $load; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function loadIfExists() |
124
|
|
|
{ |
125
|
|
|
$existing = DB_Translation::where([ |
126
|
|
|
'string' => $this->string, |
127
|
|
|
'from_lang' => $this->from, |
128
|
|
|
'to_lang' => $this->to, |
129
|
|
|
'translator' => $this->translator, |
130
|
|
|
])->get(); |
131
|
|
|
|
132
|
|
|
if (count($existing) == 0) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
if ($this->debug === true) { |
136
|
|
|
$this->translation = "<font style='color:#00CC00;'>Translation loaded from DB</font>"; |
137
|
|
|
} else { |
138
|
|
|
$this->translation = ($existing[0]->translation); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Function to save translations to DB. |
146
|
|
|
*/ |
147
|
|
|
public function checkSave() |
148
|
|
|
{ |
149
|
|
|
if ($this->save === true) { |
150
|
|
|
$trans = new DB_Translation(); |
151
|
|
|
$trans->string = $this->string; |
|
|
|
|
152
|
|
|
$trans->from_lang = $this->from; |
|
|
|
|
153
|
|
|
$trans->to_lang = $this->to; |
|
|
|
|
154
|
|
|
$trans->translator = $this->translator; |
|
|
|
|
155
|
|
|
$trans->translation = $this->translation; |
|
|
|
|
156
|
|
|
$trans->save(); |
157
|
|
|
|
158
|
|
|
// Checking debug setting to determinate how to output translation |
159
|
|
|
|
160
|
|
|
if ($this->debug === true) { |
161
|
|
|
$this->translation = "<font style='color:#00CC00;'> Translation saved on DB </font>"; |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
|
|
if ($this->debug === true) { |
165
|
|
|
$this->translation = "<font style='color:orange;'> Translation not saved on DB </font>"; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* This fuction is called to know the status of host, and it would set translation if debug is true. |
172
|
|
|
* |
173
|
|
|
* @param string $host |
174
|
|
|
*/ |
175
|
|
|
public function checkHost($host) |
176
|
|
|
{ |
177
|
|
|
$socket = @fsockopen($host, 80, $errno, $errstr, 30); |
178
|
|
|
|
179
|
|
|
if ($socket) { |
180
|
|
|
return true; |
181
|
|
|
fclose($socket); |
|
|
|
|
182
|
|
|
} else { |
183
|
|
|
if ($this->debug === true) { |
184
|
|
|
$this->translation = "<font style='color:red;'>$this->translator host is down! </font>"; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function replaceVars() |
190
|
|
|
{ |
191
|
|
|
foreach ($this->vars as $key => $var) { |
192
|
|
|
$this->translation = str_replace('VAR_'.$key, $var, $this->translation); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* This fuction is called by trans() function of Fadade Laralang |
198
|
|
|
* It would call run() function of this class and returns the translation. |
199
|
|
|
*/ |
200
|
|
|
public function __toString() |
201
|
|
|
{ |
202
|
|
|
if ($this->from == $this->to) { |
203
|
|
|
if ($this->debug) { |
204
|
|
|
return "<font style='color:orange;'>Same in <> out language</font>"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->string; |
208
|
|
|
} elseif (!$this->load || !$this->loadIfExists()) { |
209
|
|
|
$this->main(); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
$this->replaceVars(); |
212
|
|
|
|
213
|
|
|
return $this->translation; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.