lang.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains the TYPO3 Backend Language class
00029  *
00030  * $Id: lang.php 4433 2008-11-07 04:13:12Z flyguide $
00031  * Revised for TYPO3 3.6.0
00032  *
00033  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00034  */
00035 /**
00036  * [CLASS/FUNCTION INDEX of SCRIPT]
00037  *
00038  *
00039  *
00040  *   88: class language
00041  *  138:     function init($lang,$altPath='')
00042  *  183:     function addModuleLabels($arr,$prefix)
00043  *  209:     function hscAndCharConv($lStr,$hsc)
00044  *  224:     function makeEntities($str)
00045  *  241:     function JScharCode($str)
00046  *  260:     function getLL($index,$hsc=0)
00047  *  278:     function getLLL($index,$LOCAL_LANG,$hsc=0)
00048  *  299:     function sL($input,$hsc=0)
00049  *  344:     function loadSingleTableDescription($table)
00050  *  396:     function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0)
00051  *  441:     function readLLfile($fileRef)
00052  *  451:     function localizedFileRef($fileRef)
00053  *
00054  * TOTAL FUNCTIONS: 12
00055  * (This index is automatically created/updated by the extension "extdeveval")
00056  *
00057  */
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 /**
00075  * Contains the TYPO3 Backend Language class
00076  *
00077  * For detailed information about how localization is handled,
00078  * please refer to the 'Inside TYPO3' document which descibes this.
00079  *
00080  * This class is normally instantiated as the global variable $LANG in typo3/template.php
00081  * It's only available in the backend and under certain circumstances in the frontend
00082  *
00083  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00084  * @package TYPO3
00085  * @subpackage core
00086  * @see typo3/template.php, template
00087  */
00088 class language {
00089     var $lang='default';        // This is set to the language that is currently running for the user
00090     var $langSplit='default';   // Values like the labels in the tables.php-document are split by '|'. This values defines which language is represented by which position in the resulting array after splitting a value. (NOTICE: Obsolete concept!)
00091 
00092         // Default charset in backend
00093     var $charSet = 'iso-8859-1';
00094 
00095         // Array with alternative charsets for other languages. (Moved to t3lib_cs, Set from csConvObj!)
00096     var $charSetArray = array();
00097 
00098         // This is the url to the TYPO3 manual
00099     var $typo3_help_url= 'http://www.typo3.com/man_uk/';
00100         // Array with alternative URLs based on language.
00101     var $helpUrlArray = array(
00102         'dk' => 'http://www.typo3.com/man_dk/',
00103     );
00104 
00105     var $debugKey = FALSE;      // If true, will show the key/location of labels in the backend.
00106 
00107 
00108     var $moduleLabels = Array();    // Can contain labels and image references from the backend modules. Relies on t3lib_loadmodules to initialize modules after a global instance of $LANG has been created.
00109 
00110         // Internal
00111     var $langSplitIndex=0;          // Points to the position of the current language key as found in constant TYPO3_languages
00112     var $LL_files_cache=array();    // Internal cache for read LL-files
00113     var $LL_labels_cache=array();   // Internal cache for ll-labels (filled as labels are requested)
00114 
00115     /**
00116      * instance of the "t3lib_cs" class. May be used by any application.
00117      *
00118      * @var t3lib_cs
00119      */
00120     var $csConvObj;
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129     /**
00130      * Initializes the backend language.
00131      * This is for example done in typo3/template.php with lines like these:
00132      *
00133      * require (PATH_typo3.'sysext/lang/lang.php');
00134      * $LANG = t3lib_div::makeInstance('language');
00135      * $LANG->init($BE_USER->uc['lang']);
00136      *
00137      * @param   string      The language key (two character string from backend users profile)
00138      * @param   string      IGNORE. Not used.
00139      * @return  void
00140      */
00141     function init($lang,$altPath='')    {
00142 
00143             // Initialize the conversion object:
00144         $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
00145         $this->charSetArray = $this->csConvObj->charSetArray;
00146 
00147             // Internally setting the list of TYPO3 backend languages.
00148         $this->langSplit=TYPO3_languages;
00149 
00150             // Finding the requested language in this list based on the $lang key being inputted to this function.
00151         $ls = explode('|',$this->langSplit);
00152         while(list($i,$v)=each($ls))    {
00153             if ($v==$lang)  {   // Language is found. Configure it:
00154                 $this->langSplitIndex=$i;       // The index of the language as found in the TYPO3_languages list
00155                 $this->lang = $lang;            // The current language key
00156                 if ($this->helpUrlArray[$this->lang])   $this->typo3_help_url=$this->helpUrlArray[$this->lang]; // The help URL if different from the default.
00157                 if ($this->charSetArray[$this->lang])   $this->charSet=$this->charSetArray[$this->lang];        // The charset if different from the default.
00158             }
00159         }
00160 
00161             // If a forced charset is used and different from the charset otherwise used:
00162         if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']!=$this->charSet) {
00163                 // Set the forced charset:
00164             $this->charSet = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
00165 
00166             if ($this->charSet!='utf-8' && !$this->csConvObj->initCharset($this->charSet))  {
00167                 t3lib_BEfunc::typo3PrintError ('The forced character set "'.$this->charSet.'" was not found in t3lib/csconvtbl/','Forced charset not found');
00168                 exit;
00169             }
00170         }
00171     }
00172 
00173     /**
00174      * Adds labels and image references from the backend modules to the internal moduleLabels array
00175      *
00176      * @param   array       Array with references to module labels, keys: ['labels']['tablabel'], ['labels']['tabdescr'], ['tabs']['tab']
00177      * @param   string      Module name prefix
00178      * @return  void
00179      * @see t3lib_loadModules
00180      */
00181     function addModuleLabels($arr,$prefix)  {
00182         if (is_array($arr)) {
00183             reset($arr);
00184             while(list($k,$larr)=each($arr))    {
00185                 if (!isset($this->moduleLabels[$k]))    {
00186                     $this->moduleLabels[$k]=array();
00187                 }
00188                 if (is_array($larr))    {
00189                     reset($larr);
00190                     while(list($l,$v)=each($larr))  {
00191                         $this->moduleLabels[$k][$prefix.$l]=$v;
00192                     }
00193                 }
00194             }
00195         }
00196     }
00197 
00198     /**
00199      * Will htmlspecialchar() the input string and before that any charset conversion will also have taken place if needed (see init())
00200      * Used to pipe language labels through just before they are returned.
00201      *
00202      * @param   string      The string to process
00203      * @param   boolean     If set, then the string is htmlspecialchars()'ed
00204      * @return  string      The processed string
00205      * @see init()
00206      */
00207     function hscAndCharConv($lStr,$hsc) {
00208         $lStr = $hsc ? htmlspecialchars($lStr) : $lStr;
00209 
00210         // labels returned from a locallang file used to be in the language of the charset. Since TYPO3 4.1 they are always in the charset of the BE.
00211 
00212         return $lStr;
00213     }
00214 
00215     /**
00216      * Will convert the input strings special chars (all above 127) to entities. The string is expected to be encoded in the charset, $this->charSet
00217      * This function is used to create strings that can be used in the Click Menu (Context Sensitive Menus). The reason is that the values that are dynamically written into the <div> layer is decoded as iso-8859-1 no matter what charset is used in the document otherwise (only MSIE, Mozilla is OK). So by converting we by-pass this problem.
00218      *
00219      * @param   string      Input string
00220      * @return  string      Output string
00221      */
00222     function makeEntities($str) {
00223             // Convert string to UTF-8:
00224         if ($this->charSet!='utf-8')    $str = $this->csConvObj->utf8_encode($str,$this->charSet);
00225 
00226             // Convert string back again, but using the full entity conversion:
00227         $str = $this->csConvObj->utf8_to_entities($str);
00228         return $str;
00229     }
00230 
00231     /**
00232      * Converts the input string to a JavaScript function returning the same string, but charset-safe.
00233      * Used for confirm and alert boxes where we must make sure that any string content does not break the script AND want to make sure the charset is preserved.
00234      * Originally I used the JS function unescape() in combination with PHP function rawurlencode() in order to pass strings in a safe way. This could still be done for iso-8859-1 charsets but now I have applied the same method here for all charsets.
00235      *
00236      * @param   string      Input string, encoded with $this->charSet
00237      * @return  string      Output string, a JavaScript function: "String.fromCharCode(......)"
00238      */
00239     function JScharCode($str)   {
00240 
00241             // Convert string to UTF-8:
00242         if ($this->charSet!='utf-8')    $str = $this->csConvObj->utf8_encode($str,$this->charSet);
00243 
00244             // Convert the UTF-8 string into a array of char numbers:
00245         $nArr = $this->csConvObj->utf8_to_numberarray($str);
00246 
00247         return 'String.fromCharCode('.implode(',',$nArr).')';
00248     }
00249 
00250     /**
00251      * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
00252      * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
00253      *
00254      * @param   string      Label key
00255      * @param   boolean     If set, the return value is htmlspecialchar'ed
00256      * @return  string
00257      */
00258     function getLL($index,$hsc=0)   {
00259         // Get Local Language
00260         if (strcmp($GLOBALS['LOCAL_LANG'][$this->lang][$index],'')) {
00261             $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index], $hsc); // Returns local label if not blank.
00262         } else {
00263             $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index], $hsc);   // Returns default label
00264         }
00265         return $output.($this->debugKey ? ' ['.$index.']':'');
00266     }
00267 
00268     /**
00269      * Works like ->getLL() but takes the $LOCAL_LANG array used as the second argument instead of using the global array.
00270      *
00271      * @param   string      Label key
00272      * @param   array       $LOCAL_LANG array to get label key from
00273      * @param   boolean     If set, the return value is htmlspecialchar'ed
00274      * @return  string
00275      */
00276     function getLLL($index,$LOCAL_LANG,$hsc=0)  {
00277         // Get Local Language
00278         if (strcmp($LOCAL_LANG[$this->lang][$index],''))    {
00279             $output = $this->hscAndCharConv($LOCAL_LANG[$this->lang][$index], $hsc);    // Returns local label if not blank.
00280         } else {
00281             $output = $this->hscAndCharConv($LOCAL_LANG['default'][$index], $hsc);      // Returns default label
00282         }
00283         return $output.($this->debugKey ? ' ['.$index.']':'');
00284     }
00285 
00286     /**
00287      * splitLabel function
00288      * Historically labels were exploded by '|' and each part would correspond to the translation of the language found at the same 'index' in the TYPO3_languages constant.
00289      * Today all translations are based on $LOCAL_LANG variables. 'language-splitted' labels can therefore refer to a local-lang file + index instead!
00290      * It's highly recommended to use the 'local_lang' method (and thereby it's highly deprecated to use 'language-splitted' label strings)
00291      * Refer to 'Inside TYPO3' for more details
00292      *
00293      * @param   string      Label key/reference
00294      * @param   boolean     If set, the return value is htmlspecialchar'ed
00295      * @return  string
00296      */
00297     function sL($input,$hsc=0)  {
00298         if (strcmp(substr($input,0,4),'LLL:'))  {   // Using obsolete 'language-splitted' labels:
00299             $t = explode('|',$input);
00300             $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
00301             return $this->hscAndCharConv($out, $hsc);
00302         } else {    // LOCAL_LANG:
00303             if (!isset($this->LL_labels_cache[$this->lang][$input])) {  // If cached label
00304                 $restStr = trim(substr($input,4));
00305                 $extPrfx='';
00306                 if (!strcmp(substr($restStr,0,4),'EXT:'))   {   // ll-file refered to is found in an extension.
00307                     $restStr = trim(substr($restStr,4));
00308                     $extPrfx='EXT:';
00309                 }
00310                 $parts = explode(':',$restStr);
00311                 $parts[0]=$extPrfx.$parts[0];
00312                 if (!isset($this->LL_files_cache[$parts[0]]))   {   // Getting data if not cached
00313                     $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
00314 
00315                         // If the current language is found in another file, load that as well:
00316                     $lFileRef = $this->localizedFileRef($parts[0]);
00317                     if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang]) && $this->LL_files_cache[$parts[0]][$this->lang]=='EXT')  {
00318                         $tempLL = $this->readLLfile($lFileRef);
00319                         $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
00320                     }
00321 
00322                         // Overriding file?
00323                     if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
00324                         $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
00325                         $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]],$ORarray);
00326                     }
00327                 }
00328                 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1],$this->LL_files_cache[$parts[0]]);
00329             }
00330             $output = $hsc ? t3lib_div::deHSCentities(htmlspecialchars($this->LL_labels_cache[$this->lang][$input])) : $this->LL_labels_cache[$this->lang][$input]; // For the cached output charset conversion has already happend! So perform HSC right here.
00331             return $output.($this->debugKey ? ' ['.$input.']':'');
00332         }
00333     }
00334 
00335     /**
00336      * Loading $TCA_DESCR[$table]['columns'] with content from locallang files as defined in $TCA_DESCR[$table]['refs']
00337      * $TCA_DESCR is a global var
00338      *
00339      * @param   string      Table name found as key in global array $TCA_DESCR
00340      * @return  void
00341      */
00342     function loadSingleTableDescription($table) {
00343         global $TCA_DESCR;
00344 
00345         if (is_array($TCA_DESCR[$table])
00346                 && !isset($TCA_DESCR[$table]['columns'])
00347                 && is_array($TCA_DESCR[$table]['refs']))     {  // First the 'table' cannot already be loaded in [columns] and secondly there must be a references to locallang files available in [refs]
00348 
00349                 // Init $TCA_DESCR for $table-key
00350             $TCA_DESCR[$table]['columns']=array();
00351 
00352                 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
00353             foreach ($TCA_DESCR[$table]['refs'] as $llfile) {
00354                 $LOCAL_LANG = $this->includeLLFile($llfile,0,1);
00355 
00356                     // Traverse all keys
00357                 if (is_array($LOCAL_LANG['default']))   {
00358                     foreach($LOCAL_LANG['default'] as $lkey => $lVal)   {
00359 
00360                             // exploding by '.':
00361                             // 0 => fieldname,
00362                             // 1 => type from (alttitle,description,details,syntax,image_descr,image,seeAlso),
00363                             // 2 => special instruction, see switch construct
00364                         $kParts = explode('.',$lkey);
00365 
00366                             // Detecting 'hidden' labels, converting to normal fieldname
00367                         if ($kParts[0]=='_')    $kParts[0]='';
00368                         if (substr($kParts[0],0,1)=='_')    { $kParts[0] = substr($kParts[0],1); }
00369 
00370                             // Add label:
00371                         switch((string)$kParts[2])  {
00372                             case '+':   // adding
00373                                 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]].= chr(10).$lVal;
00374                             break;
00375                             default:    // Substituting:
00376                                 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]] = $lVal;
00377                             break;
00378                         }
00379                     }
00380                 }
00381             }
00382         }
00383     }
00384 
00385     /**
00386      * Includes locallang file (and possibly additional localized version if configured for)
00387      * Read language labels will be merged with $LOCAL_LANG (if $setGlobal=1).
00388      *
00389      * @param   string      $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
00390      * @param   boolean     Setting in global variable $LOCAL_LANG (or returning the variable)
00391      * @param   boolean     If $mergeLocalOntoDefault is set the local part of the $LOCAL_LANG array is merged onto the default part (if the local part exists) and the local part is unset.
00392      * @return  mixed       If $setGlobal is true the LL-files will set the $LOCAL_LANG in the global scope. Otherwise the $LOCAL_LANG array is returned from function
00393      */
00394     function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0)  {
00395             // Configure for global flag:
00396         if ($setGlobal) {
00397             global $LOCAL_LANG;
00398         }
00399 
00400             // Get default file:
00401         $llang = $this->readLLfile($fileRef);
00402 
00403         if (count($llang))  {
00404 
00405             $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule((array)$LOCAL_LANG,$llang);
00406 
00407                 // Localized addition?
00408             $lFileRef = $this->localizedFileRef($fileRef);
00409             if ($lFileRef && (string)$LOCAL_LANG[$this->lang]=='EXT')   {
00410                 $llang = $this->readLLfile($lFileRef);
00411                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$llang);
00412             }
00413 
00414                 // Overriding file?
00415             if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]))  {
00416                 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]);
00417                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$ORarray);
00418             }
00419 
00420                 // Merge local onto default:
00421             if ($mergeLocalOntoDefault && strcmp($this->lang,'default') && is_array($LOCAL_LANG[$this->lang]) && is_array($LOCAL_LANG['default']))  {
00422                 $LOCAL_LANG['default'] = array_merge($LOCAL_LANG['default'],$LOCAL_LANG[$this->lang]);  // array_merge can be used so far the keys are not numeric - which we assume they are not...
00423                 unset($LOCAL_LANG[$this->lang]);
00424             }
00425         }
00426 
00427             // Return value if not global is set.
00428         if (!$setGlobal)    {
00429             return $LOCAL_LANG;
00430         }
00431     }
00432 
00433     /**
00434      * Includes a locallang file and returns the $LOCAL_LANG array found inside.
00435      *
00436      * @param   string      Input is a file-reference (see t3lib_div::getFileAbsFileName) which, if exists, is included. That file is expected to be a 'local_lang' file containing a $LOCAL_LANG array.
00437      * @return  array       Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
00438      */
00439     function readLLfile($fileRef)   {
00440         return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
00441     }
00442 
00443     /**
00444      * Returns localized fileRef (.[langkey].php)
00445      *
00446      * @param   string      Filename/path of a 'locallang.php' file
00447      * @return  string      Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
00448      */
00449     function localizedFileRef($fileRef) {
00450         if ($this->lang!='default' && substr($fileRef,-4)=='.php')  {
00451             return substr($fileRef,0,-4).'.'.$this->lang.'.php';
00452         }
00453     }
00454 }
00455 
00456 
00457 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])   {
00458     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
00459 }
00460 
00461 ?>

Generated on Sat Jan 3 04:23:32 2009 for TYPO3 API by  doxygen 1.4.7