TYPO3 API  SVNRelease
lang.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2011 Kasper Skårhøj (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 10311 2011-01-25 20:51:14Z francois $
00031  * Revised for TYPO3 3.6.0
00032  *
00033  * @author  Kasper Skårhøj <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  * Contains the TYPO3 Backend Language class
00061  *
00062  * For detailed information about how localization is handled,
00063  * please refer to the 'Inside TYPO3' document which descibes this.
00064  *
00065  * This class is normally instantiated as the global variable $LANG in typo3/template.php
00066  * It's only available in the backend and under certain circumstances in the frontend
00067  *
00068  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00069  * @package TYPO3
00070  * @subpackage core
00071  * @see typo3/template.php, template
00072  */
00073 class language {
00074         // This is set to the language that is currently running for the user
00075     public $lang = 'default';
00076         // Values like the labels in the tables.php-document are split by '|'.
00077         // This values defines which language is represented by which position
00078         // in the resulting array after splitting a value. (NOTICE: Obsolete concept!)
00079     public $langSplit = 'default';
00080 
00081         // Default charset in backend
00082     public $charSet = 'iso-8859-1';
00083 
00084         // Array with alternative charsets for other languages.
00085         // Moved to t3lib_cs, set internally from csConvObj!
00086     public $charSetArray = array();
00087 
00088         // This is the url to the TYPO3 manual
00089     public $typo3_help_url= 'http://www.typo3.com/man_uk/';
00090 
00091         // Array with alternative URLs based on language.
00092     public $helpUrlArray = array(
00093         'dk' => 'http://www.typo3.com/man_dk/',
00094     );
00095 
00096         // If true, will show the key/location of labels in the backend.
00097     public $debugKey = FALSE;
00098 
00099         // Can contain labels and image references from the backend modules.
00100         // Relies on t3lib_loadmodules to initialize modules after a global instance of $LANG has been created.
00101     public $moduleLabels = array();
00102 
00103         // Internal
00104         // Points to the position of the current language key as found in constant TYPO3_languages
00105     public $langSplitIndex = 0;
00106         // Internal cache for read LL-files
00107     public $LL_files_cache = array();
00108         // Internal cache for ll-labels (filled as labels are requested)
00109     public $LL_labels_cache = array();
00110 
00111     /**
00112      * instance of the "t3lib_cs" class. May be used by any application.
00113      *
00114      * @var t3lib_cs
00115      */
00116     public $csConvObj;
00117 
00118     /**
00119      * Initializes the backend language.
00120      * This is for example done in typo3/template.php with lines like these:
00121      *
00122      * require (PATH_typo3 . 'sysext/lang/lang.php');
00123      * $LANG = t3lib_div::makeInstance('language');
00124      * $LANG->init($BE_USER->uc['lang']);
00125      *
00126      * @param   string      The language key (two character string from backend users profile)
00127      * @param   string      IGNORE. Not used.
00128      * @return  void
00129      */
00130     public function init($lang, $altPath = '') {
00131 
00132             // Initialize the conversion object:
00133         $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
00134         $this->charSetArray = $this->csConvObj->charSetArray;
00135 
00136             // Internally setting the list of TYPO3 backend languages.
00137         $this->langSplit = TYPO3_languages;
00138 
00139             // Finding the requested language in this list based
00140             // on the $lang key being inputted to this function.
00141         $ls = explode('|', $this->langSplit);
00142 
00143         foreach ($ls as $i => $v) {
00144                 // Language is found. Configure it:
00145             if ($v == $lang) {
00146                     // The index of the language as found in the TYPO3_languages list
00147                 $this->langSplitIndex = $i;
00148                     // The current language key
00149                 $this->lang = $lang;
00150                         // The help URL if different from the default.
00151                 if ($this->helpUrlArray[$this->lang]) {
00152                     $this->typo3_help_url = $this->helpUrlArray[$this->lang];
00153                 }
00154                 if ($this->charSetArray[$this->lang]) {
00155                         // The charset if different from the default.
00156                     $this->charSet = $this->charSetArray[$this->lang];
00157                 }
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                 throw new RuntimeException('Forced charset not found: The forced character set "'. $this->charSet . '" was not found in t3lib/csconvtbl/');
00168             }
00169         }
00170     }
00171 
00172     /**
00173      * Adds labels and image references from the backend modules to the internal moduleLabels array
00174      *
00175      * @param   array       Array with references to module labels, keys: ['labels']['tablabel'],
00176      *                      ['labels']['tabdescr'], ['tabs']['tab']
00177      * @param   string      Module name prefix
00178      * @return  void
00179      * @access  public
00180      * @see t3lib_loadModules
00181      */
00182     public function addModuleLabels($arr, $prefix) {
00183         if (is_array($arr)) {
00184             foreach ($arr as $k => $larr) {
00185                 if (!isset($this->moduleLabels[$k])) {
00186                     $this->moduleLabels[$k] = array();
00187                 }
00188                 if (is_array($larr)) {
00189                     foreach ($larr as $l => $v) {
00190                         $this->moduleLabels[$k][$prefix . $l] = $v;
00191                     }
00192                 }
00193             }
00194         }
00195     }
00196 
00197     /**
00198      * Will htmlspecialchar() the input string and before that any charset conversion
00199      * 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      * @access public
00207      */
00208     public function hscAndCharConv($lStr, $hsc) {
00209             // labels returned from a locallang file used to be in the language of the charset.
00210             // Since TYPO3 4.1 they are always in the charset of the BE.
00211         if ($hsc) {
00212             return htmlspecialchars($lStr);
00213         } else {
00214             return $lStr;
00215         }
00216     }
00217 
00218     /**
00219      * Will convert the input strings special chars (all above 127) to entities.
00220      * The string is expected to be encoded in the charset, $this->charSet
00221      * This function is used to create strings that can be used in the Click Menu
00222      * (Context Sensitive Menus). The reason is that the values that are dynamically
00223      * written into the <div> layer is decoded as iso-8859-1 no matter what charset
00224      * is used in the document otherwise (only MSIE, Mozilla is OK).
00225      * So by converting we by-pass this problem.
00226      *
00227      * @param   string      Input string
00228      * @return  string      Output string
00229      * @access  public
00230      */
00231     public function makeEntities($str) {
00232             // Convert string to UTF-8:
00233         if ($this->charSet != 'utf-8') {
00234             $str = $this->csConvObj->utf8_encode($str, $this->charSet);
00235         }
00236 
00237             // Convert string back again, but using the full entity conversion:
00238         return $this->csConvObj->utf8_to_entities($str);
00239     }
00240 
00241     /**
00242      * Converts the input string to a JavaScript function returning the same string, but charset-safe.
00243      * Used for confirm and alert boxes where we must make sure that any string content
00244      * does not break the script AND want to make sure the charset is preserved.
00245      * Originally I used the JS function unescape() in combination with PHP function
00246      * rawurlencode() in order to pass strings in a safe way. This could still be done
00247      * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
00248      *
00249      * @param   string      Input string, encoded with $this->charSet
00250      * @return  string      Output string, a JavaScript function: "String.fromCharCode(......)"
00251      * @access  public
00252      */
00253     public function JScharCode($str) {
00254 
00255             // Convert string to UTF-8:
00256         if ($this->charSet != 'utf-8') {
00257             $str = $this->csConvObj->utf8_encode($str, $this->charSet);
00258         }
00259 
00260             // Convert the UTF-8 string into a array of char numbers:
00261         $nArr = $this->csConvObj->utf8_to_numberarray($str);
00262 
00263         return 'String.fromCharCode(' . implode(',', $nArr) . ')';
00264     }
00265 
00266     /**
00267      * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
00268      * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
00269      *
00270      * @param   string      Label key
00271      * @param   boolean     If set, the return value is htmlspecialchar'ed
00272      * @return  string
00273      * @access  public
00274      */
00275     public function getLL($index, $hsc = 0) {
00276             // Get Local Language
00277         if (strcmp($GLOBALS['LOCAL_LANG'][$this->lang][$index], '')) {
00278                 // Returns local label if not blank.
00279             $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index], $hsc);
00280         } else {
00281                 // Returns default label
00282             $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index], $hsc);
00283         }
00284         return $output . ($this->debugKey ? ' [' . $index . ']' : '');
00285     }
00286 
00287     /**
00288      * Works like ->getLL() but takes the $LOCAL_LANG array
00289      * used as the second argument instead of using the global array.
00290      *
00291      * @param   string      Label key
00292      * @param   array       $LOCAL_LANG array to get label key from
00293      * @param   boolean     If set, the return value is htmlspecialchar'ed
00294      * @return  string
00295      * @access  public
00296      */
00297     public function getLLL($index, $LOCAL_LANG, $hsc = 0) {
00298             // Get Local Language
00299         if (strcmp($LOCAL_LANG[$this->lang][$index], '')) {
00300                 // Returns local label if not blank.
00301             $output = $this->hscAndCharConv($LOCAL_LANG[$this->lang][$index], $hsc);
00302         } else {
00303                 // Returns default label
00304             $output = $this->hscAndCharConv($LOCAL_LANG['default'][$index], $hsc);
00305         }
00306         return $output . ($this->debugKey ? ' [' . $index . ']' : '');
00307     }
00308 
00309     /**
00310      * splitLabel function
00311      * Historically labels were exploded by '|' and each part would correspond
00312      * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
00313      * Today all translations are based on $LOCAL_LANG variables.
00314      * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
00315      * It's highly recommended to use the 'local_lang' method
00316      * (and thereby it's highly deprecated to use 'language-splitted' label strings)
00317      * Refer to 'Inside TYPO3' for more details
00318      *
00319      * @param   string      Label key/reference
00320      * @param   boolean     If set, the return value is htmlspecialchar'ed
00321      * @return  string
00322      * @access  public
00323      */
00324     public function sL($input, $hsc = 0) {
00325             // Using obsolete 'language-splitted' labels:
00326         if (strcmp(substr($input, 0, 4), 'LLL:')) {
00327             $t = explode('|', $input);
00328             $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
00329             return $this->hscAndCharConv($out, $hsc);
00330             // LOCAL_LANG:
00331         } else {
00332                 // If cached label
00333             if (!isset($this->LL_labels_cache[$this->lang][$input])) {
00334                 $restStr = trim(substr($input, 4));
00335                 $extPrfx = '';
00336 
00337                     // ll-file refered to is found in an extension.
00338                 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
00339                     $restStr = trim(substr($restStr, 4));
00340                     $extPrfx = 'EXT:';
00341                 }
00342                 $parts = explode(':', $restStr);
00343                 $parts[0] = $extPrfx . $parts[0];
00344 
00345                     // Getting data if not cached
00346                 if (!isset($this->LL_files_cache[$parts[0]])) {
00347                     $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
00348 
00349                         // If the current language is found in another file, load that as well:
00350                     $lFileRef = $this->localizedFileRef($parts[0]);
00351                     if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
00352                             && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
00353                         $tempLL = $this->readLLfile($lFileRef);
00354                         $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
00355                     }
00356 
00357                         // Overriding file?
00358                         // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
00359                         // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
00360                     if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
00361                         t3lib_div::deprecationLog('Usage of $TYPO3_CONF_VARS[\'BE\'][\'XLLfile\'] is deprecated since TYPO3 4.3. Use $TYPO3_CONF_VARS[\'SYS\'][\'locallangXMLOverride\'][] to include the file ' . $fileRef . ' instead.');
00362                         $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
00363                         $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]], $ORarray);
00364                     }
00365                 }
00366                 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
00367             }
00368                 // For the cached output charset conversion has already happened!
00369                 // So perform HSC right here.
00370             $output = $this->LL_labels_cache[$this->lang][$input];
00371             if ($hsc) {
00372                 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
00373             }
00374             return $output . ($this->debugKey ? ' [' . $input . ']' : '');
00375         }
00376     }
00377 
00378     /**
00379      * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
00380      * as defined in $TCA_DESCR[$table]['refs']
00381      * $TCA_DESCR is a global var
00382      *
00383      * @param   string      Table name found as key in global array $TCA_DESCR
00384      * @return  void
00385      * @access  public
00386      */
00387     public function loadSingleTableDescription($table) {
00388         global $TCA_DESCR;
00389 
00390             // First the 'table' cannot already be loaded in [columns]
00391             // and secondly there must be a references to locallang files available in [refs]
00392         if (is_array($TCA_DESCR[$table])
00393                 && !isset($TCA_DESCR[$table]['columns'])
00394                 && is_array($TCA_DESCR[$table]['refs'])) {
00395 
00396                 // Init $TCA_DESCR for $table-key
00397             $TCA_DESCR[$table]['columns'] = array();
00398 
00399                 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
00400             foreach ($TCA_DESCR[$table]['refs'] as $llfile) {
00401                 $LOCAL_LANG = $this->includeLLFile($llfile, 0, 1);
00402 
00403                     // Traverse all keys
00404                 if (is_array($LOCAL_LANG['default'])) {
00405                     foreach ($LOCAL_LANG['default'] as $lkey => $lVal) {
00406                         $type = '';
00407                         $fieldName = '';
00408 
00409                             // Exploding by '.':
00410                             // 0-n => fieldname,
00411                             // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
00412                             // n+2 => special instruction, if any
00413                         $keyParts = explode('.', $lkey);
00414                         $keyPartsCount = count($keyParts);
00415                             // Check if last part is special instruction
00416                             // Only "+" is currently supported
00417                         $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ? TRUE : FALSE;
00418                         if ($specialInstruction) {
00419                             array_pop($keyParts);
00420                         }
00421 
00422                             // If there are more than 2 parts, get the type from the last part
00423                             // and merge back the other parts with a dot (.)
00424                             // Otherwise just get type and field name straightaway
00425                         if ($keyPartsCount > 2) {
00426                             $type = array_pop($keyParts);
00427                             $fieldName = implode('.', $keyParts);
00428                         } else {
00429                             $fieldName = $keyParts[0];
00430                             $type = $keyParts[1];
00431                         }
00432 
00433                             // Detecting 'hidden' labels, converting to normal fieldname
00434                         if ($fieldName == '_') {
00435                             $fieldName = '';
00436                         }
00437                         if (substr($fieldName, 0, 1) == '_') {
00438                             $fieldName = substr($fieldName, 1);
00439                         }
00440 
00441                             // Append label
00442                         if ($specialInstruction) {
00443                             $TCA_DESCR[$table]['columns'][$fieldName][$type] .= LF . $lVal;
00444                         } else {
00445                                 // Substitute label
00446                             $TCA_DESCR[$table]['columns'][$fieldName][$type] = $lVal;
00447                         }
00448                     }
00449                 }
00450             }
00451         }
00452     }
00453 
00454     /**
00455      * Includes locallang file (and possibly additional localized version if configured for)
00456      * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = true).
00457      *
00458      * @param   string      $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
00459      * @param   boolean     Setting in global variable $LOCAL_LANG (or returning the variable)
00460      * @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.
00461      * @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
00462      * @access  public
00463      */
00464     public function includeLLFile($fileRef, $setGlobal = 1, $mergeLocalOntoDefault = 0) {
00465             // Configure for global flag:
00466         if ($setGlobal) {
00467             global $LOCAL_LANG;
00468         }
00469 
00470             // Get default file
00471         $llang = $this->readLLfile($fileRef);
00472 
00473         if (is_array($llang) && count($llang)) {
00474 
00475             $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule((array)$LOCAL_LANG, $llang);
00476 
00477                 // Localized addition?
00478             $lFileRef = $this->localizedFileRef($fileRef);
00479             if ($lFileRef && (string)$LOCAL_LANG[$this->lang] == 'EXT') {
00480                 $llang = $this->readLLfile($lFileRef);
00481                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG, $llang);
00482             }
00483 
00484                 // Overriding file?
00485                 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
00486                 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
00487             if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef])) {
00488                 t3lib_div::deprecationLog('Usage of $TYPO3_CONF_VARS[\'BE\'][\'XLLfile\'] is deprecated since TYPO3 4.3. Use $TYPO3_CONF_VARS[\'SYS\'][\'locallangXMLOverride\'][] to include the file ' . $fileRef . ' instead.');
00489                 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]);
00490                 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG, $ORarray);
00491             }
00492 
00493                 // Merge local onto default
00494             if ($mergeLocalOntoDefault && strcmp($this->lang, 'default') && is_array($LOCAL_LANG[$this->lang]) && is_array($LOCAL_LANG['default'])) {
00495                     // array_merge can be used so far the keys are not
00496                     // numeric - which we assume they are not...
00497                 $LOCAL_LANG['default'] = array_merge($LOCAL_LANG['default'], $LOCAL_LANG[$this->lang]);
00498                 unset($LOCAL_LANG[$this->lang]);
00499             }
00500         }
00501 
00502             // Return value if not global is set.
00503         if (!$setGlobal) {
00504             return $LOCAL_LANG;
00505         }
00506     }
00507 
00508     /**
00509      * Includes a locallang file and returns the $LOCAL_LANG array found inside.
00510      *
00511      * @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.
00512      * @return  array       Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
00513      * @access  private
00514      */
00515     protected function readLLfile($fileRef) {
00516         return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
00517     }
00518 
00519     /**
00520      * Returns localized fileRef (.[langkey].php)
00521      *
00522      * @param   string      Filename/path of a 'locallang.php' file
00523      * @return  string      Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
00524      * @access  private
00525      */
00526     protected function localizedFileRef($fileRef) {
00527         if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
00528             return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
00529         }
00530     }
00531 }
00532 
00533 
00534 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
00535     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
00536 }
00537 
00538 ?>