|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the ezpI18n class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package kernel 00009 */ 00010 00011 class ezpI18n 00012 { 00013 /** 00014 * Indicates if text translation is enabled or not. 00015 * @see ezpI18n::isEnabled() 00016 * 00017 * @var null|bool 00018 */ 00019 protected static $isEnabled = null; 00020 00021 /** 00022 * Replaces keys found in \a $text with values in \a $arguments. 00023 * If \a $arguments is an associative array it will use the argument 00024 * keys as replacement keys. If not it will convert the index to 00025 * a key looking like %n, where n is a number between 1 and 9. 00026 * 00027 * @param string $string 00028 * @param array $arguments 00029 * @return string 00030 */ 00031 protected static function insertArguments( $text, $arguments ) 00032 { 00033 if ( is_array( $arguments ) ) 00034 { 00035 $replaceList = array(); 00036 foreach ( $arguments as $argumentKey => $argumentItem ) 00037 { 00038 if ( is_int( $argumentKey ) ) 00039 $replaceList['%' . ( ($argumentKey%9) + 1 )] = $argumentItem; 00040 else 00041 $replaceList[$argumentKey] = $argumentItem; 00042 } 00043 $text = strtr( $text, $replaceList ); 00044 } 00045 return $text; 00046 } 00047 00048 /** 00049 * Enabled if the site.ini settings RegionalSettings/TextTranslation is set to disabled 00050 * 00051 * @return bool 00052 */ 00053 protected static function isEnabled() 00054 { 00055 if ( self::$isEnabled === null ) 00056 { 00057 $ini = eZINI::instance(); 00058 $useTextTranslation = $ini->variable( 'RegionalSettings', 'TextTranslation' ) != 'disabled'; 00059 self::$isEnabled = $useTextTranslation || eZTranslatorManager::dynamicTranslationsEnabled(); 00060 } 00061 return self::$isEnabled; 00062 } 00063 00064 /** 00065 * Resets the state ezpI18n class. 00066 */ 00067 public static function reset() 00068 { 00069 self::$isEnabled = null; 00070 } 00071 00072 /** 00073 * Translates the source \a $source with context \a $context and optional comment \a $comment 00074 * and returns the translation if translations are enabled. 00075 * Uses {@link ezpI18n::translateText()} 00076 * 00077 * Example: 00078 * 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 ) ); 00079 * 00080 * @param string $context 00081 * @param string $source 00082 * @param string|null $comment 00083 * @param array|null $arguments 00084 * @return string 00085 */ 00086 public static function tr( $context, $source, $comment = null, $arguments = null ) 00087 { 00088 if ( self::isEnabled() ) 00089 { 00090 return self::translateText( $context, $source, $comment, $arguments ); 00091 } 00092 return self::insertArguments( $source, $arguments ); 00093 } 00094 00095 /** 00096 * Translates the source \a $source with context \a $context and optional comment \a $comment 00097 * and returns the translation if locale code is not eng-GB. 00098 * Uses {@link eZTranslatorMananger::translate()} to do the actual translation 00099 * 00100 * Example: 00101 * 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 ) ); 00102 * 00103 * @param string $context 00104 * @param string $source 00105 * @param string|null $comment 00106 * @param array|null $arguments 00107 * @return string 00108 */ 00109 protected static function translateText( $context, $source, $comment = null, $arguments = null ) 00110 { 00111 $localeCode = eZLocale::instance()->localeFullCode(); 00112 if ( $localeCode == 'eng-GB' ) 00113 { 00114 // we don't have ts-file for 'eng-GB'. 00115 return self::insertArguments( $source, $arguments ); 00116 } 00117 00118 $ini = eZINI::instance(); 00119 $useCache = $ini->variable( 'RegionalSettings', 'TranslationCache' ) != 'disabled'; 00120 eZTSTranslator::initialize( $context, $localeCode, 'translation.ts', $useCache ); 00121 00122 // Bork translation: Makes it easy to see what is not translated. 00123 // If no translation is found in the eZTSTranslator, a Bork translation will be returned. 00124 // Bork is different than, but similar to, eng-GB, and is enclosed in square brackets []. 00125 $developmentMode = $ini->variable( 'RegionalSettings', 'DevelopmentMode' ) != 'disabled'; 00126 if ( $developmentMode ) 00127 { 00128 eZBorkTranslator::initialize(); 00129 } 00130 00131 $man = eZTranslatorManager::instance(); 00132 $trans = $man->translate( $context, $source, $comment ); 00133 if ( $trans !== null ) { 00134 return self::insertArguments( $trans, $arguments ); 00135 } 00136 00137 if ( $comment != null and strlen( $comment ) > 0 ) 00138 eZDebug::writeDebug( "Missing translation for message in context: '$context' with comment: '$comment'. The untranslated message is: '$source'", __METHOD__ ); 00139 else 00140 eZDebug::writeDebug( "Missing translation for message in context: '$context'. The untranslated message is: '$source'", __METHOD__ ); 00141 00142 return self::insertArguments( $source, $arguments ); 00143 } 00144 } 00145 00146 ?>