dict.class.inc.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class Dict
  20. * Management of localizable strings
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. class DictException extends CoreException
  26. {
  27. }
  28. class DictExceptionUnknownLanguage extends DictException
  29. {
  30. public function __construct($sLanguageCode)
  31. {
  32. $aContext = array();
  33. $aContext['language_code'] = $sLanguageCode;
  34. parent::__construct('Unknown localization language', $aContext);
  35. }
  36. }
  37. class DictExceptionMissingString extends DictException
  38. {
  39. public function __construct($sLanguageCode, $sStringCode)
  40. {
  41. $aContext = array();
  42. $aContext['language_code'] = $sLanguageCode;
  43. $aContext['string_code'] = $sStringCode;
  44. parent::__construct('Missing localized string', $aContext);
  45. }
  46. }
  47. define('DICT_ERR_STRING', 1); // when a string is missing, return the identifier
  48. define('DICT_ERR_EXCEPTION', 2); // when a string is missing, throw an exception
  49. //define('DICT_ERR_LOG', 3); // when a string is missing, log an error
  50. class Dict
  51. {
  52. protected static $m_bTraceFiles = false;
  53. protected static $m_aEntryFiles = array();
  54. protected static $m_iErrorMode = DICT_ERR_STRING;
  55. protected static $m_sDefaultLanguage = 'EN US';
  56. protected static $m_sCurrentLanguage = null; // No language selected by default
  57. protected static $m_aLanguages = array(); // array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  58. protected static $m_aData = array();
  59. public static function EnableTraceFiles()
  60. {
  61. self::$m_bTraceFiles = true;
  62. }
  63. public static function GetEntryFiles()
  64. {
  65. return self::$m_aEntryFiles;
  66. }
  67. public static function SetDefaultLanguage($sLanguageCode)
  68. {
  69. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  70. {
  71. throw new DictExceptionUnknownLanguage($sLanguageCode);
  72. }
  73. self::$m_sDefaultLanguage = $sLanguageCode;
  74. }
  75. public static function SetUserLanguage($sLanguageCode)
  76. {
  77. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  78. {
  79. throw new DictExceptionUnknownLanguage($sLanguageCode);
  80. }
  81. self::$m_sCurrentLanguage = $sLanguageCode;
  82. }
  83. public static function GetUserLanguage()
  84. {
  85. if (self::$m_sCurrentLanguage == null) // May happen when no user is logged in (i.e login screen, non authentifed page)
  86. {
  87. // In which case let's use the default language
  88. return self::$m_sDefaultLanguage;
  89. }
  90. return self::$m_sCurrentLanguage;
  91. }
  92. //returns a hash array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  93. public static function GetLanguages()
  94. {
  95. return self::$m_aLanguages;
  96. }
  97. // iErrorMode from {DICT_ERR_STRING, DICT_ERR_EXCEPTION}
  98. public static function SetErrorMode($iErrorMode)
  99. {
  100. self::$m_iErrorMode = $iErrorMode;
  101. }
  102. public static function S($sStringCode, $sDefault = null)
  103. {
  104. // Attempt to find the string in the user language
  105. //
  106. if (!array_key_exists(self::GetUserLanguage(), self::$m_aData))
  107. {
  108. // It may happen, when something happens before the dictionnaries get loaded
  109. return $sStringCode;
  110. }
  111. $aCurrentDictionary = self::$m_aData[self::GetUserLanguage()];
  112. if (array_key_exists($sStringCode, $aCurrentDictionary))
  113. {
  114. return $aCurrentDictionary[$sStringCode];
  115. }
  116. // Attempt to find the string in the default language
  117. //
  118. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  119. if (array_key_exists($sStringCode, $aDefaultDictionary))
  120. {
  121. return $aDefaultDictionary[$sStringCode];
  122. }
  123. // Attempt to find the string in english
  124. //
  125. $aDefaultDictionary = self::$m_aData['EN US'];
  126. if (array_key_exists($sStringCode, $aDefaultDictionary))
  127. {
  128. return $aDefaultDictionary[$sStringCode];
  129. }
  130. // Could not find the string...
  131. //
  132. switch (self::$m_iErrorMode)
  133. {
  134. case DICT_ERR_STRING:
  135. if (is_null($sDefault))
  136. {
  137. return $sStringCode;
  138. }
  139. else
  140. {
  141. return $sDefault;
  142. }
  143. break;
  144. case DICT_ERR_EXCEPTION:
  145. default:
  146. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  147. break;
  148. }
  149. return 'bug!';
  150. }
  151. public static function Format($sFormatCode /*, ... arguments ....*/)
  152. {
  153. $sLocalizedFormat = self::S($sFormatCode);
  154. $aArguments = func_get_args();
  155. array_shift($aArguments);
  156. if ($sLocalizedFormat == $sFormatCode)
  157. {
  158. // Make sure the information will be displayed (ex: an error occuring before the dictionary gets loaded)
  159. return $sFormatCode.' - '.implode(', ', $aArguments);
  160. }
  161. return vsprintf($sLocalizedFormat, $aArguments);
  162. }
  163. // sLanguageCode: Code identifying the language i.e. FR-FR
  164. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  165. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  166. // aEntries: Hash array of dictionnary entries
  167. // ~~ or ~* can be used to indicate entries still to be translated.
  168. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  169. {
  170. if (self::$m_bTraceFiles)
  171. {
  172. $aBacktrace = debug_backtrace();
  173. $sFile = $aBacktrace[0]["file"];
  174. foreach($aEntries as $sKey => $sValue)
  175. {
  176. self::$m_aEntryFiles[$sLanguageCode][$sKey] = array(
  177. 'file' => $sFile,
  178. 'value' => $sValue
  179. );
  180. }
  181. }
  182. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  183. {
  184. self::$m_aLanguages[$sLanguageCode] = array('description' => $sEnglishLanguageDesc, 'localized_description' => $sLocalizedLanguageDesc);
  185. self::$m_aData[$sLanguageCode] = array();
  186. }
  187. foreach($aEntries as $sCode => $sValue)
  188. {
  189. self::$m_aData[$sLanguageCode][$sCode] = self::FilterString($sValue);
  190. }
  191. }
  192. /**
  193. * Clone a string in every language (if it exists in that language)
  194. */
  195. public static function CloneString($sSourceCode, $sDestCode)
  196. {
  197. foreach(self::$m_aLanguages as $sLanguageCode => $foo)
  198. {
  199. if (isset(self::$m_aData[$sLanguageCode][$sSourceCode]))
  200. {
  201. self::$m_aData[$sLanguageCode][$sDestCode] = self::$m_aData[$sLanguageCode][$sSourceCode];
  202. }
  203. }
  204. }
  205. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  206. {
  207. $aMissing = array(); // Strings missing for the target language
  208. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  209. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  210. $aOK = array(); // Strings having different values in both dictionaries
  211. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  212. {
  213. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  214. {
  215. $aMissing[$sStringCode] = $sValue;
  216. }
  217. }
  218. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  219. {
  220. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  221. {
  222. $aUnexpected[$sStringCode] = $sValue;
  223. }
  224. else
  225. {
  226. // The value exists in the reference
  227. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  228. if ($sValue == $sRefValue)
  229. {
  230. $aNotTranslated[$sStringCode] = $sValue;
  231. }
  232. else
  233. {
  234. $aOK[$sStringCode] = $sValue;
  235. }
  236. }
  237. }
  238. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  239. }
  240. public static function Dump()
  241. {
  242. MyHelpers::var_dump_html(self::$m_aData);
  243. }
  244. public static function InCache($sApplicationPrefix)
  245. {
  246. if (function_exists('apc_fetch'))
  247. {
  248. $bResult = false;
  249. // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
  250. //
  251. self::$m_aData = apc_fetch($sApplicationPrefix.'-dict');
  252. if (is_bool(self::$m_aData) && (self::$m_aData === false))
  253. {
  254. self::$m_aData = array();
  255. }
  256. else
  257. {
  258. self::$m_aLanguages = apc_fetch($sApplicationPrefix.'-languages');
  259. if (is_bool(self::$m_aLanguages) && (self::$m_aLanguages === false))
  260. {
  261. self::$m_aLanguages = array();
  262. }
  263. else
  264. {
  265. $bResult = true;
  266. }
  267. }
  268. return $bResult;
  269. }
  270. return false;
  271. }
  272. public static function InitCache($sApplicationPrefix)
  273. {
  274. if (function_exists('apc_store'))
  275. {
  276. apc_store($sApplicationPrefix.'-languages', self::$m_aLanguages);
  277. apc_store($sApplicationPrefix.'-dict', self::$m_aData);
  278. }
  279. }
  280. public static function ResetCache($sApplicationPrefix)
  281. {
  282. if (function_exists('apc_delete'))
  283. {
  284. apc_delete($sApplicationPrefix.'-languages');
  285. apc_delete($sApplicationPrefix.'-dict');
  286. }
  287. }
  288. protected static function FilterString($s)
  289. {
  290. return str_replace(array('~~', '~*'), '', $s);
  291. }
  292. }
  293. ?>