dict.class.inc.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $aCurrentDictionary = self::$m_aData[self::$m_sCurrentLanguage];
  88. if (array_key_exists($sStringCode, $aCurrentDictionary))
  89. {
  90. return $aCurrentDictionary[$sStringCode];
  91. }
  92. // Attempt to find the string in the default language
  93. //
  94. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  95. if (array_key_exists($sStringCode, $aDefaultDictionary))
  96. {
  97. return $aDefaultDictionary[$sStringCode];
  98. }
  99. // Could not find the string...
  100. //
  101. switch (self::$m_iErrorMode)
  102. {
  103. case DICT_ERR_STRING:
  104. if (is_null($sDefault))
  105. {
  106. return $sStringCode;
  107. }
  108. else
  109. {
  110. return $sDefault;
  111. }
  112. break;
  113. case DICT_ERR_EXCEPTION:
  114. default:
  115. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  116. break;
  117. }
  118. return 'bug!';
  119. }
  120. public static function Format($sFormatCode /*, ... arguments ....*/)
  121. {
  122. $sLocalizedFormat = self::S($sFormatCode);
  123. $aArguments = func_get_args();
  124. array_shift($aArguments);
  125. return vsprintf($sLocalizedFormat, $aArguments);
  126. }
  127. // sLanguageCode: Code identifying the language i.e. FR-FR
  128. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  129. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  130. // aEntries: Hash array of dictionnary entries
  131. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  132. {
  133. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  134. {
  135. self::$m_aLanguages[$sLanguageCode] = array($sEnglishLanguageDesc, $sLocalizedLanguageDesc);
  136. self::$m_aData[$sLanguageCode] = array();
  137. }
  138. self::$m_aData[$sLanguageCode] = array_merge(self::$m_aData[$sLanguageCode], $aEntries);
  139. }
  140. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  141. {
  142. $aMissing = array(); // Strings missing for the target language
  143. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  144. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  145. $aOK = array(); // Strings having different values in both dictionaries
  146. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  147. {
  148. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  149. {
  150. $aMissing[$sStringCode] = $sValue;
  151. }
  152. }
  153. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  154. {
  155. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  156. {
  157. $aUnexpected[$sStringCode] = $sValue;
  158. }
  159. else
  160. {
  161. // The value exists in the reference
  162. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  163. if ($sValue == $sRefValue)
  164. {
  165. $aNotTranslated[$sStringCode] = $sValue;
  166. }
  167. else
  168. {
  169. $aOK[$sStringCode] = $sValue;
  170. }
  171. }
  172. }
  173. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  174. }
  175. public static function Dump()
  176. {
  177. MyHelpers::var_dump_html(self::$m_aData);
  178. }
  179. }
  180. /*
  181. Dans les templates, les chaines localizables seront remplacées par un tag <itopstring>code_de_la_chaine</itopstring>
  182. Modifier les profils utilisateurs pour stocker le langage de l'utilisateur.
  183. */
  184. ?>