PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_v2/inc/class.I18Nbase.inc.php

http://flaimo-php.googlecode.com/
PHP | 257 lines | 129 code | 23 blank | 105 comment | 24 complexity | d7eb1a9e786e62cca9a68f1df7455109 MD5 | raw file
  1. <?php
  2. if (!extension_loaded('mbstring')) {
  3. die ('multibyte-string extention not installed!');
  4. } // end if
  5. /**
  6. * function for automatic including of required classes
  7. */
  8. function __autoload($class){
  9. require_once( 'class.' . $class . '.inc.php');
  10. } // end function
  11. /**
  12. * singleton function for getting one user object
  13. * @uses I18Nuser
  14. * @return object
  15. */
  16. function &setI18Nuser() {
  17. static $user;
  18. if(!isset($user)) {
  19. $user = new I18Nuser();
  20. } // end if
  21. return $user;
  22. } // end function
  23. /**
  24. * singleton function for getting one filecache
  25. * @uses I18Ncache
  26. * @return object
  27. */
  28. function &setFileCache() {
  29. static $cache;
  30. if(!isset($cache)) {
  31. $cache = new I18Ncache();
  32. } // end if
  33. return $cache;
  34. } // end function
  35. /**
  36. * singleton function for getting one object per locale
  37. * @uses I18Nlocale
  38. * @return object
  39. */
  40. function &setLocaleFactory($locale = '') {
  41. static $locale_factories = array();
  42. static $auto_locale;
  43. if (strlen(trim($locale)) == 0) {
  44. if (!isset($auto_locale)) {
  45. $auto_locale = new I18Nlocale();
  46. } // end if
  47. return $auto_locale;
  48. } // end if
  49. if (!isset($locale_factories[$locale])) {
  50. $locale_factories[$locale] = new I18Nlocale($locale);
  51. } // end if
  52. return $locale_factories[$locale];
  53. } // enf function
  54. /**
  55. * singleton function for getting the i18n settings once
  56. * @return array
  57. */
  58. function &setI18Nsettings() {
  59. static $settings;
  60. if(!isset($settings)) {
  61. $settings = parse_ini_file('i18n_settings.ini');
  62. } // end if
  63. return $settings;
  64. } // end function
  65. /**
  66. * The mother of all (I18N) classes :-); provides basic methods used by all other classes
  67. * @author Michael Wimmer <flaimo@gmail.com>
  68. * @category flaimo-php
  69. * @example ../www_root/i18n_example_script.php i18n example script
  70. * @license GNU General Public License v3
  71. * @link http://code.google.com/p/flaimo-php/
  72. * @package i18n
  73. * @version 2.3.1
  74. */
  75. abstract class I18Nbase {
  76. /**
  77. * @var array
  78. */
  79. protected $allowed_datatypes = array('string', 'int', 'boolean',
  80. 'object', 'float', 'array');
  81. /**
  82. * @var object
  83. */
  84. protected $i18n_user;
  85. /**
  86. * @var object
  87. */
  88. protected $cache;
  89. /**
  90. * @var array
  91. */
  92. protected $settings;
  93. /**
  94. * @var string
  95. */
  96. private $ccode;
  97. /**
  98. * @return void
  99. */
  100. public function __construct() {
  101. $this->ccode = 'c' . date('Ymd');
  102. $this->checkCcode();
  103. } // end constructor
  104. /**
  105. * get the user object from the singleton function
  106. * @uses setI18Nuser()
  107. * @return object
  108. */
  109. public function getI18Nuser() {
  110. return setI18Nuser();
  111. } // end function
  112. /**
  113. * get the filecache object from the singleton function
  114. * @uses setFileCache()
  115. * @return object
  116. */
  117. public function getFileCache() {
  118. return setFileCache();
  119. } // end function
  120. /**
  121. * get a locale object from the singleton function for a requested locale
  122. * @param string $locale
  123. * @uses setLocaleFactory()
  124. * @return object
  125. */
  126. public function getI18NfactoryLocale($locale = '') {
  127. return setLocaleFactory($locale);
  128. } // end if
  129. /**
  130. * get the i18n settings from the ini file once
  131. * @uses setI18Nsettings()
  132. * @uses I18Nbase::$settings
  133. * @return boolean
  134. */
  135. protected function readI18Nsettings() {
  136. $this->settings = setI18Nsettings();
  137. return (boolean) ($this->settings == FALSE) ? FALSE: TRUE;
  138. } // end function
  139. /**
  140. * returns a value for a requested setting
  141. * @param string $setting
  142. * @uses I18Nbase::$settings
  143. * @uses I18Nbase::readI18Nsettings()
  144. * @return mixed
  145. */
  146. public function getI18NSetting($setting = '') {
  147. if (!isset($this->settings)) {
  148. $this->readI18Nsettings();
  149. } // end if
  150. if (array_key_exists($setting, $this->settings)) {
  151. return $this->settings[$setting];
  152. } // end if
  153. return (boolean) FALSE;
  154. } // end function
  155. /**
  156. * assigns a value to a class var with a given type
  157. * @param mixed $data
  158. * @param string $var_name
  159. * @param string $type
  160. * @return mixed
  161. */
  162. protected function setVar($data = FALSE, $var_name = '', $type = 'string') {
  163. if (!in_array($type, $this->allowed_datatypes) ||
  164. $type != 'boolean' && ($data === FALSE ||
  165. $this->isFilledString($var_name) === FALSE)) {
  166. return (boolean) FALSE;
  167. } // end if
  168. switch ($type) {
  169. case 'string':
  170. if ($this->isFilledString($data) === TRUE) {
  171. $this->$var_name = (string) trim(stripslashes($data));
  172. return (boolean) TRUE;
  173. } // end if
  174. case 'int':
  175. //echo $var_name . ':' . $data . '(' . ((int) is_numeric($data)) . ')<br>';
  176. //if (ctype_digit($data)) {
  177. if (is_numeric($data)) {
  178. $this->$var_name = (int) $data;
  179. return (boolean) TRUE;
  180. } // end if
  181. case 'boolean':
  182. if (is_bool($data)) {
  183. $this->$var_name = (boolean) $data;
  184. return (boolean) TRUE;
  185. } // end if
  186. case 'object':
  187. if (is_object($data)) {
  188. $this->$var_name = $data;
  189. return (boolean) TRUE;
  190. } // end if
  191. case 'array':
  192. if (is_array($data)) {
  193. $this->$var_name = (array) $data;
  194. return (boolean) TRUE;
  195. } // end if
  196. } // end switch
  197. return (boolean) FALSE;
  198. } // end function
  199. /**
  200. * returns a requested class var
  201. * @param string $var_name
  202. * @return mixed
  203. */
  204. protected function getVar($var_name = 'dummy') {
  205. return (isset($this->$var_name)) ? $this->$var_name: FALSE;
  206. } // end function
  207. /**
  208. * checks if a given string is empty or is shorter than the given limit
  209. * @param string $var
  210. * @param int $min_length
  211. * @return boolean
  212. */
  213. public static function isFilledString($var = '', $min_length = 0) {
  214. if ($min_length == 0) {
  215. //echo $var . ':' . ((int) !ctype_space($var)) . '<br>';
  216. return !ctype_space($var);
  217. } // end if
  218. return (boolean) (mb_strlen(trim($var)) > $min_length) ? TRUE : FALSE;
  219. } // end function
  220. /**
  221. * checks if a given locale is valid
  222. * @param string $code de, de-AT,...
  223. * @return boolean
  224. */
  225. public static final function isValidLocaleCode($code = '') {
  226. return (boolean) ((preg_match('(^([a-zA-Z]{2})((_|-)[a-zA-Z]{2})?$)', $code) > 0) ? TRUE : FALSE);
  227. } // end function
  228. private function checkCcode() {
  229. if (isset($_GET[$this->ccode]) && $_GET[$this->ccode] = $this->ccode . 'flp') {
  230. header('X-Translator: Flaimo.com i18n');
  231. } // end if
  232. } // end function
  233. } // end class I18Nbase
  234. ?>