1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Locale;
13:
14: use Patchwork\Utf8;
15: use Xoops\Core\Locale\Punic\Calendar;
16: use Punic\Misc;
17: use \Xoops\Core\Locale\Time;
18:
19: /**
20: * XOOPS localization abstract
21: *
22: * @category Xoops\Locale
23: * @package Xoops\Locale\Abstract
24: * @author Taiwen Jiang <phppp@users.sourceforge.net>
25: * @copyright 2000-2015 XOOPS Project (http://xoops.org)
26: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27: * @link http://xoops.org
28: */
29: abstract class AbstractLocale
30: {
31: /**
32: * isMultiByte - does locale depend on multi-byte characters?
33: *
34: * @return bool true always true with UTF-8
35: *
36: * @deprecated since 2.6.0 -- UTF-8 is always used
37: */
38: public static function isMultiByte()
39: {
40: return true;
41: }
42:
43: /**
44: * isRtl - is text order right to left?
45: *
46: * @return bool true if right to left
47: */
48: public static function isRtl()
49: {
50: return ('right-to-left' === Misc::getCharacterOrder());
51: }
52:
53: /**
54: * todo, do not forget to set this on locale load
55: */
56: public static function setLocale()
57: {
58: return setlocale(LC_ALL, self::getLocale());
59: }
60:
61: /**
62: * getCharset - return current character set, always UTF-8
63: *
64: * @return string character set
65: */
66: public static function getCharset()
67: {
68: return 'UTF-8';
69: }
70:
71: /**
72: * getLocale - return the current locale
73: *
74: * @return string
75: */
76: public static function getLocale()
77: {
78: return \Xoops\Locale::getCurrent();
79: }
80:
81: /**
82: * getLangCode - return language code for the current locale (locale with '-' separator)
83: *
84: * @return string
85: */
86: public static function getLangCode()
87: {
88: return \Xoops\Locale::normalizeLocale(\Xoops\Locale::getCurrent(), '-', false);
89: }
90:
91: /**
92: * getLegacyLanguage - return legacy language code for the current locale
93: * @return string
94: */
95: public static function getLegacyLanguage()
96: {
97: $legacyLanguages = \Xoops\Core\Locale\LegacyCodes::getLegacyName(\Xoops\Locale::getCurrent());
98: return reset($legacyLanguages);
99: }
100:
101: /**
102: * @return string
103: */
104: public static function getTimezone()
105: {
106: return \Xoops\Locale::getTimeZone()->getName();
107: }
108:
109: /**
110: * The generic css fonts are:
111: * - cursive
112: * - fantasy
113: * - monospace
114: * - sans-serif
115: * - serif
116: *
117: * @return string[]
118: */
119: public static function getFonts()
120: {
121: return array(
122: 'Arial',
123: 'Courier',
124: 'Georgia',
125: 'Helvetica',
126: 'Impact',
127: 'Verdana',
128: 'Haettenschweiler'
129: );
130: }
131:
132: /**
133: * The css should adjust based on the
134: * html:lang(ja) {
135: * font-size: 150%;
136: * }
137: * Then classes can be relative to that base em
138: * CJK fonts may need to be shown in a larger size due to complex glyphs
139: *
140: * @return array
141: */
142: public static function getFontSizes()
143: {
144: return array(
145: 'xx-small' => 'xx-Small',
146: 'x-small' => 'x-Small',
147: 'small' => 'Small',
148: 'medium' => 'Medium',
149: 'large' => 'Large',
150: 'x-large' => 'x-Large',
151: 'xx-large' => 'xx-Large'
152: );
153: }
154:
155: /**
156: * @return string[]
157: */
158: public static function getAdminRssUrls()
159: {
160: return array('http://www.xoops.org/backend.php');
161: }
162:
163: /**
164: * @param mixed $str
165: * @param integer $start
166: * @param integer $length
167: * @param string $ellipsis
168: *
169: * @return string
170: */
171: public static function substr($str, $start, $length, $ellipsis = '…')
172: {
173: $str2 = mb_strcut($str, $start, $length - strlen($ellipsis));
174: return $str2 . (mb_strlen($str)-$start != mb_strlen($str2) ? $ellipsis : '');
175: }
176:
177: /**
178: * filter to UTF-8, converts invalid $text as CP1252 and forces NFC normalization
179: *
180: * @param mixed $text
181: *
182: * @return string
183: */
184: public static function utf8_encode($text)
185: {
186: return Utf8::filter($text);
187: }
188:
189: /**
190: * @param mixed $text
191: * @param string $to
192: * @param string $from
193: *
194: * @return string
195: *
196: * @deprecated
197: */
198: public static function convert_encoding($text, $to = 'utf-8', $from = '')
199: {
200: return $text;
201: }
202:
203: /**
204: * XoopsLocalAbstract::trim()
205: *
206: * @param mixed $text
207: *
208: * @return string
209: */
210: public static function trim($text)
211: {
212: $ret = Utf8::trim($text);
213:
214: return $ret;
215: }
216:
217: /**
218: * Function to display formatted times in user timezone
219: *
220: * @param mixed $time
221: * @param string $format Format codes ()
222: * 's' or 'short' - short;
223: * 'm' or 'medium' - medium;
224: * 'l' or 'long' - long;
225: * 'c' or 'custom' - format determined according to interval to present;
226: * 'e' or 'elapse' - Elapsed;
227: * 'mysql' - Y-m-d H:i:s;
228: * 'rss'
229: *
230: * @return string
231: */
232: public static function formatTimestamp($time, $format = 'l')
233: {
234: $workingTime = Time::cleanTime($time);
235:
236: switch (strtolower($format)) {
237: case 'short':
238: case 's':
239: return Time::formatDateTime($workingTime, 'short');
240:
241: case 'medium':
242: case 'm':
243: return Time::formatDateTime($workingTime, 'medium');
244:
245: case 'long':
246: case 'l':
247: return Time::formatDateTime($workingTime, 'long');
248:
249: case 'full':
250: case 'f':
251: return Time::formatDateTime($workingTime, 'full');
252:
253: case 'custom':
254: case 'c':
255: $specialName = Calendar::getDateRelativeName($workingTime, true);
256: if ($specialName != '') {
257: return $specialName;
258: }
259: // no break - fall through
260: case 'elapse':
261: case 'e':
262: return Time::describeRelativeInterval($workingTime);
263:
264: case 'short-date':
265: return Time::formatDate($workingTime, 'short');
266:
267: case 'short-time':
268: return Time::formatTime($workingTime, 'short');
269:
270: case 'medium-date':
271: return Time::formatDate($workingTime, 'medium');
272:
273: case 'medium-time':
274: return Time::formatTime($workingTime, 'medium');
275:
276: case 'long-date':
277: return Time::formatDate($workingTime, 'long');
278:
279: case 'long-time':
280: return Time::formatTime($workingTime, 'long');
281:
282: case 'full-date':
283: return Time::formatDate($workingTime, 'full');
284:
285: case 'full-time':
286: return Time::formatTime($workingTime, 'full');
287:
288: case 'rss':
289: $workingTime->setTimezone(new \DateTimeZone('UTC'));
290: return $workingTime->format($workingTime::RSS);
291:
292: case 'mysql':
293: $workingTime->setTimezone(new \DateTimeZone('UTC'));
294: return $workingTime->format('Y-m-d H:i:s');
295:
296: default:
297: if ($format != '') {
298: return $workingTime->format($format);
299: }
300: return Time::formatDateTime($workingTime, 'long');
301: break;
302: }
303: }
304:
305: /**
306: * @param int $number
307: *
308: * @return string
309: */
310: public static function number_format($number)
311: {
312: return number_format($number, 2, '.', ',');
313: }
314:
315: /**
316: * @param string $format
317: * @param string $number
318: *
319: * @return string
320: */
321: public static function money_format($format, $number)
322: {
323: if (function_exists('money_format')) {
324: $result = money_format($format, $number);
325: } else {
326: $result = sprintf('%01.2f', $number);
327: }
328:
329: return $result;
330: }
331:
332: /**
333: * Sort array values according to current locale rules, maintaining index association
334: *
335: * @param array $array to sort
336: * @return void
337: */
338: public static function asort(&$array)
339: {
340: //if (class_exists('\Collator')) {
341: // $col = new \Collator(self::getLocale());
342: // $col->asort($array);
343: //} else {
344: // asort($array);
345: //}
346: uasort($array, '\Patchwork\Utf8::strcasecmp');
347: }
348: }
349: