dict.class.inc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Dict
  4. * Handles Localizable data
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class DictException extends CoreException
  15. {
  16. }
  17. class DictExceptionUnknownLanguage extends DictException
  18. {
  19. public function __construct($sLanguageCode)
  20. {
  21. $aContext = array();
  22. $aContext['language_code'] = $sLanguageCode;
  23. parent::__construct('Unknown localization language', $aContext);
  24. }
  25. }
  26. class DictExceptionMissingString extends DictException
  27. {
  28. public function __construct($sLanguageCode, $sStringCode)
  29. {
  30. $aContext = array();
  31. $aContext['language_code'] = $sLanguageCode;
  32. $aContext['string_code'] = $sStringCode;
  33. parent::__construct('Missing localized string', $aContext);
  34. }
  35. }
  36. define('DICT_ERR_STRING', 1); // when a string is missing, return the identifier
  37. define('DICT_ERR_EXCEPTION', 2); // when a string is missing, throw an exception
  38. //define('DICT_ERR_LOG', 3); // when a string is missing, log an error
  39. class Dict
  40. {
  41. protected static $m_iErrorMode = DICT_ERR_STRING;
  42. protected static $m_sDefaultLanguage = 'EN US';
  43. protected static $m_sCurrentLanguage = 'EN US';
  44. protected static $m_aLanguages = array(); // array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  45. protected static $m_aData = array();
  46. public static function SetDefaultLanguage($sLanguageCode)
  47. {
  48. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  49. {
  50. throw new DictExceptionUnknownLanguage($sLanguageCode);
  51. }
  52. self::$m_sDefaultLanguage = $sLanguageCode;
  53. }
  54. public static function SetUserLanguage($sLanguageCode)
  55. {
  56. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  57. {
  58. throw new DictExceptionUnknownLanguage($sLanguageCode);
  59. }
  60. self::$m_sCurrentLanguage = $sLanguageCode;
  61. }
  62. //returns a hash array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  63. public static function GetLanguages()
  64. {
  65. return self::$m_aLanguages;
  66. }
  67. // iErrorMode from {DICT_ERR_STRING, DICT_ERR_EXCEPTION}
  68. public static function SetErrorMode($iErrorMode)
  69. {
  70. self::$m_iErrorMode = $iErrorMode;
  71. }
  72. public static function S($sStringCode, $sDefault = null)
  73. {
  74. // Attempt to find the string in the user language
  75. //
  76. $aCurrentDictionary = self::$m_aData[self::$m_sCurrentLanguage];
  77. if (array_key_exists($sStringCode, $aCurrentDictionary))
  78. {
  79. return $aCurrentDictionary[$sStringCode];
  80. }
  81. // Attempt to find the string in the default language
  82. //
  83. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  84. if (array_key_exists($sStringCode, $aDefaultDictionary))
  85. {
  86. return $aDefaultDictionary[$sStringCode];
  87. }
  88. // Could not find the string...
  89. //
  90. switch (self::$m_iErrorMode)
  91. {
  92. case DICT_ERR_STRING:
  93. if (is_null($sDefault))
  94. {
  95. return $sStringCode;
  96. }
  97. else
  98. {
  99. return $sDefault;
  100. }
  101. break;
  102. case DICT_ERR_EXCEPTION:
  103. default:
  104. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  105. break;
  106. }
  107. return 'bug!';
  108. }
  109. public static function Format($sFormatCode /*, ... arguments ....*/)
  110. {
  111. $sLocalizedFormat = self::S($sFormatCode);
  112. $aArguments = func_get_args();
  113. array_shift($aArguments);
  114. return vsprintf($sLocalizedFormat, $aArguments);
  115. }
  116. // sLanguageCode: Code identifying the language i.e. FR-FR
  117. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  118. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  119. // aEntries: Hash array of dictionnary entries
  120. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  121. {
  122. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  123. {
  124. self::$m_aLanguages[$sLanguageCode] = array($sEnglishLanguageDesc, $sLocalizedLanguageDesc);
  125. self::$m_aData[$sLanguageCode] = array();
  126. }
  127. self::$m_aData[$sLanguageCode] = array_merge(self::$m_aData[$sLanguageCode], $aEntries);
  128. }
  129. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  130. {
  131. $aMissing = array(); // Strings missing for the target language
  132. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  133. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  134. $aOK = array(); // Strings having different values in both dictionaries
  135. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  136. {
  137. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  138. {
  139. $aMissing[$sStringCode] = $sValue;
  140. }
  141. }
  142. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  143. {
  144. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  145. {
  146. $aUnexpected[$sStringCode] = $sValue;
  147. }
  148. else
  149. {
  150. // The value exists in the reference
  151. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  152. if ($sValue == $sRefValue)
  153. {
  154. $aNotTranslated[$sStringCode] = $sValue;
  155. }
  156. else
  157. {
  158. $aOK[$sStringCode] = $sValue;
  159. }
  160. }
  161. }
  162. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  163. }
  164. public static function Dump()
  165. {
  166. MyHelpers::var_dump_html(self::$m_aData);
  167. }
  168. }
  169. /*
  170. Dans les templates, les chaines localizables seront remplacées par un tag <itopstring>code_de_la_chaine</itopstring>
  171. Modifier les profils utilisateurs pour stocker le langage de l'utilisateur.
  172. */
  173. ?>