PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/atlas/engine/ezc/Translation/src/translation.php

https://github.com/jacomyma/GEXF-Atlas
PHP | 196 lines | 69 code | 6 blank | 121 comment | 8 complexity | 16f787a615b4997c242cee63fd6e5129 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
  4. * @license http://ez.no/licenses/new_bsd New BSD License
  5. * @version 1.2
  6. * @filesource
  7. * @package Translation
  8. */
  9. /**
  10. * ezcTranslation is a container that holds the translated strings for a
  11. * specific context.
  12. *
  13. * ezcTranslation objects are returned by the ezcTranslationManager for every
  14. * requested context.
  15. *
  16. * For an example see {@link ezcTranslationManager}.
  17. *
  18. * @package Translation
  19. * @version 1.2
  20. * @mainclass
  21. */
  22. class ezcTranslation
  23. {
  24. /**
  25. * Contains an array where the key is the original string (often
  26. * English) and the element is an object of the class
  27. * ezcTranslationData (a struct).
  28. *
  29. * @var array(string=>ezcTranslationData)
  30. */
  31. private $translationMap;
  32. /**
  33. * Constructs the ezcTranslation object.
  34. *
  35. * The constructor receives an array containing the translation elements,
  36. * and builds up an internal map between the original string and the
  37. * accompanying translation data.
  38. *
  39. * @param array(ezcTranslationData) $data
  40. */
  41. function __construct( array $data )
  42. {
  43. $this->translationMap = array();
  44. foreach ( $data as $translationElement )
  45. {
  46. $this->translationMap[$translationElement->original] = $translationElement;
  47. }
  48. }
  49. /**
  50. * Returns the replacement for the key $key from the parameters $params.
  51. *
  52. * The params array is an associative array in the form array('key'=>'value').
  53. *
  54. * This is a callback function used by the getTranslation() method for each
  55. * matched parameter in the translated string.
  56. *
  57. * @param string $key
  58. * @param array $params
  59. * @return string
  60. */
  61. private function parameterCallback( $key, array $params )
  62. {
  63. if ( !isset( $params[strtolower( $key )] ) )
  64. {
  65. throw new ezcTranslationParameterMissingException( $key );
  66. }
  67. $string = $params[strtolower( $key )];
  68. // We use ctype_upper() here to check if the first character of the key
  69. // is an uppercase letter. If it is then we make the first character of
  70. // the returned translation also an upper case character. With this
  71. // mechanism we can correctly upper case translated strings if word
  72. // order changes. See
  73. // {@link ezcTranslationTest::testGetStringWithParameters} for an
  74. // example of this.
  75. if ( ctype_upper( $key[0] ) )
  76. {
  77. $string = ucfirst( $string );
  78. }
  79. return $string;
  80. }
  81. /**
  82. * Returns the translated version of the original string $key.
  83. *
  84. * This method returns a translated string and substitutes the parameters $param
  85. * in the localized string.
  86. *
  87. * @throws ezcTranslationKeyNotAvailableException when the key is not available
  88. * @throws ezcTranslationParameterMissingException when not enough
  89. * parameters are passed for a parameterized string
  90. * @param string $key
  91. * @param array(string=>string) $params
  92. * @return string
  93. */
  94. public function getTranslation( $key, array $params = array() )
  95. {
  96. if ( !isset( $this->translationMap[$key] ) )
  97. {
  98. throw new ezcTranslationKeyNotAvailableException( $key );
  99. }
  100. $translatedString = $this->translationMap[$key]->translation;
  101. // Little optimization to prevent preg if not needed, it bails out too
  102. // if there is just a percent sign in the string without a valid
  103. // parameter-identifier, but we can live with that.
  104. if ( strstr( $translatedString, '%' ) === false )
  105. {
  106. return $translatedString;
  107. }
  108. // So we do have a possibility of a parameterized string, replace those
  109. // with the parameters. The callback function can actually throw an
  110. // exception to tell that there was a missing parameter.
  111. return (string) preg_replace( '@%(([A-Za-z][a-z_]*[a-z])|[1-9])@e', '$this->parameterCallback("\\1", $params)', $translatedString );
  112. }
  113. /**
  114. * Returns the replacement for the key $key from the parameters $params.
  115. *
  116. * The params array is an associative array in the form array('key'=>'value').
  117. *
  118. * This is a callback function used by the compileTranslation() method for each
  119. * matched parameter in the translated string.
  120. *
  121. * @param string $key
  122. * @param array $params
  123. * @return string
  124. */
  125. private function parameterCallbackCompile( $key, array $params )
  126. {
  127. if ( !isset( $params[strtolower( $key )] ) )
  128. {
  129. throw new ezcTranslationParameterMissingException( $key );
  130. }
  131. // We use ctype_upper() here to check if the first character of the key
  132. // is an uppercase letter. If it is then we make the first character of
  133. // the returned translation also an upper case character. With this
  134. // mechanism we can correctly upper case translated strings if word
  135. // order changes. See
  136. // {@link ezcTranslationTest::testGetStringWithParameters} for an
  137. // example of this.
  138. if ( ctype_upper( $key[0] ) )
  139. {
  140. $string = "' . ucfirst(". $params[strtolower( $key )] . ") . '";
  141. }
  142. else
  143. {
  144. $string = "' . ". $params[strtolower( $key )] . " . '";
  145. }
  146. return $string;
  147. }
  148. /**
  149. * Returns the translated version of the original string $key.
  150. *
  151. * This method returns a translated string and substitutes the parameters $param
  152. * in the localized string with PHP code to place the variable data into
  153. * the string at a later moment. Instead of the values for each of the
  154. * parameters, an expression to get to the data should be sumbitted into
  155. * the $params array.
  156. *
  157. * <code>
  158. * echo $translation->compileTranslation( "Hello #%nr", array( "nr" => '$this->send->nr' ) );
  159. * </code>
  160. *
  161. * Will return something like:
  162. * <code>
  163. * 'Hallo #' . $this->send->nr . ''
  164. * </code>
  165. *
  166. * @param string $key
  167. * @param array(string=>string) $params
  168. * @return string
  169. */
  170. public function compileTranslation( $key, array $params = array() )
  171. {
  172. if ( !isset( $this->translationMap[$key] ) )
  173. {
  174. throw new ezcTranslationKeyNotAvailableException( $key );
  175. }
  176. $translatedString = var_export( $this->translationMap[$key]->translation, true );
  177. // Little optimization to prevent preg if not needed, it bails out too
  178. // if there is just a percent sign in the string without a valid
  179. // parameter-identifier, but we can live with that.
  180. if ( strstr( $translatedString, '%' ) === false )
  181. {
  182. return $translatedString;
  183. }
  184. // So we do have a possibility of a parameterized string, replace those
  185. // with the parameters. The callback function can actually throw an
  186. // exception to tell that there was a missing parameter.
  187. return (string) preg_replace( '@%(([A-Za-z][a-z_]*[a-z])|[1-9])@e', '$this->parameterCallbackCompile("\\1", $params)', $translatedString );
  188. }
  189. }
  190. ?>