Documentation TYPO3 par Ameos

lang.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 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 ***************************************************************/
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                 // Internal charset conversion:
00116         var $origCharSet='';            // If set, then it means that the this->charSet is set to a forced, common value for the WHOLE backend regardless of user language. And THIS variable will contain the original charset for the language labels. With ->csConvObj we must then convert the original charset to the charset used in the backend from now on.
00117         var $csConvObj;                         // An instance of the "t3lib_cs" class. May be used by any application.
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00138         function init($lang,$altPath='')        {
00139 
00140                         // Initialize the conversion object:
00141                 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
00142                 $this->charSetArray = $this->csConvObj->charSetArray;
00143 
00144                         // Internally setting the list of TYPO3 backend languages.
00145                 $this->langSplit=TYPO3_languages;
00146 
00147                         // Finding the requested language in this list based on the $lang key being inputted to this function.
00148                 $ls = explode('|',$this->langSplit);
00149                 while(list($i,$v)=each($ls))    {
00150                         if ($v==$lang)  {       // Language is found. Configure it:
00151                                 $this->langSplitIndex=$i;               // The index of the language as found in the TYPO3_languages list
00152                                 $this->lang = $lang;                    // The current language key
00153                                 if ($this->helpUrlArray[$this->lang])   $this->typo3_help_url=$this->helpUrlArray[$this->lang]; // The help URL if different from the default.
00154                                 if ($this->charSetArray[$this->lang])   $this->charSet=$this->charSetArray[$this->lang];                // The charset if different from the default.
00155                         }
00156                 }
00157 
00158                         // If a forced charset is used and different from the charset otherwise used:
00159                 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']!=$this->charSet)     {
00160                                 // Set the forced charset:
00161                         $this->origCharSet = $this->charSet;
00162                         $this->charSet = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
00163 
00164                         if ($this->charSet!='utf-8' && !$this->csConvObj->initCharset($this->charSet))  {
00165                                 t3lib_BEfunc::typo3PrintError ('The forced character set "'.$this->charSet.'" was not found in t3lib/csconvtbl/','Forced charset not found');
00166                                 exit;
00167                         }
00168                         if ($this->origCharSet!='utf-8' && !$this->csConvObj->initCharset($this->origCharSet))          {
00169                                 t3lib_BEfunc::typo3PrintError ('The original character set "'.$this->origCharSet.'" was not found in t3lib/csconvtbl/','Forced charset not found');
00170                                 exit;
00171                         }
00172                 }
00173         }
00174 
00183         function addModuleLabels($arr,$prefix)  {
00184                 if (is_array($arr))     {
00185                         reset($arr);
00186                         while(list($k,$larr)=each($arr))        {
00187                                 if (!isset($this->moduleLabels[$k]))    {
00188                                         $this->moduleLabels[$k]=array();
00189                                 }
00190                                 if (is_array($larr))    {
00191                                         reset($larr);
00192                                         while(list($l,$v)=each($larr))  {
00193                                                 $this->moduleLabels[$k][$prefix.$l]=$v;
00194                                         }
00195                                 }
00196                         }
00197                 }
00198         }
00199 
00209         function hscAndCharConv($lStr,$hsc)     {
00210                 $lStr = $hsc ? htmlspecialchars($lStr) : $lStr;
00211                 if ($this->origCharSet) {
00212                         $lStr = $this->csConvObj->conv($lStr,$this->origCharSet,$this->charSet,1);
00213                 }
00214                 return $lStr;
00215         }
00216 
00224         function makeEntities($str)     {
00225                         // Convert string to UTF-8:
00226                 if ($this->charSet!='utf-8')    $str = $this->csConvObj->utf8_encode($str,$this->charSet);
00227 
00228                         // Convert string back again, but using the full entity conversion:
00229                 $str = $this->csConvObj->utf8_to_entities($str);
00230                 return $str;
00231         }
00232 
00241         function JScharCode($str)       {
00242 
00243                         // Convert string to UTF-8:
00244                 if ($this->charSet!='utf-8')    $str = $this->csConvObj->utf8_encode($str,$this->charSet);
00245 
00246                         // Convert the UTF-8 string into a array of char numbers:
00247                 $nArr = $this->csConvObj->utf8_to_numberarray($str);
00248 
00249                 return 'String.fromCharCode('.implode(',',$nArr).')';
00250         }
00251 
00260         function getLL($index,$hsc=0)   {
00261                 // Get Local Language
00262                 if (strcmp($GLOBALS['LOCAL_LANG'][$this->lang][$index],''))     {
00263                         $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index], $hsc);     // Returns local label if not blank.
00264                 } else {
00265                         $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index], $hsc);       // Returns default label
00266                 }
00267                 return $output.($this->debugKey ? ' ['.$index.']':'');
00268         }
00269 
00278         function getLLL($index,$LOCAL_LANG,$hsc=0)      {
00279                 // Get Local Language
00280                 if (strcmp($LOCAL_LANG[$this->lang][$index],''))        {
00281                         $output = $this->hscAndCharConv($LOCAL_LANG[$this->lang][$index], $hsc);        // Returns local label if not blank.
00282                 } else {
00283                         $output = $this->hscAndCharConv($LOCAL_LANG['default'][$index], $hsc);          // Returns default label
00284                 }
00285                 return $output.($this->debugKey ? ' ['.$index.']':'');
00286         }
00287 
00299         function sL($input,$hsc=0)      {
00300                 if (strcmp(substr($input,0,4),'LLL:'))  {       // Using obsolete 'language-splitted' labels:
00301                         $t = explode('|',$input);
00302                         $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
00303                         return $this->hscAndCharConv($out, $hsc);
00304                 } else {        // LOCAL_LANG:
00305                         if (!isset($this->LL_labels_cache[$this->lang][$input])) {      // If cached label
00306                                 $restStr = trim(substr($input,4));
00307                                 $extPrfx='';
00308                                 if (!strcmp(substr($restStr,0,4),'EXT:'))       {       // ll-file refered to is found in an extension.
00309                                         $restStr = trim(substr($restStr,4));
00310                                         $extPrfx='EXT:';
00311                                 }
00312                                 $parts = explode(':',$restStr);
00313                                 $parts[0]=$extPrfx.$parts[0];
00314                                 if (!isset($this->LL_files_cache[$parts[0]]))   {       // Getting data if not cached
00315                                         $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
00316 
00317                                                 // If the current language is found in another file, load that as well:
00318                                         $lFileRef = $this->localizedFileRef($parts[0]);
00319                                         if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang]) && $this->LL_files_cache[$parts[0]][$this->lang]=='EXT')      {
00320                                                 $tempLL = $this->readLLfile($lFileRef);
00321                                                 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
00322                                         }
00323 
00324                                                 // Overriding file?
00325                                         if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]))     {
00326                                                 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
00327                                                 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]],$ORarray);
00328                                         }
00329                                 }
00330                                 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1],$this->LL_files_cache[$parts[0]]);
00331                         }
00332                         $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.
00333                         return $output.($this->debugKey ? ' ['.$input.']':'');
00334                 }
00335         }
00336 
00344         function loadSingleTableDescription($table)     {
00345                 global $TCA_DESCR;
00346 
00347                 if (is_array($TCA_DESCR[$table])
00348                                 && !isset($TCA_DESCR[$table]['columns'])
00349                                 && 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]
00350 
00351                                 // Init $TCA_DESCR for $table-key
00352                         $TCA_DESCR[$table]['columns']=array();
00353 
00354                                 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
00355                         foreach ($TCA_DESCR[$table]['refs'] as $llfile) {
00356                                 $LOCAL_LANG = $this->includeLLFile($llfile,0,1);
00357 
00358                                         // Traverse all keys
00359                                 if (is_array($LOCAL_LANG['default']))   {
00360                                         foreach($LOCAL_LANG['default'] as $lkey => $lVal)       {
00361 
00362                                                         // exploding by '.':
00363                                                         // 0 => fieldname,
00364                                                         // 1 => type from (alttitle,description,details,syntax,image_descr,image,seeAlso),
00365                                                         // 2 => special instruction, see switch construct
00366                                                 $kParts = explode('.',$lkey);
00367 
00368                                                         // Detecting 'hidden' labels, converting to normal fieldname
00369                                                 if ($kParts[0]=='_')    $kParts[0]='';
00370                                                 if (substr($kParts[0],0,1)=='_')        { $kParts[0] = substr($kParts[0],1); }
00371 
00372                                                         // Add label:
00373                                                 switch((string)$kParts[2])      {
00374                                                         case '+':       // adding
00375                                                                 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]].= chr(10).$lVal;
00376                                                         break;
00377                                                         default:        // Substituting:
00378                                                                 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]] = $lVal;
00379                                                         break;
00380                                                 }
00381                                         }
00382                                 }
00383                         }
00384                 }
00385         }
00386 
00396         function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0)  {
00397                         // Configure for global flag:
00398                 if ($setGlobal) {
00399                         global $LOCAL_LANG;
00400                 }
00401 
00402                         // Get default file:
00403                 $llang = $this->readLLfile($fileRef);
00404 
00405                 if (count($llang))      {
00406 
00407                         $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$llang);
00408 
00409                                 // Localized addition?
00410                         $lFileRef = $this->localizedFileRef($fileRef);
00411                         if ($lFileRef && (string)$LOCAL_LANG[$this->lang]=='EXT')       {
00412                                 $llang = $this->readLLfile($lFileRef);
00413                                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$llang);
00414                         }
00415 
00416                                 // Overriding file?
00417                         if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]))      {
00418                                 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]);
00419                                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$ORarray);
00420                         }
00421 
00422                                 // Merge local onto default:
00423                         if ($mergeLocalOntoDefault && strcmp($this->lang,'default') && is_array($LOCAL_LANG[$this->lang]) && is_array($LOCAL_LANG['default']))  {
00424                                 $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...
00425                                 unset($LOCAL_LANG[$this->lang]);
00426                         }
00427                 }
00428 
00429                         // Return value if not global is set.
00430                 if (!$setGlobal)        {
00431                         return $LOCAL_LANG;
00432                 }
00433         }
00434 
00441         function readLLfile($fileRef)   {
00442                 return t3lib_div::readLLfile($fileRef,$this->lang);
00443         }
00444 
00451         function localizedFileRef($fileRef)     {
00452                 if ($this->lang!='default' && substr($fileRef,-4)=='.php')      {
00453                         return substr($fileRef,0,-4).'.'.$this->lang.'.php';
00454                 }
00455         }
00456 }
00457 
00458 // Include extension to the template class?
00459 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])       {
00460         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
00461 }
00462 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6