1 | <?php |
||||
2 | /* |
||||
3 | * i18n class called Lezer, shorthand L |
||||
4 | * honnors Ludwik *Lejzer* Zamenhof (Polish: Ludwik Łazarz Zamenhof; 15 December [O.S. 3 December] 1859 – 14 April [O.S. 1 April] 1917), |
||||
5 | * a medical doctor, inventor, and writer; most widely known for creating Esperanto. |
||||
6 | * |
||||
7 | * also Lezer is dutch for Reader, and it sounds like LASER, which is kinda cool. |
||||
8 | */ |
||||
9 | namespace HexMakina\Lezer; |
||||
10 | |||||
11 | use \HexMakina\LocalFS\FileSystem; |
||||
12 | use \HexMakina\Tempus\{Dato,DatoTempo,Tempo}; |
||||
13 | |||||
14 | class Lezer extends \i18n{ |
||||
15 | |||||
16 | private $detected_language_files = []; |
||||
17 | private $detected_language_env = []; |
||||
18 | |||||
19 | // protected $basePath = 'locale'; |
||||
20 | // protected $filePath = 'locale/{LANGUAGE}/user_interface.ini'; // uses gettext hierarchy |
||||
21 | // protected $cachePath = 'locale/cache/'; |
||||
22 | // protected $fallbackLang = 'fra'; // uses ISO-639-3 |
||||
23 | protected $currentLang = null; |
||||
24 | |||||
25 | public function one_language() |
||||
26 | { |
||||
27 | $this->detect_language_files(); |
||||
28 | $this->detect_language_env(); |
||||
29 | $the_one_language = current(array_intersect($this->detect_language_files(), $this->detect_language_env())); |
||||
30 | |||||
31 | if($the_one_language) |
||||
32 | $this->setForcedLang($the_one_language); |
||||
33 | |||||
34 | return $the_one_language; |
||||
35 | } |
||||
36 | |||||
37 | public function detect_language_files() |
||||
38 | { |
||||
39 | $files = FileSystem::preg_scandir(dirname($this->filePath), '/.json$/'); |
||||
40 | if(empty($files)) |
||||
41 | return []; |
||||
42 | |||||
43 | $files = implode('',$files); |
||||
44 | $res = preg_match_all('/([a-z]{3})\.json/', $files, $m); |
||||
45 | if($res) // false or 0 is none found |
||||
46 | $this->detected_language_files = $m[1]; |
||||
47 | return $this->detected_language_files; |
||||
48 | } |
||||
49 | |||||
50 | public function compileFunction() |
||||
51 | { |
||||
52 | return '' |
||||
53 | . "function ".$this->prefix .'($string, $args=NULL) {'."\n" |
||||
54 | . ' if (!defined("'.$this->prefix.'::".$string))' |
||||
55 | . ' return $string;' |
||||
56 | . ' $return = constant("'.$this->prefix.'::".$string);'."\n" |
||||
57 | . ' return $args ? vsprintf($return,$args) : $return;' |
||||
58 | . "\n}"; |
||||
59 | } |
||||
60 | /** |
||||
61 | * getUserLangs() |
||||
62 | * Returns the user languages |
||||
63 | * Normally it returns an array like this: |
||||
64 | * 1. Forced language |
||||
65 | * 2. Language in $_GET['lang'] |
||||
66 | * 3. Language in $_SESSION['lang'] |
||||
67 | * 4. COOKIE |
||||
68 | * 5. Fallback language |
||||
69 | * Note: duplicate values are deleted. |
||||
70 | * |
||||
71 | * @return array with the user languages sorted by priority. |
||||
72 | */ |
||||
73 | public function detect_language_env() { |
||||
74 | $userLangs = array(); |
||||
75 | |||||
76 | // 1. forced language |
||||
77 | if ($this->forcedLang != NULL) { |
||||
78 | $userLangs['forced'] = $this->forcedLang; |
||||
79 | } |
||||
80 | |||||
81 | // 2. GET parameter 'lang' |
||||
82 | if (isset($_GET['lang']) && is_string($_GET['lang'])) { |
||||
83 | $userLangs['get'] = $_GET['lang']; |
||||
84 | } |
||||
85 | |||||
86 | // 3. SESSION parameter 'lang' |
||||
87 | if (isset($_SESSION['lang']) && is_string($_SESSION['lang'])) { |
||||
88 | $userLangs['session'] = $_SESSION['lang']; |
||||
89 | } |
||||
90 | |||||
91 | // 4. COOKIES |
||||
92 | if (isset($_COOKIE['lang']) && is_string($_COOKIE['lang'])) { |
||||
93 | $userLangs['cookie'] = $_COOKIE['lang']; |
||||
94 | } |
||||
95 | |||||
96 | // Lowest priority: fallback |
||||
97 | $userLangs['fallback'] = $this->fallbackLang; |
||||
98 | // remove duplicate elements |
||||
99 | $userLangs = array_unique($userLangs); |
||||
100 | |||||
101 | // remove illegal userLangs |
||||
102 | foreach ($userLangs as $key => $value) { |
||||
103 | // only allow a-z, A-Z and 0-9 and _ and - |
||||
104 | if (preg_match('/^[a-zA-Z0-9_-]*$/', $value) === 1) |
||||
105 | $this->detected_language_env[$key] = $value; |
||||
106 | } |
||||
107 | |||||
108 | return $this->detected_language_env; |
||||
109 | } |
||||
110 | |||||
111 | |||||
112 | public static function model_type_to_label($form_model) |
||||
113 | { |
||||
114 | return L(sprintf('MODEL_%s_INSTANCE',get_class($form_model)::model_type())); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
115 | } |
||||
116 | public static function field_name_to_label($form_model, $field_name) |
||||
117 | { |
||||
118 | return L(sprintf('MODEL_%s_FIELD_%s',(get_class($form_model))::model_type(), $field_name)); |
||||
0 ignored issues
–
show
The function
L was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
119 | } |
||||
120 | |||||
121 | // options['decimals'] = int |
||||
122 | // options['abbrev'] = mixed: key needs to be set |
||||
123 | public static function when($event, $options=[]) |
||||
124 | { |
||||
125 | try { |
||||
126 | $amount_of_days = DatoTempo::days_diff(new \DateTime($event), new \DateTime()); |
||||
127 | |||||
128 | } catch (\Exception $e) { |
||||
129 | return __FUNCTION__.': error'; |
||||
130 | } |
||||
131 | |||||
132 | if($amount_of_days === -1) |
||||
133 | return L('DATETIME_RANGE_YESTERDAY'); |
||||
0 ignored issues
–
show
The function
L was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
134 | elseif($amount_of_days === 0) |
||||
135 | return L('DATETIME_RANGE_TODAY'); |
||||
136 | elseif($amount_of_days === 1) |
||||
137 | return L('DATETIME_RANGE_TOMORROW'); |
||||
138 | |||||
139 | |||||
140 | $datetime_parts = [ |
||||
141 | 'y' => 'DATETIME_UNIT_YEAR', |
||||
142 | 'm' => 'DATETIME_UNIT_MONTH', |
||||
143 | 'w' => 'DATETIME_UNIT_WEEK', |
||||
144 | 'd' => 'DATETIME_UNIT_DAY', |
||||
145 | 'h' => 'DATETIME_UNIT_HOUR', |
||||
146 | 'i' => 'DATETIME_UNIT_MINUTE', |
||||
147 | 's' => 'DATETIME_UNIT_SECOND' |
||||
148 | ]; |
||||
149 | |||||
150 | $date_diff = DatoTempo::days_diff_in_parts(abs($amount_of_days)); |
||||
0 ignored issues
–
show
It seems like
$amount_of_days can also be of type false ; however, parameter $num of abs() does only seem to accept double|integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
151 | $ordering = []; |
||||
152 | foreach($datetime_parts as $unit => $label) |
||||
153 | { |
||||
154 | if(!isset($date_diff[$unit])) |
||||
155 | continue; |
||||
156 | |||||
157 | $qty = (int)$date_diff[$unit]; |
||||
158 | |||||
159 | if($qty === 0) |
||||
160 | continue; |
||||
161 | |||||
162 | if(isset($options['abbrev'])) |
||||
163 | $label.= '_ABBREV'; |
||||
164 | elseif($qty > 1) |
||||
165 | $label.= '_PLURAL'; |
||||
166 | |||||
167 | $ordering[$unit] = $qty.' '.L($label).'.'; |
||||
168 | } |
||||
169 | $ret = (isset($amount_of_days) && $amount_of_days >= 0) ? L('DATETIME_RANGE_PREFIX_FUTURE') : L('DATETIME_RANGE_PREFIX_PAST'); |
||||
170 | $ret.= ' '.implode(' & ', array_slice($ordering, 0,2)); |
||||
171 | |||||
172 | return $ret; |
||||
173 | } |
||||
174 | |||||
175 | public static function time($time_string, $short=true) |
||||
176 | { |
||||
177 | if ($short === true) |
||||
178 | $time_string = substr($time_string, 0, 5); |
||||
179 | return $time_string; |
||||
180 | } |
||||
181 | |||||
182 | public static function human_date($date_string, $short=true) |
||||
183 | { |
||||
184 | if($date_string === '0000-00-00' || empty($date_string)) |
||||
185 | return L('MODEL_common_VALUE_EMPTY'); |
||||
0 ignored issues
–
show
The function
L was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
186 | |||||
187 | if(preg_match('/^[0-9]{4}$/', $date_string) === 1) |
||||
188 | return intval($date_string); |
||||
189 | |||||
190 | list($year, $month, $day) = explode('-',$date_string); |
||||
191 | |||||
192 | $ret = intval($day).' '.L("DATETIME_CALENDAR_MONTH_$month"); |
||||
193 | |||||
194 | if($short === true && Dato::format(null, 'Y') === $year) |
||||
195 | return $ret; |
||||
196 | else |
||||
197 | return "$ret $year"; |
||||
198 | } |
||||
199 | |||||
200 | public static function human_month($date_string) |
||||
201 | { |
||||
202 | return L('DATETIME_CALENDAR_MONTH_'.Dato::format($date_string, 'm')); |
||||
0 ignored issues
–
show
The function
L was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
203 | } |
||||
204 | |||||
205 | public static function human_day($date_string) |
||||
206 | { |
||||
207 | return L('DATETIME_CALENDAR_DAY_'.Dato::format($date_string, 'N')); |
||||
0 ignored issues
–
show
The function
L was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
208 | } |
||||
209 | |||||
210 | public static function human_seconds($seconds) |
||||
211 | { |
||||
212 | $hours = floor($seconds / 3600); |
||||
213 | $mins = floor(($seconds-$hours*3600) / 60); |
||||
214 | $secs = floor($seconds % 60); |
||||
215 | |||||
216 | $hours_format ='%dh %dm %ds'; |
||||
217 | return sprintf($hours_format, $hours, $mins, $secs); |
||||
218 | } |
||||
219 | |||||
220 | |||||
221 | } |
||||
222 | |||||
223 | ?> |
||||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||||
224 |