dict.class.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_sCurrentLanguage = 'EN US';
  43. protected static $m_aLanguages = array(); // array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  44. protected static $m_aData = array();
  45. public static function SetLanguage($sLanguageCode)
  46. {
  47. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  48. {
  49. throw new DictExceptionUnknownLanguage($sLanguageCode);
  50. }
  51. self::$m_sCurrentLanguage = $sLanguageCode;
  52. }
  53. //returns a hash array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  54. public static function GetLanguages()
  55. {
  56. return self::$m_aLanguages;
  57. }
  58. // iErrorMode from {DICT_ERR_STRING, DICT_ERR_EXCEPTION}
  59. public static function SetErrorMode($iErrorMode)
  60. {
  61. self::$m_iErrorMode = $iErrorMode;
  62. }
  63. public static function S($sStringCode, $sDefault = null)
  64. {
  65. $aCurrentDictionary = self::$m_aData[self::$m_sCurrentLanguage];
  66. if (!array_key_exists($sStringCode, $aCurrentDictionary))
  67. {
  68. switch (self::$m_iErrorMode)
  69. {
  70. case DICT_ERR_STRING:
  71. if (is_null($sDefault))
  72. {
  73. return $sStringCode;
  74. }
  75. else
  76. {
  77. return $sDefault;
  78. }
  79. break;
  80. case DICT_ERR_EXCEPTION:
  81. default:
  82. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  83. break;
  84. }
  85. }
  86. return $aCurrentDictionary[$sStringCode];
  87. }
  88. public static function Format($sFormatCode /*, ... arguments ....*/)
  89. {
  90. $sLocalizedFormat = self::S($sFormatCode);
  91. $aArguments = func_get_args();
  92. array_shift($aArguments);
  93. return vsprintf($sFormat, $aArguments);
  94. }
  95. // sLanguageCode: Code identifying the language i.e. FR-FR
  96. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  97. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  98. // aEntries: Hash array of dictionnary entries
  99. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  100. {
  101. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  102. {
  103. self::$m_aLanguages[$sLanguageCode] = array($sEnglishLanguageDesc, $sLocalizedLanguageDesc);
  104. self::$m_aData[$sLanguageCode] = array();
  105. }
  106. self::$m_aData[$sLanguageCode] = array_merge(self::$m_aData[$sLanguageCode], $aEntries);
  107. }
  108. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  109. {
  110. $aMissing = array(); // Strings missing for the target language
  111. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  112. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  113. $aOK = array(); // Strings having different values in both dictionaries
  114. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  115. {
  116. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  117. {
  118. $aMissing[$sStringCode] = $sValue;
  119. }
  120. }
  121. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  122. {
  123. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  124. {
  125. $aUnexpected[$sStringCode] = $sValue;
  126. }
  127. else
  128. {
  129. // The value exists in the reference
  130. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  131. if ($sValue == $sRefValue)
  132. {
  133. $aNotTranslated[$sStringCode] = $sValue;
  134. }
  135. else
  136. {
  137. $aOK[$sStringCode] = $sValue;
  138. }
  139. }
  140. }
  141. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  142. }
  143. public static function Dump()
  144. {
  145. MyHelpers::var_dump_html(self::$m_aData);
  146. }
  147. }
  148. /*
  149. Dans les templates, les chaines localizables seront remplacées par un tag <itopstring>code_de_la_chaine</itopstring>
  150. Modifier les profils utilisateurs pour stocker le langage de l'utilisateur.
  151. */
  152. ?>