Source for file Language.php

Documentation is available at Language.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.  
  25. import("sylar.common.locale.Locale");
  26. import("sylar.common.system.ConfigBox");
  27.  
  28. /**
  29. * Class to manage the languages and translations
  30. *
  31. * @see Sylar_Locale
  32. * @see Sylar_LocaleConfiguration
  33. *
  34. * @package Sylar
  35. * @version 1.0
  36. * @since 21/feb/08
  37. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  38. * @copyright Sylar Development Team
  39. */
  40. class Sylar_Language extends Sylar_Locale{
  41. // Locale Configuration object
  42. private $oConfig;
  43. // Array that contains the translations
  44. private $aDictionary;
  45. /**
  46. * @return void
  47. * @param Sylar_LocaleConfiguration $oConfig
  48. */
  49. function __construct(Sylar_LocaleConfiguration $oConfig){
  50. parent::__construct($oConfig);
  51. // prepare dictionary
  52. $this->formatDict();
  53. }
  54. function __destruct(){
  55. unset($this->aDictionary);
  56. }
  57.  
  58.  
  59. // Setter and Getter
  60. //__________________________________________________________________________
  61. public function setDictionary($aDictionary){
  62. $this->aDictionary = $aDictionary;
  63. }
  64. public function getDictionary(){
  65. return $this->aDictionary;
  66. }
  67. public function setConfig(Sylar_LocaleConfiguration $oConfig){
  68. parent::setConfig($oConfig);
  69. }
  70. public function getConfig(){
  71. return parent::getConfig();
  72. }
  73. /**
  74. * @return void
  75. * @param string $sApplicationLocaleRootFs
  76. */
  77. public function setApplicationLocaleRootFs($sApplicationLocaleRootFs){
  78. parent::setApplicationLocaleRootFs($sApplicationLocaleRootFs);
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getApplicationLocaleRootFs(){
  84. return parent::getApplicationLocaleRootFs();
  85. }
  86. // Public Methods
  87. //__________________________________________________________________________
  88. /**
  89. * It load a specified Sylar dictionary file
  90. * It load dictionaries from Sylar standard directory
  91. *
  92. * @param string $sDictName Dictionary file name without the extension (must be a .php file)
  93. * @param boolean $bFlagAppend
  94. */
  95. public function loadSylarDictionary($sDictName, $bFlagAppend=true){
  96. try{
  97. // Define right path
  98. $dictPath = Sylar_ConfigBox::getLocaleFsRoot().$this->getConfig()->getLanguage()."/lang/".$sDictName.".php";
  99. // call generic import Dict
  100. $this->loadDictionary($dictPath, $bFlagAppend);
  101. }catch (ExceptionInSylar $ex){
  102. throw $ex;
  103. }
  104. }
  105. public function loadApplicationDictionary($sDictName, $bFlagAppend=true){
  106. try{
  107. if($this->getApplicationLocaleRootFs()==null){
  108. throw new ExceptionInSylar("Application Locale Root on Filesystem is not defined or not visible from the object Language", 10007 );
  109. }
  110. // Define right path
  111. $dictPath = $this->getApplicationLocaleRootFs().$this->getConfig()->getLanguage()."/lang/".$sDictName.".php";
  112. // call generic import Dict
  113. $this->loadDictionary($dictPath, $bFlagAppend);
  114. }catch (ExceptionInSylar $ex){
  115. throw $ex;
  116. }
  117. }
  118.  
  119. /**
  120. * It returns the translation string associated to label in the dictionary array
  121. * A generic error message is showed if the label doesn't exists in the dictionary
  122. *
  123. * @see loadDictionary
  124. *
  125. * @param string $sLabel
  126. * @return string with translations
  127. */
  128. public function provideText($sLabel, $aReplaceList=null){
  129. try{
  130. $result = $this->extractText($sLabel);
  131. if($result==null){
  132. $result = "Translate not avaiable.";
  133. if(Sylar_ConfigBox::isSylarInDebugMode()){
  134. $result .= " [Label: ".$sLabel."]";
  135. }
  136. // the end
  137. return $result;
  138. }
  139. if(!is_null($aReplaceList)){
  140. if(!is_array($aReplaceList)){
  141. throw new ExceptionInSylar("Language encode error! Provided replace array is not an array", 10008 );
  142. }
  143. // Replace in Text from array
  144. $result = vsprintf($result, $aReplaceList);
  145. }
  146. return $result;
  147. }catch (ExceptionInSylar $ex){
  148. throw $ex;
  149. }
  150. }
  151. // Private Method
  152. //__________________________________________________________________________
  153. /**
  154. * Format the dictionary container and prepare it to contain the translations list
  155. *
  156. * @return void
  157. */
  158. private function formatDict(){
  159. unset($this->aDictionary);
  160. $this->aDictionary=array();
  161. }
  162. /**
  163. * Extract the string of translation from dictionary and return it.
  164. * Return null if the label doesn't exists.
  165. *
  166. * @param string $sLabel
  167. * @return string
  168. */
  169. private function extractText($sLabel){
  170. if(array_key_exists($sLabel, $this->getDictionary())){
  171. // TODO use get
  172. return $this->aDictionary[$sLabel];
  173. }else{
  174. return null;
  175. }
  176. }
  177. /**
  178. * Load Dictionary Array from disk.
  179. *
  180. * @param string $sDictPath
  181. * @param boolean $bFlagAppend
  182. */
  183. private function loadDictionary($sDictPath, $bFlagAppend=true){
  184. try{
  185. // Load dict file from disk
  186. if(!file_exists($sDictPath)){
  187. throw new ExceptionInSylar("Dict file does not exists. File: ".$sDictPath, 10006 );
  188. }else{
  189. require_once($sDictPath);
  190. }
  191. // IMPORTANT!
  192. // in the file Dict $dictPath is defined the dict array his name is:
  193. // $aDict = array("Key"=>"Value", etc...)
  194. // Append array
  195. if($bFlagAppend){
  196. $this->setDictionary( array_merge( $this->getDictionary(), $aDict ) );
  197. // Overwrite array
  198. }else{
  199. $this->formatDict();
  200. $this->setDictionary($aDict);
  201. }
  202. }catch (ExceptionInSylar $ex){
  203. throw $ex;
  204. }
  205. }
  206. }
  207. ?>

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