dict.class.inc.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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, $bUserLanguageOnly = false)
  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. if (!$bUserLanguageOnly)
  117. {
  118. // Attempt to find the string in the default language
  119. //
  120. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  121. if (array_key_exists($sStringCode, $aDefaultDictionary))
  122. {
  123. return $aDefaultDictionary[$sStringCode];
  124. }
  125. // Attempt to find the string in english
  126. //
  127. $aDefaultDictionary = self::$m_aData['EN US'];
  128. if (array_key_exists($sStringCode, $aDefaultDictionary))
  129. {
  130. return $aDefaultDictionary[$sStringCode];
  131. }
  132. }
  133. // Could not find the string...
  134. //
  135. switch (self::$m_iErrorMode)
  136. {
  137. case DICT_ERR_STRING:
  138. if (is_null($sDefault))
  139. {
  140. return $sStringCode;
  141. }
  142. else
  143. {
  144. return $sDefault;
  145. }
  146. break;
  147. case DICT_ERR_EXCEPTION:
  148. default:
  149. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  150. break;
  151. }
  152. return 'bug!';
  153. }
  154. public static function Format($sFormatCode /*, ... arguments ....*/)
  155. {
  156. $sLocalizedFormat = self::S($sFormatCode);
  157. $aArguments = func_get_args();
  158. array_shift($aArguments);
  159. if ($sLocalizedFormat == $sFormatCode)
  160. {
  161. // Make sure the information will be displayed (ex: an error occuring before the dictionary gets loaded)
  162. return $sFormatCode.' - '.implode(', ', $aArguments);
  163. }
  164. return vsprintf($sLocalizedFormat, $aArguments);
  165. }
  166. // sLanguageCode: Code identifying the language i.e. FR-FR
  167. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  168. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  169. // aEntries: Hash array of dictionnary entries
  170. // ~~ or ~* can be used to indicate entries still to be translated.
  171. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  172. {
  173. if (self::$m_bTraceFiles)
  174. {
  175. $aBacktrace = debug_backtrace();
  176. $sFile = $aBacktrace[0]["file"];
  177. foreach($aEntries as $sKey => $sValue)
  178. {
  179. self::$m_aEntryFiles[$sLanguageCode][$sKey] = array(
  180. 'file' => $sFile,
  181. 'value' => $sValue
  182. );
  183. }
  184. }
  185. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  186. {
  187. self::$m_aLanguages[$sLanguageCode] = array('description' => $sEnglishLanguageDesc, 'localized_description' => $sLocalizedLanguageDesc);
  188. self::$m_aData[$sLanguageCode] = array();
  189. }
  190. foreach($aEntries as $sCode => $sValue)
  191. {
  192. self::$m_aData[$sLanguageCode][$sCode] = self::FilterString($sValue);
  193. }
  194. }
  195. /**
  196. * Clone a string in every language (if it exists in that language)
  197. */
  198. public static function CloneString($sSourceCode, $sDestCode)
  199. {
  200. foreach(self::$m_aLanguages as $sLanguageCode => $foo)
  201. {
  202. if (isset(self::$m_aData[$sLanguageCode][$sSourceCode]))
  203. {
  204. self::$m_aData[$sLanguageCode][$sDestCode] = self::$m_aData[$sLanguageCode][$sSourceCode];
  205. }
  206. }
  207. }
  208. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  209. {
  210. $aMissing = array(); // Strings missing for the target language
  211. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  212. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  213. $aOK = array(); // Strings having different values in both dictionaries
  214. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  215. {
  216. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  217. {
  218. $aMissing[$sStringCode] = $sValue;
  219. }
  220. }
  221. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  222. {
  223. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  224. {
  225. $aUnexpected[$sStringCode] = $sValue;
  226. }
  227. else
  228. {
  229. // The value exists in the reference
  230. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  231. if ($sValue == $sRefValue)
  232. {
  233. $aNotTranslated[$sStringCode] = $sValue;
  234. }
  235. else
  236. {
  237. $aOK[$sStringCode] = $sValue;
  238. }
  239. }
  240. }
  241. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  242. }
  243. public static function Dump()
  244. {
  245. MyHelpers::var_dump_html(self::$m_aData);
  246. }
  247. public static function InCache($sApplicationPrefix)
  248. {
  249. if (function_exists('apc_fetch'))
  250. {
  251. $bResult = false;
  252. // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
  253. //
  254. self::$m_aData = apc_fetch($sApplicationPrefix.'-dict');
  255. if (is_bool(self::$m_aData) && (self::$m_aData === false))
  256. {
  257. self::$m_aData = array();
  258. }
  259. else
  260. {
  261. self::$m_aLanguages = apc_fetch($sApplicationPrefix.'-languages');
  262. if (is_bool(self::$m_aLanguages) && (self::$m_aLanguages === false))
  263. {
  264. self::$m_aLanguages = array();
  265. }
  266. else
  267. {
  268. $bResult = true;
  269. }
  270. }
  271. return $bResult;
  272. }
  273. return false;
  274. }
  275. public static function InitCache($sApplicationPrefix)
  276. {
  277. if (function_exists('apc_store'))
  278. {
  279. apc_store($sApplicationPrefix.'-languages', self::$m_aLanguages);
  280. apc_store($sApplicationPrefix.'-dict', self::$m_aData);
  281. }
  282. }
  283. public static function ResetCache($sApplicationPrefix)
  284. {
  285. if (function_exists('apc_delete'))
  286. {
  287. apc_delete($sApplicationPrefix.'-languages');
  288. apc_delete($sApplicationPrefix.'-dict');
  289. }
  290. }
  291. protected static function FilterString($s)
  292. {
  293. return str_replace(array('~~', '~*'), '', $s);
  294. }
  295. }
  296. ?>