dict.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class Dict
  18. * Management of localizable strings
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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_iErrorMode = DICT_ERR_STRING;
  53. protected static $m_sDefaultLanguage = 'EN US';
  54. protected static $m_sCurrentLanguage = 'EN US';
  55. protected static $m_aLanguages = array(); // array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  56. protected static $m_aData = array();
  57. public static function SetDefaultLanguage($sLanguageCode)
  58. {
  59. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  60. {
  61. throw new DictExceptionUnknownLanguage($sLanguageCode);
  62. }
  63. self::$m_sDefaultLanguage = $sLanguageCode;
  64. }
  65. public static function SetUserLanguage($sLanguageCode)
  66. {
  67. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  68. {
  69. throw new DictExceptionUnknownLanguage($sLanguageCode);
  70. }
  71. self::$m_sCurrentLanguage = $sLanguageCode;
  72. }
  73. //returns a hash array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  74. public static function GetLanguages()
  75. {
  76. return self::$m_aLanguages;
  77. }
  78. // iErrorMode from {DICT_ERR_STRING, DICT_ERR_EXCEPTION}
  79. public static function SetErrorMode($iErrorMode)
  80. {
  81. self::$m_iErrorMode = $iErrorMode;
  82. }
  83. public static function S($sStringCode, $sDefault = null)
  84. {
  85. // Attempt to find the string in the user language
  86. //
  87. if (!array_key_exists(self::$m_sCurrentLanguage, self::$m_aData))
  88. {
  89. // It may happen, when something happens before the dictionnaries get loaded
  90. return $sStringCode;
  91. }
  92. $aCurrentDictionary = self::$m_aData[self::$m_sCurrentLanguage];
  93. if (array_key_exists($sStringCode, $aCurrentDictionary))
  94. {
  95. return $aCurrentDictionary[$sStringCode];
  96. }
  97. // Attempt to find the string in the default language
  98. //
  99. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  100. if (array_key_exists($sStringCode, $aDefaultDictionary))
  101. {
  102. return $aDefaultDictionary[$sStringCode];
  103. }
  104. // Could not find the string...
  105. //
  106. switch (self::$m_iErrorMode)
  107. {
  108. case DICT_ERR_STRING:
  109. if (is_null($sDefault))
  110. {
  111. return $sStringCode;
  112. }
  113. else
  114. {
  115. return $sDefault;
  116. }
  117. break;
  118. case DICT_ERR_EXCEPTION:
  119. default:
  120. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  121. break;
  122. }
  123. return 'bug!';
  124. }
  125. public static function Format($sFormatCode /*, ... arguments ....*/)
  126. {
  127. $sLocalizedFormat = self::S($sFormatCode);
  128. $aArguments = func_get_args();
  129. if ($sLocalizedFormat == $sFormatCode)
  130. {
  131. // Make sure the information will be displayed (ex: an error occuring before the dictionary gets loaded)
  132. return $sFormatCode.' - '.implode(', ', $aArguments);
  133. }
  134. array_shift($aArguments);
  135. return vsprintf($sLocalizedFormat, $aArguments);
  136. }
  137. // sLanguageCode: Code identifying the language i.e. FR-FR
  138. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  139. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  140. // aEntries: Hash array of dictionnary entries
  141. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  142. {
  143. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  144. {
  145. self::$m_aLanguages[$sLanguageCode] = array('description' => $sEnglishLanguageDesc, 'localized_description' => $sLocalizedLanguageDesc);
  146. self::$m_aData[$sLanguageCode] = array();
  147. }
  148. self::$m_aData[$sLanguageCode] = array_merge(self::$m_aData[$sLanguageCode], $aEntries);
  149. }
  150. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  151. {
  152. $aMissing = array(); // Strings missing for the target language
  153. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  154. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  155. $aOK = array(); // Strings having different values in both dictionaries
  156. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  157. {
  158. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  159. {
  160. $aMissing[$sStringCode] = $sValue;
  161. }
  162. }
  163. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  164. {
  165. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  166. {
  167. $aUnexpected[$sStringCode] = $sValue;
  168. }
  169. else
  170. {
  171. // The value exists in the reference
  172. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  173. if ($sValue == $sRefValue)
  174. {
  175. $aNotTranslated[$sStringCode] = $sValue;
  176. }
  177. else
  178. {
  179. $aOK[$sStringCode] = $sValue;
  180. }
  181. }
  182. }
  183. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  184. }
  185. public static function Dump()
  186. {
  187. MyHelpers::var_dump_html(self::$m_aData);
  188. }
  189. }
  190. /*
  191. Dans les templates, les chaines localizables seront remplacées par un tag <itopstring>code_de_la_chaine</itopstring>
  192. Modifier les profils utilisateurs pour stocker le langage de l'utilisateur.
  193. */
  194. ?>