dict.class.inc.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = null; // No language selected by default
  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. public static function GetCurrentLanguage()
  74. {
  75. if (self::$m_sCurrentLanguage == null) // May happen when no user is logged in (i.e login screen, non authentifed page)
  76. {
  77. // In which case let's use the default language
  78. return self::$m_sDefaultLanguage;
  79. }
  80. return self::$m_sCurrentLanguage;
  81. }
  82. //returns a hash array( code => array( 'description' => '...', 'localized_description' => '...') ...)
  83. public static function GetLanguages()
  84. {
  85. return self::$m_aLanguages;
  86. }
  87. // iErrorMode from {DICT_ERR_STRING, DICT_ERR_EXCEPTION}
  88. public static function SetErrorMode($iErrorMode)
  89. {
  90. self::$m_iErrorMode = $iErrorMode;
  91. }
  92. public static function S($sStringCode, $sDefault = null)
  93. {
  94. // Attempt to find the string in the user language
  95. //
  96. if (!array_key_exists(self::GetCurrentLanguage(), self::$m_aData))
  97. {
  98. // It may happen, when something happens before the dictionnaries get loaded
  99. return $sStringCode;
  100. }
  101. $aCurrentDictionary = self::$m_aData[self::GetCurrentLanguage()];
  102. if (array_key_exists($sStringCode, $aCurrentDictionary))
  103. {
  104. return $aCurrentDictionary[$sStringCode];
  105. }
  106. // Attempt to find the string in the default language
  107. //
  108. $aDefaultDictionary = self::$m_aData[self::$m_sDefaultLanguage];
  109. if (array_key_exists($sStringCode, $aDefaultDictionary))
  110. {
  111. return $aDefaultDictionary[$sStringCode];
  112. }
  113. // Could not find the string...
  114. //
  115. switch (self::$m_iErrorMode)
  116. {
  117. case DICT_ERR_STRING:
  118. if (is_null($sDefault))
  119. {
  120. return $sStringCode;
  121. }
  122. else
  123. {
  124. return $sDefault;
  125. }
  126. break;
  127. case DICT_ERR_EXCEPTION:
  128. default:
  129. throw new DictExceptionMissingString(self::$m_sCurrentLanguage, $sStringCode);
  130. break;
  131. }
  132. return 'bug!';
  133. }
  134. public static function Format($sFormatCode /*, ... arguments ....*/)
  135. {
  136. $sLocalizedFormat = self::S($sFormatCode);
  137. $aArguments = func_get_args();
  138. if ($sLocalizedFormat == $sFormatCode)
  139. {
  140. // Make sure the information will be displayed (ex: an error occuring before the dictionary gets loaded)
  141. return $sFormatCode.' - '.implode(', ', $aArguments);
  142. }
  143. array_shift($aArguments);
  144. return vsprintf($sLocalizedFormat, $aArguments);
  145. }
  146. // sLanguageCode: Code identifying the language i.e. FR-FR
  147. // sEnglishLanguageDesc: Description of the language code, in English. i.e. French (France)
  148. // sLocalizedLanguageDesc: Description of the language code, in its own language. i.e. Français (France)
  149. // aEntries: Hash array of dictionnary entries
  150. public static function Add($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
  151. {
  152. if (!array_key_exists($sLanguageCode, self::$m_aLanguages))
  153. {
  154. self::$m_aLanguages[$sLanguageCode] = array('description' => $sEnglishLanguageDesc, 'localized_description' => $sLocalizedLanguageDesc);
  155. self::$m_aData[$sLanguageCode] = array();
  156. }
  157. self::$m_aData[$sLanguageCode] = array_merge(self::$m_aData[$sLanguageCode], $aEntries);
  158. }
  159. public static function MakeStats($sLanguageCode, $sLanguageRef = 'EN US')
  160. {
  161. $aMissing = array(); // Strings missing for the target language
  162. $aUnexpected = array(); // Strings defined for the target language, but not found in the reference dictionary
  163. $aNotTranslated = array(); // Strings having the same value in both dictionaries
  164. $aOK = array(); // Strings having different values in both dictionaries
  165. foreach (self::$m_aData[$sLanguageRef] as $sStringCode => $sValue)
  166. {
  167. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageCode]))
  168. {
  169. $aMissing[$sStringCode] = $sValue;
  170. }
  171. }
  172. foreach (self::$m_aData[$sLanguageCode] as $sStringCode => $sValue)
  173. {
  174. if (!array_key_exists($sStringCode, self::$m_aData[$sLanguageRef]))
  175. {
  176. $aUnexpected[$sStringCode] = $sValue;
  177. }
  178. else
  179. {
  180. // The value exists in the reference
  181. $sRefValue = self::$m_aData[$sLanguageRef][$sStringCode];
  182. if ($sValue == $sRefValue)
  183. {
  184. $aNotTranslated[$sStringCode] = $sValue;
  185. }
  186. else
  187. {
  188. $aOK[$sStringCode] = $sValue;
  189. }
  190. }
  191. }
  192. return array($aMissing, $aUnexpected, $aNotTranslated, $aOK);
  193. }
  194. public static function Dump()
  195. {
  196. MyHelpers::var_dump_html(self::$m_aData);
  197. }
  198. }
  199. ?>