PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/common/ezpi18n.php

https://github.com/Yannix/ezpublish
PHP | 146 lines | 70 code | 12 blank | 64 comment | 16 complexity | ce7a0a8f65438ff7727abfb4e40269a0 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezpI18n class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. class ezpI18n
  11. {
  12. /**
  13. * Indicates if text translation is enabled or not.
  14. * @see ezpI18n::isEnabled()
  15. *
  16. * @var null|bool
  17. */
  18. protected static $isEnabled = null;
  19. /**
  20. * Replaces keys found in \a $text with values in \a $arguments.
  21. * If \a $arguments is an associative array it will use the argument
  22. * keys as replacement keys. If not it will convert the index to
  23. * a key looking like %n, where n is a number between 1 and 9.
  24. *
  25. * @param string $string
  26. * @param array $arguments
  27. * @return string
  28. */
  29. protected static function insertArguments( $text, $arguments )
  30. {
  31. if ( is_array( $arguments ) )
  32. {
  33. $replaceList = array();
  34. foreach ( $arguments as $argumentKey => $argumentItem )
  35. {
  36. if ( is_int( $argumentKey ) )
  37. $replaceList['%' . ( ($argumentKey%9) + 1 )] = $argumentItem;
  38. else
  39. $replaceList[$argumentKey] = $argumentItem;
  40. }
  41. $text = strtr( $text, $replaceList );
  42. }
  43. return $text;
  44. }
  45. /**
  46. * Enabled if the site.ini settings RegionalSettings/TextTranslation is set to disabled
  47. *
  48. * @return bool
  49. */
  50. protected static function isEnabled()
  51. {
  52. if ( self::$isEnabled === null )
  53. {
  54. $ini = eZINI::instance();
  55. $useTextTranslation = $ini->variable( 'RegionalSettings', 'TextTranslation' ) != 'disabled';
  56. self::$isEnabled = $useTextTranslation || eZTranslatorManager::dynamicTranslationsEnabled();
  57. }
  58. return self::$isEnabled;
  59. }
  60. /**
  61. * Resets the state ezpI18n class.
  62. */
  63. public static function reset()
  64. {
  65. self::$isEnabled = null;
  66. }
  67. /**
  68. * Translates the source \a $source with context \a $context and optional comment \a $comment
  69. * and returns the translation if translations are enabled.
  70. * Uses {@link ezpI18n::translateText()}
  71. *
  72. * Example:
  73. * translate( 'content/view', 'There are %count nodes in this list out of %total total nodes.', 'Children view of nodes for whole site', array( '%count' => $c, '%total' => $t ) );
  74. *
  75. * @param string $context
  76. * @param string $source
  77. * @param string|null $comment
  78. * @param array|null $arguments
  79. * @return string
  80. */
  81. public static function tr( $context, $source, $comment = null, $arguments = null )
  82. {
  83. if ( self::isEnabled() )
  84. {
  85. return self::translateText( $context, $source, $comment, $arguments );
  86. }
  87. return self::insertArguments( $source, $arguments );
  88. }
  89. /**
  90. * Translates the source \a $source with context \a $context and optional comment \a $comment
  91. * and returns the translation if locale code is not eng-GB.
  92. * Uses {@link eZTranslatorMananger::translate()} to do the actual translation
  93. *
  94. * Example:
  95. * translateText( 'content/view', 'There are %count nodes in this list out of %total total nodes.', 'Children view of nodes for whole site', array( '%count' => $c, '%total' => $t ) );
  96. *
  97. * @param string $context
  98. * @param string $source
  99. * @param string|null $comment
  100. * @param array|null $arguments
  101. * @return string
  102. */
  103. protected static function translateText( $context, $source, $comment = null, $arguments = null )
  104. {
  105. $localeCode = eZLocale::instance()->localeFullCode();
  106. if ( $localeCode == 'eng-GB' )
  107. {
  108. // we don't have ts-file for 'eng-GB'.
  109. return self::insertArguments( $source, $arguments );
  110. }
  111. $ini = eZINI::instance();
  112. $useCache = $ini->variable( 'RegionalSettings', 'TranslationCache' ) != 'disabled';
  113. eZTSTranslator::initialize( $context, $localeCode, 'translation.ts', $useCache );
  114. // Bork translation: Makes it easy to see what is not translated.
  115. // If no translation is found in the eZTSTranslator, a Bork translation will be returned.
  116. // Bork is different than, but similar to, eng-GB, and is enclosed in square brackets [].
  117. $developmentMode = $ini->variable( 'RegionalSettings', 'DevelopmentMode' ) != 'disabled';
  118. if ( $developmentMode )
  119. {
  120. eZBorkTranslator::initialize();
  121. }
  122. $man = eZTranslatorManager::instance();
  123. $trans = $man->translate( $context, $source, $comment );
  124. if ( $trans !== null ) {
  125. return self::insertArguments( $trans, $arguments );
  126. }
  127. if ( $comment != null and strlen( $comment ) > 0 )
  128. eZDebug::writeDebug( "Missing translation for message in context: '$context' with comment: '$comment'. The untranslated message is: '$source'", __METHOD__ );
  129. else
  130. eZDebug::writeDebug( "Missing translation for message in context: '$context'. The untranslated message is: '$source'", __METHOD__ );
  131. return self::insertArguments( $source, $arguments );
  132. }
  133. }
  134. ?>