eZ Publish  [4.0]
i18n.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <06-Jul-2003 15:52:54 amos>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 //include_once( 'lib/ezutils/classes/ezini.php' );
00030 
00031 /*!
00032  \return the current language used.
00033 */
00034 function ezcurrentLanguage()
00035 {
00036     //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00037     $locale = eZLocale::instance();
00038     return $locale->translationCode();
00039 }
00040 
00041 /*!
00042  Replaces keys found in \a $text with values in \a $arguments.
00043  If \a $arguments is an associative array it will use the argument
00044  keys as replacement keys. If not it will convert the index to
00045  a key looking like %n, where n is a number between 1 and 9.
00046  Returns the new string.
00047 */
00048 function ezinsertarguments( $text, $arguments )
00049 {
00050     if ( is_array( $arguments ) )
00051     {
00052         $replaceList = array();
00053         foreach ( $arguments as $argumentKey => $argumentItem )
00054         {
00055             if ( is_int( $argumentKey ) )
00056                 $replaceList['%' . ( ($argumentKey%9) + 1 )] = $argumentItem;
00057             else
00058                 $replaceList[$argumentKey] = $argumentItem;
00059         }
00060         $text = strtr( $text, $replaceList );
00061     }
00062     return $text;
00063 }
00064 
00065 /*!
00066  Translates the source \a $source with context \a $context and optional comment \a $comment
00067  and returns the translation.
00068  Uses eZTranslatorMananger::translate() to do the actual translation.
00069 
00070  If the site.ini settings RegionalSettings/TextTranslation is set to disabled this function
00071  will only return the source text.
00072 */
00073 $ini = eZINI::instance();
00074 $useTextTranslation = false;
00075 $hasFallback = false;
00076 if ( $ini->variable( 'RegionalSettings', 'TextTranslation' ) != 'disabled' )
00077 {
00078     $language = ezcurrentLanguage();
00079     $iniI18N = eZINI::instance( "i18n.ini" );
00080     $fallbacks = $iniI18N->variable( 'TranslationSettings', 'FallbackLanguages' );
00081 
00082     //include_once( 'lib/ezutils/classes/ezextension.php' );
00083     $extensionBase = eZExtension::baseDirectory();
00084     $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' );
00085 
00086     if ( array_key_exists( $language,  $fallbacks ) and $fallbacks[$language] )
00087     {
00088         if ( file_exists( 'share/translations/' . $fallbacks[$language] . '/translation.ts' ) )
00089         {
00090             $hasFallback = true;
00091         }
00092         else
00093         {
00094             foreach ( $translationExtensions as $translationExtension )
00095             {
00096                 $extensionPath = $extensionBase . '/' . $translationExtension . '/translations/' . $fallbacks[$language] . '/translation.ts';
00097                 if ( file_exists( $extensionPath ) )
00098                 {
00099                     $hasFallback = true;
00100                     break;
00101                 }
00102             }
00103         }
00104     }
00105     if ( file_exists( 'share/translations/' . $language . '/translation.ts' ) || $hasFallback )
00106     {
00107         $useTextTranslation = true;
00108     }
00109     else
00110     {
00111         foreach ( $translationExtensions as $translationExtension )
00112         {
00113             $extensionPath = $extensionBase . '/' . $translationExtension . '/translations/' . $language . '/translation.ts';
00114             if ( file_exists( $extensionPath ) )
00115             {
00116                 $useTextTranslation = true;
00117                 break;
00118             }
00119         }
00120     }
00121 }
00122 
00123 //include_once( 'lib/ezi18n/classes/eztranslatormanager.php' );
00124 
00125 if ( $useTextTranslation || eZTranslatorManager::dynamicTranslationsEnabled() )
00126 {
00127     //include_once( 'lib/ezi18n/classes/eztstranslator.php' );
00128 
00129     function ezi18n( $context, $source, $comment = null, $arguments = null )
00130     {
00131         return eZTranslateText( $context, $source, $comment, $arguments );
00132     }
00133 
00134     function ezx18n( $extension, $context, $source, $comment = null, $arguments = null )
00135     {
00136         return eZTranslateText( $context, $source, $comment, $arguments );
00137     }
00138 
00139     function eZTranslateText( $context, $source, $comment = null, $arguments = null )
00140     {
00141         $ini = eZINI::instance();
00142         if ( $ini->variable( 'RegionalSettings', 'Locale' ) == 'eng-GB' )
00143         {
00144             // we don't have ts-file for 'eng-GB'.
00145             // NOTE: don't remove this 'if'. it's needed to support dynamic switch between translations.
00146             return ezinsertarguments( $source, $arguments );
00147         }
00148 
00149         $language = ezcurrentLanguage();
00150 
00151         $file = 'translation.ts';
00152 
00153         // translation.ts translation
00154         $useCache = $ini->variable( 'RegionalSettings', 'TranslationCache' ) != 'disabled';
00155         eZTSTranslator::initialize( $context, $language, $file, $useCache );
00156 
00157         // Bork translation: Makes it easy to see what is not translated.
00158         // If no translation is found in the eZTSTranslator, a Bork translation will be returned.
00159         // Bork is different than, but similar to, eng-GB, and is enclosed in square brackets [].
00160         $developmentMode = $ini->variable( 'RegionalSettings', 'DevelopmentMode' ) != 'disabled';
00161         if ( $developmentMode )
00162         {
00163             //include_once( 'lib/ezi18n/classes/ezborktranslator.php' );
00164             eZBorkTranslator::initialize();
00165         }
00166 
00167         $man = eZTranslatorManager::instance();
00168         $trans = $man->translate( $context, $source, $comment );
00169         if ( $trans !== null ) {
00170             return ezinsertarguments( $trans, $arguments );
00171         }
00172 
00173         if ( $comment != null and strlen( $comment ) > 0 )
00174             eZDebug::writeDebug( "Missing translation for message in context: '$context' with comment: '$comment'. The untranslated message is: '$source'", "ezi18n" );
00175         else
00176             eZDebug::writeDebug( "Missing translation for message in context: '$context'. The untranslated message is: '$source'", "ezi18n" );
00177 
00178         return ezinsertarguments( $source, $arguments );
00179     }
00180 }
00181 else
00182 {
00183     function ezi18n( $context, $source, $comment = null, $arguments = null )
00184     {
00185         return ezinsertarguments( $source, $arguments );
00186     }
00187 
00188     function ezx18n( $extension, $context, $source, $comment = null, $arguments = null )
00189     {
00190         return ezinsertarguments( $source, $arguments );
00191     }
00192 }
00193 
00194 ?>