PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/environment/classes/localization/Localization.class.php

https://gitlab.com/x33n/ProjectPier-Core
PHP | 488 lines | 181 code | 53 blank | 254 comment | 21 complexity | b786fca880fcba7484effbba67463ddb MD5 | raw file
  1. <?php
  2. include_once 'persian.php';
  3. function utf8_strrev($str, $reverse_numbers = true){
  4. $pattern = $reverse_numbers ? '/./us' : '/(\d+)?./us';
  5. preg_match_all($pattern, $str, $ar);
  6. return join('',array_reverse($ar[0]));
  7. }
  8. /**
  9. * Localization class
  10. *
  11. * This class will set up PHP environment to mach locale settings (using
  12. * setlocale() function) and import apropriate set of words from language
  13. * folder. Properties of this class are used by some other system classes
  14. * for outputing data in correct format (for instance DateTimeValueLib).
  15. *
  16. * @version 1.0
  17. * @http://www.projectpier.org/
  18. */
  19. class Localization {
  20. /**
  21. * Path to directory where language settings are
  22. *
  23. * @var string
  24. */
  25. private $language_dir_path = null;
  26. /**
  27. * strftime() function format used for presenting date and time
  28. *
  29. * @var string
  30. */
  31. private $datetime_format = 'M d. Y H:i';
  32. /**
  33. * strftime() function format used for presenting date
  34. *
  35. * @var string
  36. */
  37. private $date_format = 'M d. Y';
  38. /**
  39. * Descriptive date format is string used in date() function that will autput date
  40. * in such a way that it tells as much as it can: with day it is and when it is.
  41. * This one is used for such things as milestones and tasks where you need to see
  42. * as much info about due date as you can from a simple, short string
  43. *
  44. * @var string
  45. */
  46. private $descriptive_date_format = 'l, j F';
  47. /**
  48. * strftime() function format used for presenting time
  49. *
  50. * @var string
  51. */
  52. private $time_format = 'H:i';
  53. /**
  54. * Locale code
  55. *
  56. * @var string
  57. */
  58. private $locale;
  59. /**
  60. * Current locale settings, returned by setlocale() function
  61. *
  62. * @var string
  63. */
  64. private $current_locale;
  65. /**
  66. * Container of langs
  67. *
  68. * @var Container
  69. */
  70. private $langs;
  71. /**
  72. * Construct the Localization
  73. *
  74. * @access public
  75. * @param string $language_dir_path Path to the language dir
  76. * @param string $local
  77. * @return Localization
  78. */
  79. function __construct() {
  80. $this->langs = new Container();
  81. } // __construct
  82. /**
  83. * Return lang by name
  84. *
  85. * @param string $name
  86. * @param mixed $default Default value that will be returned if lang is not found
  87. * @return string
  88. */
  89. function lang($name, $default = null) {
  90. if (is_null($default)) {
  91. $default = "{$this->locale}($name)";
  92. } // if
  93. return $this->langs->get($name, $default);
  94. } // lang
  95. /**
  96. * Load language settings
  97. *
  98. * @access public
  99. * @param string $locale Locale code
  100. * @param string $language_dir Path to directory where we have all
  101. * languages defined
  102. * @return null
  103. * @throws DirDnxError If language dir does not exists
  104. * @throws FileDnxError If language settings file for this local settings
  105. * does not exists in lanuage dir
  106. */
  107. function loadSettings($locale, $languages_dir) {
  108. $this->setLocale($locale);
  109. $this->setLanguageDirPath($languages_dir);
  110. return $this->loadLanguageSettings();
  111. } // loadSettings
  112. /**
  113. * Load language settings
  114. *
  115. * @param void
  116. * @throws DirDnxError If language dir does not exists
  117. * @throws FileDnxError If language settings file for this local settings
  118. * does not exists in lanuage dir
  119. */
  120. private function loadLanguageSettings() {
  121. trace(__FILE__,'loadLanguageSettings()');
  122. // Check dir...
  123. $language_dir = $this->getLanguageDirPath();
  124. if (!is_dir($language_dir)) {
  125. throw new DirDnxError($language_dir);
  126. } // if
  127. $locale = $this->getLocale();
  128. $locale_dir = $language_dir.'/'.$locale;
  129. if (!is_dir($locale_dir)) {
  130. throw new DirDnxError($locale_dir);
  131. } // if
  132. // Get settings file path and include it
  133. $settings_file = $locale_dir.'/'.$locale.'.php';
  134. if (!is_file($settings_file)) {
  135. trace(__FILE__,'loadLanguageSettings()');
  136. throw new FileDnxError($settings_file, "Failed to find language settings file".$settings_file);
  137. }
  138. trace(__FILE__,'loadLanguageSettings():include_once '.$settings_file);
  139. include_once $settings_file;
  140. // Clear langs
  141. $this->langs->clear();
  142. // Load core language files
  143. $this->loadLanguageFiles($locale_dir);
  144. // load every plugin language files
  145. $dirs = get_dirs(PLUGINS_DIR,false);
  146. foreach ($dirs as $plugin_dir) {
  147. if (plugin_active($plugin_dir)) { // plugin_dir is same as plugin name
  148. $locale_dir = PLUGINS_DIR.'/'.$plugin_dir.'/language/' . $locale;
  149. if (is_dir($locale_dir)) {
  150. $this->loadLanguageFiles($locale_dir);
  151. } else {
  152. //$locale_dir = PLUGINS_DIR.'/'.$plugin_dir.'/language/en_us';
  153. if (is_dir($locale_dir)) {
  154. $this->loadLanguageFiles($locale_dir);
  155. } // if
  156. } // if
  157. } // if
  158. } // foreach
  159. // Done!
  160. return true;
  161. } // loadLanguageSettings
  162. /**
  163. * loadLanguageFiles
  164. *
  165. * @access private
  166. * @param String $dir Select files from this dir
  167. * @param String $ext Select files with given extension
  168. * @return string
  169. */
  170. private function loadLanguageFiles($dir, $ext = 'php') {
  171. trace(__FILE__,"loadLanguageFiles($dir, $ext):begin");
  172. $files = get_files($dir, $ext);
  173. // Loop through files and add langs
  174. if (is_array($files)) {
  175. foreach ($files as $file) {
  176. //try {
  177. $langs = include_once $file;
  178. //} catch (Exception $e) {}
  179. if (is_array($langs)) {
  180. $this->langs->append($langs);
  181. } // if
  182. } // foreach
  183. } // if
  184. }
  185. /**
  186. * Return language specific formatted date
  187. *
  188. * @access public
  189. * @param $fmt (see date() in php.net)
  190. * @param $timestamp
  191. * @return string
  192. */
  193. function date_lang($fmt, $timestamp = 0) {
  194. $jd = unixtojd($timestamp);
  195. $pdate = jd_to_persian( $jd );
  196. $date_lang = date($fmt, $timestamp);
  197. if (strpos($fmt, 'a')!==false) {
  198. $a = date('a', $timestamp); // e.g. am or pm
  199. $date_lang = str_replace( $a, lang($a), $date_lang );
  200. }
  201. if (strpos($fmt, 'A')!==false) {
  202. $a = date('A', $timestamp); // e.g. AM or PM
  203. $date_lang = str_replace( $a, lang($a), $date_lang );
  204. }
  205. if (strpos($fmt, 'l')!==false) {
  206. $l = date('l', $timestamp); // e.g. Thursday
  207. $n = date('N', $timestamp); // e.g. 1=Monday, ..., 7=Sunday
  208. $date_lang = str_replace( $l, lang('weekday full ' . $n), $date_lang );
  209. }
  210. if (strpos($fmt, 'D')!==false) {
  211. $d = date('D', $timestamp); // e.g. Thu
  212. $n = date('N', $timestamp); // e.g. 1=Monday, ..., 7=Sunday
  213. $date_lang = str_replace( $d, lang('weekday short ' . $n), $date_lang );
  214. }
  215. if (strpos($fmt, 'M')!==false) {
  216. $m = date('M', $timestamp); // e.g. Feb
  217. $n = date('n', $timestamp); // e.g. 2
  218. $date_lang = str_replace( $m, lang('month short ' . $n), $date_lang );
  219. }
  220. if (strpos($fmt, 'F')!==false) {
  221. $f = date('F', $timestamp); // e.g. February
  222. $n = date('n', $timestamp); // e.g. 2
  223. $date_lang = str_replace( $f, lang('month full ' . $n), $date_lang );
  224. }
  225. if (strpos($fmt, 'S')!==false) {
  226. $s = date('S', $timestamp); // e.g. st, nd, rd or th
  227. $n = 4;
  228. if ($s=='st') $n = 1;
  229. if ($s=='nd') $n = 2;
  230. if ($s=='rd') $n = 3;
  231. $date_lang = str_replace( $s, lang('ordinal ' . $n), $date_lang );
  232. }
  233. //return $date_lang . ' reversed Persian=' . utf8_strrev(FormatPersianSmallDate ( $pdate )) . ' Persian=' . FormatPersianSmallDate ( $pdate );
  234. //return utf8_strrev(FormatPersianDate ( $pdate ));
  235. return $date_lang;
  236. } // date_lang
  237. /**
  238. * Return formatted date
  239. *
  240. * @access public
  241. * @param DateTimeValue $date
  242. * @param float $timezone Timezone offset in hours
  243. * @return string
  244. */
  245. function formatDate(DateTimeValue $date, $timezone = 0, $format = NULL) {
  246. $lang_date_format = $this->langs->get('date format', null);
  247. $date_format = ($format) ? $format : ( ($lang_date_format) ? $lang_date_format : $this->date_format );
  248. return $this->date_lang($date_format, $date->getTimestamp() + ($timezone * 3600));
  249. } // formatDate
  250. /**
  251. * * Descriptive date format is string used in date() function that will autput date
  252. * in such a way that it tells as much as it can: with day it is and when it is.
  253. * This one is used for such things as milestones and tasks where you need to see
  254. * as much info about due date as you can from a simple, short string
  255. *
  256. * @param DateTimeValue $date
  257. * @param float $timezone Timezone offset in hours
  258. * @return string
  259. */
  260. function formatDescriptiveDate(DateTimeValue $date, $timezone = 0, $format = NULL) {
  261. $lang_date_format = $this->langs->get('descriptive date format', null);
  262. $date_format = ($format) ? $format : ( ($lang_date_format) ? $lang_date_format : $this->descriptive_date_format );
  263. return $this->date_lang($date_format, $date->getTimestamp() + ($timezone * 3600));
  264. } // formatDescriptiveDate
  265. /**
  266. * Return formated datetime
  267. *
  268. * @access public
  269. * @param DateTimeValue $date
  270. * @param float $timezone Timezone offset in hours
  271. * @return string
  272. */
  273. function formatDateTime(DateTimeValue $date, $timezone = 0) {
  274. $lang_datetime_format = $this->langs->get('datetime format', null);
  275. $datetime_format = ($lang_datetime_format) ? $lang_datetime_format : $this->datetime_format;
  276. return $this->date_lang($datetime_format, $date->getTimestamp() + ($timezone * 3600));
  277. } // formatDateTime
  278. /**
  279. * Return formated time
  280. *
  281. * @access public
  282. * @param DateTimeValue $date
  283. * @param float $timezone Timezone offset in hours
  284. * @return string
  285. */
  286. function formatTime(DateTimeValue $date, $timezone = 0) {
  287. $lang_time_format = $this->langs->get('time format', null);
  288. $time_format = ($lang_time_format) ? $lang_time_format : $this->time_format;
  289. return $this->date_lang($time_format, $date->getTimestamp() + ($timezone * 3600));
  290. } // formatTime
  291. // -------------------------------------------------------------
  292. // Getters and setters
  293. // -------------------------------------------------------------
  294. /**
  295. * Get language_dir_path
  296. *
  297. * @access public
  298. * @param null
  299. * @return string
  300. */
  301. function getLanguageDirPath() {
  302. return $this->language_dir_path;
  303. } // getLanguageDirPath
  304. /**
  305. * Set language_dir_path value
  306. *
  307. * @access public
  308. * @param string $value
  309. * @return null
  310. */
  311. function setLanguageDirPath($value) {
  312. $this->language_dir_path = $value;
  313. } // setLanguageDirPath
  314. /**
  315. * Get datetime format
  316. *
  317. * @access public
  318. * @param null
  319. * @return string
  320. */
  321. function getDateTimeFormat() {
  322. return $this->datetime_format;
  323. } // getDateTimeFormat
  324. /**
  325. * Set datetime foramt value
  326. *
  327. * @access public
  328. * @param string $value
  329. * @return null
  330. */
  331. function setDateTimeFormat($value) {
  332. $this->datetime_format = (string) $value;
  333. } // setDateTimeFormat
  334. /**
  335. * Get date format
  336. *
  337. * @access public
  338. * @param null
  339. * @return string
  340. */
  341. function getDateFormat() {
  342. return $this->date_format;
  343. } // getDateFormat
  344. /**
  345. * Set date format value
  346. *
  347. * @access public
  348. * @param string $value
  349. * @return null
  350. */
  351. function setDateFormat($value) {
  352. $this->date_format = (string) $value;
  353. } // setDateFormat
  354. /**
  355. * Get time format
  356. *
  357. * @access public
  358. * @param null
  359. * @return string
  360. */
  361. function getTimeFormat() {
  362. return $this->time_format;
  363. } // getTimeFormat
  364. /**
  365. * Set time format value
  366. *
  367. * @access public
  368. * @param string $value
  369. * @return null
  370. */
  371. function setTimeFormat($value) {
  372. $this->time_format = (string) $value;
  373. } // setTimeFormat
  374. /**
  375. * Get locale
  376. *
  377. * @access public
  378. * @param null
  379. * @return string
  380. */
  381. function getLocale() {
  382. return $this->locale;
  383. } // getLocale
  384. /**
  385. * Set locale value
  386. *
  387. * @access public
  388. * @param string $value
  389. * @return boolean
  390. */
  391. function setLocale($value) {
  392. $this->locale = $value;
  393. } // setLocale
  394. /**
  395. * Return current locale settings
  396. *
  397. * @access public
  398. * @param void
  399. * @return string
  400. */
  401. //function getCurrentLocale() {
  402. // if (trim($this->current_locale)) {
  403. // return $this->current_locale;
  404. // } else {
  405. // return setlocale(LC_ALL, 0);
  406. // } // if
  407. //} // getCurrentLocale
  408. /**
  409. * Interface to langs container
  410. *
  411. * @access public
  412. * @param void
  413. * @return Container
  414. */
  415. function langs() {
  416. return $this->langs;
  417. } // langs
  418. /**
  419. * Return localization instance
  420. *
  421. * @access public
  422. * @param string $locale Localization code
  423. * @return Localization
  424. */
  425. static function instance() {
  426. static $instance;
  427. // Prepare instance
  428. if (!($instance instanceof Localization)) {
  429. $instance = new Localization();
  430. } // if
  431. // Done...
  432. return $instance;
  433. } // instance
  434. } // Localization
  435. ?>