Source for file Locale.php

Documentation is available at Locale.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.system.ConfigBox');
  25.  
  26.  
  27. /**
  28. * Locale Management
  29. *
  30. * @todo At the moment is used only for the language, in the future must be
  31. * implemented and extended to all other locale parameters
  32. *
  33. *
  34. * @package Sylar
  35. * @version 1.0
  36. * @since 07/apr/08
  37. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  38. * @copyright Sylar Development Team
  39. */
  40. class Sylar_Locale{
  41. // Locale Configuration object
  42. private $oConfig;
  43. // File system root of locale for Application files
  44. private $sApplicationLocaleRootFs;
  45. function __construct(Sylar_LocaleConfiguration $oConfig=null){
  46. if(!is_null($oConfig)){
  47. $this->setConfig($oConfig);
  48. }
  49. }
  50. function __destruct(){
  51. // nothing to do at the moment
  52. }
  53.  
  54. // Setter and Getter
  55. //__________________________________________________________________________
  56. /**
  57. * @return void
  58. * @param Sylar_LocaleConfiguration $oConfig
  59. */
  60. public function setConfig(Sylar_LocaleConfiguration $oConfig){
  61. $this->oConfig = $oConfig;
  62. }
  63. /**
  64. * @return Sylar_LocaleConfiguration
  65. */
  66. public function getConfig(){
  67. return $this->oConfig;
  68. }
  69. /**
  70. * @return void
  71. * @param string $sApplicationLocaleRootFs
  72. */
  73. public function setApplicationLocaleRootFs($sApplicationLocaleRootFs){
  74. $this->sApplicationLocaleRootFs=$sApplicationLocaleRootFs;
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function getApplicationLocaleRootFs(){
  80. return $this->sApplicationLocaleRootFs;
  81. }
  82. // Public Methods
  83. //__________________________________________________________________________
  84. // TODO To be done
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /**
  100. * Locale Configuration
  101. *
  102. * It contains all settings about locale. Language, character set, Time, etc...
  103. *
  104. * @package Sylar
  105. * @version 1.0
  106. * @since 07/apr/08
  107. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  108. * @copyright Sylar Development Team
  109. */
  110. class Sylar_LocaleConfiguration{
  111. // Language (example it_IT, en_GB, etc...)
  112. private $sLang;
  113. // Character set
  114. private $sCharSet;
  115. // Time Zone GMT +:
  116. private $iTimeZoneOffsetGMT;
  117. // Currency €, $, £, etc...
  118. // TODO keep attention to symbols like '$'!!!
  119. private $sCurrency;
  120. // A list of common and standard preconfigured Locale
  121. private $aStandardSettings;
  122. // The key selected for the automatic config
  123. private $sStandardSettingKey;
  124.  
  125.  
  126. function __construct($sStandardSettingsKey=null){
  127. // if not set then gets the default
  128. if(is_null($sStandardSettingsKey)){
  129. $sStandardSettingsKey = Sylar_ConfigBox::getDefaultLocaleKey();
  130. }
  131. // prepare array
  132. $this->aStandardSettings = array();
  133. $this->loadStandardSettingsList();
  134. // Set configuration
  135. $this->configureFromStandardSettings($sStandardSettingsKey);
  136. }
  137. function __destruct(){
  138. // nothing to do at the moment
  139. }
  140.  
  141. // Setter and Getter
  142. //__________________________________________________________________________
  143. /**
  144. * @return void
  145. * @param string $sLang
  146. */
  147. public function setLanguage($sLang){
  148. $this->sLang = $sLang;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getLanguage(){
  154. return $this->sLang;
  155. }
  156. /**
  157. * @return void
  158. * @param string $sCharSet
  159. */
  160. public function setCharSet($sCharSet){
  161. $this->sCharSet = $sCharSet;
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function getCharSet(){
  167. return $this->sCharSet;
  168. }
  169. /**
  170. * @return void
  171. * @param int $iTimeZoneOffsetGMT
  172. */
  173. public function setTimeZoneOffset($iTimeZoneOffsetGMT){
  174. $this->iTimeZoneOffsetGMT = $iTimeZoneOffsetGMT;
  175. }
  176. /**
  177. * @return int
  178. */
  179. public function getTimeZoneOffset(){
  180. return $this->iTimeZoneOffsetGMT;
  181. }
  182.  
  183. /**
  184. * @return void
  185. * @param string $sKey
  186. */
  187. public function setStandardSettingKey($sStandardSettingKey){
  188. $this->sStandardSettingKey = $sStandardSettingKey;
  189. }
  190. /**
  191. * @return string
  192. */
  193. public function getStandardSettingKey(){
  194. return $this->sStandardSettingKey;
  195. }
  196.  
  197. // Public Methods
  198. //__________________________________________________________________________
  199.  
  200.  
  201. /**
  202. * Verify if configuration is defined
  203. * return true if all is ok, false otherwise
  204. *
  205. * @return boolean
  206. */
  207. public function check(){
  208. try{
  209. if(is_null( $this->getLanguage() )){
  210. throw new ExceptionInSylar("Bad Locale Configuration. Language is null", 10001 );
  211. }
  212. if(is_null( $this->getCharSet() )){
  213. throw new ExceptionInSylar("Bad Locale Configuration. CharSet is null", 10002 );
  214. }
  215. if(is_null( $this->getTimeZoneOffset() )){
  216. throw new ExceptionInSylar("Bad Locale Configuration. Time Zone Offset is null", 10003 );
  217. }
  218. // TODO to implements currency
  219. // if all is ok return true
  220. return true;
  221. }catch (ExceptionInSylar $ex){
  222. throw $ex;
  223. return false;
  224. }
  225. }
  226. function switchToStandardSetting($sStandardSettingsKey){
  227. try{
  228. // if not set then gets the default
  229. if(is_null($sStandardSettingsKey)){
  230. // all remain the same
  231. throw new ExceptionInSylar("No StandardSettingsKey provided. Configuration not modified", 10009 );
  232. }
  233. // Set configuration
  234. $this->configureFromStandardSettings($sStandardSettingsKey);
  235. }catch (ExceptionInSylar $ex){
  236. throw $ex;
  237. return;
  238. }
  239. }
  240. // Private Methods
  241. //__________________________________________________________________________
  242. /**
  243. * Load defined standard configuration in the array
  244. * in the future it can take the list in different way...
  245. * DB, external config file, ecc...
  246. *
  247. * @return void
  248. */
  249. private function loadStandardSettingsList(){
  250. $this->setStandardSettings("it_IT", array("Lang"=>"it_IT", "CharSet"=>"UTF-8", "TimeZoneOffset"=>"+1", "Currency"=>"€"));
  251. $this->setStandardSettings("en_EN", array("Lang"=>"en_EN", "CharSet"=>"UTF-8", "TimeZoneOffset"=>"0", "Currency"=>"£"));
  252. $this->setStandardSettings("en_US", array("Lang"=>"en_US", "CharSet"=>"UTF-8", "TimeZoneOffset"=>"-8", "Currency"=>"$"));
  253. }
  254. /**
  255. * set a specified array of settings with specified key
  256. *
  257. * @return void
  258. * @param string $sKey the key of array settings
  259. * @param array $aSettings settings array
  260. */
  261. private function setStandardSettings($sKey, $aSettings){
  262. $this->aStandardSettings[$sKey] = $aSettings;
  263. }
  264. /**
  265. * get the configuration array with specified key
  266. *
  267. * @see configureFromStandardSettings
  268. *
  269. * @return array
  270. * @param string $sKey
  271. */
  272. private function getStandardSettings($sKey){
  273. try{
  274. if(!array_key_exists($sKey, $this->aStandardSettings)){
  275. throw new ExceptionInSylar("The key of standard locale configuration does not exists. Key: ".$sKey, 10004 );
  276. return null;
  277. }else{
  278. return $this->aStandardSettings[$sKey];
  279. }
  280. }catch (ExceptionInSylar $ex){
  281. throw $ex;
  282. return null;
  283. }
  284. }
  285. /**
  286. * Load standard configuration
  287. * Load configuration from a specified list
  288. *
  289. * @return void
  290. * @param string $sKey is like: it_IT, en_US, ecc...
  291. */
  292. private function configureFromStandardSettings($sKey){
  293. try{
  294. if(is_null($sKey)){
  295. throw new ExceptionInSylar("The key of standard locale configuration is null", 10005 );
  296. }
  297. if( is_array($this->getStandardSettings($sKey)) ){
  298. // Set the key selected
  299. $this->setStandardSettingKey($sKey);
  300. //Set the config parameters
  301. $aSetting = $this->getStandardSettings($sKey);
  302. $this->setLanguage( $aSetting["Lang"] );
  303. $this->setCharSet( $aSetting["CharSet"] );
  304. $this->setTimeZoneOffset( $aSetting["TimeZoneOffset"] );
  305. // TODO to implement Currency data
  306. }
  307. }catch (ExceptionInSylar $ex){
  308. throw $ex;
  309. }
  310. }
  311.  
  312. }
  313. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:16 +0200 by phpDocumentor 1.3.0RC3