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