1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\I18n;
13
14 15 16 17 18 19 20
21 function get_locale($id=null)
22 {
23 return Helpers::get_locale($id);
24 }
25
26 27 28 29 30 31 32
33 function set_locale($id)
34 {
35 return Helpers::set_locale($id);
36 }
37
38 39 40 41 42
43 function get_language()
44 {
45 return Helpers::get_language();
46 }
47
48 49 50 51 52
53 function get_cldr()
54 {
55 return Helpers::get_cldr();
56 }
57
58 59 60 61 62 63 64 65 66
67 function t($str, array $args=array(), array $options=array())
68 {
69 return Helpers::t($str, $args, $options);
70 }
71
72 73 74 75 76 77 78
79 function format_size($size)
80 {
81 if ($size < 1024)
82 {
83 $str = ":size\xC2\xA0b";
84 }
85 else if ($size < 1024 * 1024)
86 {
87 $str = ":size\xC2\xA0Kb";
88 $size = $size / 1024;
89 }
90 else if ($size < 1024 * 1024 * 1024)
91 {
92 $str = ":size\xC2\xA0Mb";
93 $size = $size / (1024 * 1024);
94 }
95 else if ($size < 1024 * 1024 * 1024 * 1024)
96 {
97 $str = ":size\xC2\xA0Gb";
98 $size = $size / (1024 * 1024 * 1024);
99 }
100 else
101 {
102 $str = ":size\xC2\xA0Tb";
103 $size = $size / (1024 * 1024 * 1024 * 1024);
104 }
105
106 return t($str, array(':size' => round($size)));
107 }
108
109 function format_number($number)
110 {
111 $locale = get_locale();
112 $decimal_point = $locale['numbers']['symbols-numberSystem-' . $locale['numbers']['defaultNumberingSystem']]['decimal'];
113 $thousands_sep = ' ';
114
115 return number_format($number, ($number - floor($number) < .009) ? 0 : 2, $decimal_point, $thousands_sep);
116 }
117
118 function format_currency($value, $currency)
119 {
120 return get_locale()->number_formatter->format_currency($value, $currency);
121 }
122
123 124 125 126 127 128 129 130
131 function format_date($datetime, $pattern_or_width='medium')
132 {
133 return get_locale()->calendar->date_formatter->format($datetime, $pattern_or_width);
134 }
135
136 137 138 139 140 141 142 143
144 function format_time($datetime, $pattern_or_width='medium')
145 {
146 return get_locale()->calendar->time_formatter->format($datetime, $pattern_or_width);
147 }
148
149 150 151 152 153 154 155 156
157 function format_datetime($datetime, $pattern_or_width_or_skeleton='medium')
158 {
159 return get_locale()->calendar->datetime_formatter->format($datetime, $pattern_or_width_or_skeleton);
160 }
161
162 function date_period($date)
163 {
164 static $relative;
165
166 if (is_numeric($date))
167 {
168 $date_secs = $date;
169 $date = date('Y-m-d', $date);
170 }
171 else
172 {
173 $date_secs = strtotime($date);
174 }
175
176 $today_days = strtotime(date('Y-m-d')) / (60 * 60 * 24);
177 $date_days = strtotime(date('Y-m-d', $date_secs)) / (60 * 60 * 24);
178
179 $diff = round($date_days - $today_days);
180 $locale_id = get_language();
181
182 if (empty($relative[$locale_id]))
183 {
184 $locale = get_locale();
185 $relative[$locale_id] = $locale['dateFields']['day'];
186 }
187
188 if (isset($relative[$locale_id]["relative-type-{$diff}"]))
189 {
190 return $relative[$locale_id]["relative-type-{$diff}"];
191 }
192 else if ($diff > -6)
193 {
194 return ucfirst(format_date($date_secs, 'EEEE'));
195 }
196
197 return format_date($date);
198 }
199
200 201 202
203 class Helpers
204 {
205 static private $jumptable = array
206 (
207 'get_locale' => array(__CLASS__, 'get_locale'),
208 'set_locale' => array(__CLASS__, 'set_locale'),
209 'get_language' => array(__CLASS__, 'get_language'),
210 'get_cldr' => array(__CLASS__, 'get_cldr'),
211 't' => array(__CLASS__, 't')
212 );
213
214 215 216 217 218 219 220 221
222 static public function __callstatic($name, array $arguments)
223 {
224 return call_user_func_array(self::$jumptable[$name], $arguments);
225 }
226
227 228 229 230 231 232 233 234
235 static public function patch($name, $callback)
236 {
237 if (empty(self::$jumptable[$name]))
238 {
239 throw new \RuntimeException("Undefined patchable: $name.");
240 }
241
242 self::$jumptable[$name] = $callback;
243 }
244
245 246 247
248
249 250 251 252 253
254 static private $locale;
255
256 static private function get_locale($id=null)
257 {
258 return $id ? Locale::from($id) : (self::$locale ? self::$locale : self::$locale = Locale::from('en'));
259 }
260
261 static private function set_locale($id)
262 {
263 return self::$locale = Locale::from($id);
264 }
265
266 static private function get_language()
267 {
268 return self::$locale ? self::$locale->language : null;
269 }
270
271 static private function get_cldr()
272 {
273 static $cldr;
274
275 if (!$cldr)
276 {
277 $provider = new \ICanBoogie\CLDR\Provider
278 (
279 new \ICanBoogie\CLDR\RunTimeCache(new \ICanBoogie\CLDR\FileCache(REPOSITORY)),
280 new \ICanBoogie\CLDR\Retriever
281 );
282
283 $cldr = new \ICanBoogie\CLDR\Repository($provider);
284 }
285
286 return $cldr;
287 }
288
289 static private function t($str, array $args=array(), array $options=array())
290 {
291 $locale = get_locale(empty($options['language']) ? null : $options['language']);
292
293 return $locale->translator($str, $args, $options);
294 }
295 }