eZ Publish  [4.0]
ezi18noperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZi18nOperator class
00004 //
00005 // Created on: <18-Apr-2002 12:15:07 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 //!! eZKernel
00032 //! The class eZi18nOperator does
00033 /*!
00034 
00035 */
00036 
00037 //include_once( 'lib/ezi18n/classes/eztranslatormanager.php' );
00038 //include_once( 'lib/ezi18n/classes/eztstranslator.php' );
00039 
00040 class eZi18nOperator
00041 {
00042     /*!
00043     */
00044     function eZi18nOperator( $name = 'i18n', $extensionName = 'x18n' )
00045     {
00046         $this->Operators = array( $name, $extensionName );
00047         $this->Name = $name;
00048         $this->ExtensionName = $extensionName;
00049     }
00050 
00051     /*!
00052      Returns the operators in this class.
00053     */
00054     function operatorList()
00055     {
00056         return $this->Operators;
00057     }
00058 
00059     /*!
00060      \return true to tell the template engine that the parameter list exists per operator type.
00061     */
00062     function namedParameterPerOperator()
00063     {
00064         return true;
00065     }
00066 
00067     /*!
00068      See eZTemplateOperator::namedParameterList()
00069     */
00070     function namedParameterList()
00071     {
00072         return array( $this->Name => array( 'context' => array( 'type' => 'string',
00073                                                                 'required' => false,
00074                                                                 'default' => false ),
00075                                             'comment' => array( 'type' => 'string',
00076                                                                 'required' => false,
00077                                                                 'default' => '' ),
00078                                             'arguments' => array( 'type' => 'hash',
00079                                                                   'required' => false,
00080                                                                   'default' => false ) ),
00081                       $this->ExtensionName => array( 'extension' => array( 'type' => 'string',
00082                                                                            'required' => true,
00083                                                                            'default' => false ),
00084                                                      'context' => array( 'type' => 'string',
00085                                                                          'required' => false,
00086                                                                          'default' => false ),
00087                                                      'comment' => array( 'type' => 'string',
00088                                                                          'required' => false,
00089                                                                          'default' => '' ),
00090                                                      'arguments' => array( 'type' => 'hash',
00091                                                                            'required' => false,
00092                                                                            'default' => false ) ) );
00093     }
00094 
00095     function operatorTemplateHints()
00096     {
00097         return array( $this->Name => array( 'input' => true,
00098                                             'output' => true,
00099                                             'parameters' => true,
00100                                             'element-transformation' => true,
00101                                             'transform-parameters' => true,
00102                                             'input-as-parameter' => 'always',
00103                                             'element-transformation-func' => 'i18nTrans') );
00104     }
00105 
00106     function i18nTrans( $operatorName, &$node, $tpl, &$resourceData,
00107                         $element, $lastElement, $elementList, $elementTree, &$parameters )
00108     {
00109         // i18n( $input, $context, $comment, $arguments )
00110         // Check if if the three first parameters are constants, if not we cannot compile it
00111         foreach ( array_slice( $parameters, 0, 3 ) as $parameter )
00112         {
00113             if ( $parameter !== null &&
00114                  !eZTemplateNodeTool::isConstantElement( $parameter ) )
00115             {
00116                 return false;
00117             }
00118         }
00119 
00120         require_once( 'kernel/common/i18n.php' );
00121         $value = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
00122 
00123         $numParameters = count ( $parameters );
00124         $context = ( $numParameters > 1 ) ? eZTemplateNodeTool::elementStaticValue( $parameters[1] ) : null;
00125         $comment = ( $numParameters > 2 ) ? eZTemplateNodeTool::elementStaticValue( $parameters[2] ) : null;
00126 
00127         if ( $numParameters < 4 )
00128         {
00129             return array ( eZTemplateNodeTool::createStringElement( ezi18n( $context, $value, $comment, null ) ) );
00130         }
00131 
00132         $values = array();
00133 
00134         $ini = eZINI::instance();
00135         if ( $ini->variable( 'RegionalSettings', 'TextTranslation' ) != 'disabled' )
00136         {
00137             $language = ezcurrentLanguage();
00138             if ( $language != "eng-GB" ) // eng-GB does not need translation
00139             {
00140                 $file = 'translation.ts';
00141                 $ini = eZINI::instance();
00142                 $useCache = $ini->variable( 'RegionalSettings', 'TranslationCache' ) != 'disabled';
00143                 eZTSTranslator::initialize( $context, $language, $file, $useCache );
00144 
00145                 $man = eZTranslatorManager::instance();
00146                 $newValue = $man->translate( $context, $value, $comment );
00147                 if ( $newValue )
00148                 {
00149                     $value = $newValue;
00150                 }
00151             }
00152         }
00153 
00154         $values[] = array( eZTemplateNodeTool::createStringElement( $value ) );
00155         $values[] = $parameters[3];
00156 
00157         $code = '%tmp1% = array();' . "\n" .
00158              'foreach ( %2% as %tmp2% => %tmp3% )' . "\n" .
00159              '{' . "\n" .
00160              '  if ( is_int( %tmp2% ) )' . "\n" .
00161              '    %tmp1%[\'%\' . ( (%tmp2%%9) + 1 )] = %tmp3%;' . "\n" .
00162              '  else' . "\n" .
00163              '    %tmp1%[%tmp2%] = %tmp3%;' . "\n" .
00164              '}' . "\n" .
00165              '%output% = strtr( %1%, %tmp1% );' . "\n";
00166 
00167         return array( eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 3 ) );
00168     }
00169 
00170     /*!
00171      \reimp
00172     */
00173     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$value, $namedParameters )
00174     {
00175         require_once( 'kernel/common/i18n.php' );
00176         switch ( $operatorName )
00177         {
00178             case $this->Name:
00179             {
00180                 $context = $namedParameters['context'];
00181                 $comment = $namedParameters['comment'];
00182                 $arguments = $namedParameters['arguments'];
00183                 $value = ezi18n( $context, $value, $comment, $arguments );
00184             } break;
00185             case $this->ExtensionName:
00186             {
00187                 $extension = $namedParameters['extension'];
00188                 $context = $namedParameters['context'];
00189                 $comment = $namedParameters['comment'];
00190                 $arguments = $namedParameters['arguments'];
00191                 $value = ezx18n( $extension, $context, $value, $comment, $arguments );
00192             } break;
00193         }
00194     }
00195 
00196     /// \privatesection
00197     public $Operators;
00198     public $Name;
00199     public $ExtensionName;
00200 };
00201 
00202 ?>