Source for file language.php
Documentation is available at language.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Allows for quoting in language .ini files.
* Languages/translation handler class
* @package Joomla.Platform
* Array of JLanguage objects
protected static $languages =
array();
* Debug language, If true, highlights if string isn't found.
* The default language, used when a language file in the requested language does not exist.
* An array of orphaned text.
* Array holding the language metadata.
* Array holding the language locale or boolean null if none.
* A nested array of language files that have been loaded
* List of language files that are in error state
* An array of used text, used during debugging.
protected $used =
array();
* Counter for number of loads.
* An array used to store overrides.
* Name of the transliterator function for this language.
* Name of the pluralSuffixesCallback function for this language.
* Name of the ignoredSearchWordsCallback function for this language.
* Name of the lowerLimitSearchWordCallback function for this language.
* Name of the uppperLimitSearchWordCallback function for this language.
* Name of the searchDisplayedCharactersNumberCallback function for this language.
* Constructor activating the default information of the language.
* @param string $lang The language
* @param boolean $debug Indicates if language debugging is enabled.
public function __construct($lang =
null, $debug =
false)
$filename =
JPATH_BASE .
"/language/overrides/$lang.override.ini";
// Sort the underlying heap by key values to optimize merging
ksort($contents, SORT_STRING);
// Look for a language specific localise class
// Note: Manual indexing to enforce load order.
$paths[0] =
JPATH_SITE .
"/language/overrides/$lang.localise.php";
$paths[2] =
JPATH_SITE .
"/language/$lang/$lang.localise.php";
if (defined('JPATH_ADMINISTRATOR'))
// Note: Manual indexing to enforce load order.
/* Class exists. Try to find
* -a transliterate method,
* -a getPluralSuffixes method,
* -a getIgnoredSearchWords method
* -a getLowerLimitSearchWord method
* -a getUpperLimitSearchWord method
* -a getSearchDisplayCharactersNumber method
* Returns a language object.
* @param string $lang The language to use.
* @param boolean $debug The debug mode.
* @return JLanguage The Language object.
public static function getInstance($lang, $debug =
false)
if (!isset
(self::$languages[$lang .
$debug]))
self::$languages[$lang .
$debug] =
new JLanguage($lang, $debug);
return self::$languages[$lang .
$debug];
* Translate function, mimics the php gettext (alias _) function.
* The function checks if $jsSafe is true, then if $interpretBackslashes is true.
* @param string $string The string to translate
* @param boolean $jsSafe Make the result javascript safe
* @param boolean $interpretBackSlashes Interpret \t and \n
* @return string The translation of the string
public function _($string, $jsSafe =
false, $interpretBackSlashes =
true)
$key =
strtoupper($string);
// Store debug information
$this->used[$key] =
array();
$this->used[$key][] =
$caller;
$caller['string'] =
$string;
$string =
'??' .
$string .
'??';
elseif ($interpretBackSlashes)
// Interpret \n and \t characters
$string =
str_replace(array('\\\\', '\t', '\n'), array("\\", "\t", "\n"), $string);
* This method processes a string and replaces all accented UTF-8 characters by unaccented
* @param string $string The string to transliterate.
* @return string The transliteration of the string.
* Getter for transliteration function
* @return callable The transliterator function
* Set the transliteration function.
* @param callable $function Function name or the actual function.
* @return callable The previous function.
* Returns an array of suffixes for plural rules.
* @param integer $count The count number the rule is for.
* @return array The array of suffixes.
return array((string)
$count);
* Getter for pluralSuffixesCallback function.
* @return callable Function name or the actual function.
* Set the pluralSuffixes function.
* @param callable $function Function name or actual function.
* @return callable The previous function.
* Returns an array of ignored search words
* @return array The array of ignored search words.
* Getter for ignoredSearchWordsCallback function.
* @return callable Function name or the actual function.
* Setter for the ignoredSearchWordsCallback function
* @param callable $function Function name or actual function.
* @return callable The previous function.
* Returns a lower limit integer for length of search words
* @return integer The lower limit integer for length of search words (3 if no value was set for a specific language).
* Getter for lowerLimitSearchWordCallback function
* @return callable Function name or the actual function.
* Setter for the lowerLimitSearchWordCallback function.
* @param callable $function Function name or actual function.
* @return callable The previous function.
* Returns an upper limit integer for length of search words
* @return integer The upper limit integer for length of search words (20 if no value was set for a specific language).
* Getter for upperLimitSearchWordCallback function
* @return callable Function name or the actual function.
* Setter for the upperLimitSearchWordCallback function
* @param callable $function Function name or the actual function.
* @return callable The previous function.
* Returns the number of characters displayed in search results.
* @return integer The number of characters displayed (200 if no value was set for a specific language).
* Getter for searchDisplayedCharactersNumberCallback function
* @return callable Function name or the actual function.
* Setter for the searchDisplayedCharactersNumberCallback function.
* @param callable $function Function name or the actual function.
* @return callable The previous function.
* Checks if a language exists.
* This is a simple, quick check for the directory that should contain language files for the given user.
* @param string $lang Language to check.
* @param string $basePath Optional path to check.
* @return boolean True if the language exists.
public static function exists($lang, $basePath =
JPATH_BASE)
// Return false if no language was specified
$path =
$basePath .
'/language/' .
$lang;
// Return previous check results if it exists
if (isset
($paths[$path]))
// Check if the language exists
$paths[$path] =
is_dir($path);
* Loads a single language file and appends the results to the existing strings
* @param string $extension The extension for which a language file should be loaded.
* @param string $basePath The basepath to use.
* @param string $lang The language to load, default null for the current language.
* @param boolean $reload Flag that will force a language to be reloaded if set to true.
* @param boolean $default Flag that force the default language to be loaded if the current does not exist.
* @return boolean True if the file has successfully loaded.
public function load($extension =
'joomla', $basePath =
JPATH_BASE, $lang =
null, $reload =
false, $default =
true)
// Load the default language first if we're not debugging and a non-default language is requested to be loaded
// with $default set to true
$this->load($extension, $basePath, $this->default, false, true);
$path =
self::getLanguagePath($basePath, $lang);
$internal =
$extension ==
'joomla' ||
$extension ==
'';
$filename =
$internal ?
$lang :
$lang .
'.' .
$extension;
$filename =
"$path/$filename.ini";
if (isset
($this->paths[$extension][$filename]) &&
!$reload)
// This file has already been tested for loading.
$result =
$this->paths[$extension][$filename];
// Load the language file
// Check whether there was a problem with loading the file
if ($result ===
false &&
$default)
// No strings, so either file doesn't exist or the file is invalid
$oldFilename =
$filename;
// Check the standard file name
$path =
self::getLanguagePath($basePath, $this->default);
$filename =
$internal ?
$this->default :
$this->default .
'.' .
$extension;
$filename =
"$path/$filename.ini";
// If the one we tried is different than the new name, try again
if ($oldFilename !=
$filename)
$result =
$this->loadLanguage($filename, $extension, false);
* This method will not note the successful loading of a file - use load() instead.
* @param string $filename The name of the file.
* @param string $extension The name of the extension.
* @return boolean True if new strings have been added to the language
protected function loadLanguage($filename, $extension =
'unknown')
$strings =
$this->parse($filename);
// Sort the underlying heap by key values to optimize merging
ksort($strings, SORT_STRING);
// Do not bother with ksort here. Since the originals were sorted, PHP will already have chosen the best heap.
// Record the result of loading the extension's file.
if (!isset
($this->paths[$extension]))
$this->paths[$extension] =
array();
$this->paths[$extension][$filename] =
$result;
* Parses a language file.
* @param string $filename The name of the file.
* @return array The array of parsed strings.
protected function parse($filename)
// Capture hidden PHP errors from the parsing.
$track_errors =
ini_get('track_errors');
// Restore error tracking to what it was before.
ini_set('track_errors', $track_errors);
// Initialise variables for manually parsing the file for common errors.
$blacklist =
array('YES', 'NO', 'NULL', 'FALSE', 'ON', 'OFF', 'NONE', 'TRUE');
$regex =
'/^(|(\[[^\]]*\])|([A-Z][A-Z0-9_\-\.]*\s*=(\s*(("[^"]*")|(_QQ_)))+))\s*(;.*)?$/';
// Open the file as a stream.
$file =
new SplFileObject($filename);
foreach ($file as $lineNumber =>
$line)
// Avoid BOM error as BOM is OK when using parse_ini
// Check that the key is not in the blacklist and that the line format passes the regex.
// Workaround to reduce regex complexity when matching escaped quotes
// Check if we encountered any errors.
$this->errorfiles[$filename] =
$filename .
' : error(s) in line(s) ' .
implode(', ', $errors);
// We didn't find any errors but there's probably a parse notice.
$this->errorfiles['PHP' .
$filename] =
'PHP parser errors :' .
$php_errormsg;
* Get a metadata language property.
* @param string $property The name of the property.
* @param mixed $default The default value.
* @return mixed The value of the property.
public function get($property, $default =
null)
* Determine who called JLanguage or JText.
* @return array Caller information.
// Try to determine the source if none was provided
// Search through the backtrace to our caller
while ($continue &&
next($backtrace))
$class =
@ $step['class'];
// We're looking for something outside of language.php
if ($class !=
'JLanguage' &&
$class !=
'JText')
$info['function'] =
@ $step['function'];
$info['step'] =
prev($backtrace);
// Determine the file and name of the file
$info['file'] =
@ $step['file'];
$info['line'] =
@ $step['line'];
* @return string Official name element of the language.
* Get a list of language files that have been loaded.
* @param string $extension An optional extension name.
public function getPaths($extension =
null)
if (isset
($this->paths[$extension]))
return $this->paths[$extension];
* Get a list of language files that are in error state.
* Getter for the language tag (as defined in RFC 3066)
* @return string The language tag.
* @return boolean True is it an RTL language.
* Set the Debug property.
* @param boolean $debug The debug setting.
* @return boolean Previous value.
$previous =
$this->debug;
$this->debug = (boolean)
$debug;
* Get the Debug property.
* @return boolean True is in debug mode.
* Get the default language code.
* @return string Language code.
* Set the default language code.
* @param string $lang The language code.
* @return string Previous value.
* Get the list of orphaned strings if being tracked.
* @return array Orphaned text.
* Get the list of used strings.
* Used strings are those strings requested and found either as a string or a constant.
* @return array Used strings.
* Determines is a key exists.
* @param string $string The key to check.
* @return boolean True, if the key exists.
public function hasKey($string)
return isset
($this->strings[$key]);
* Returns a associative array holding the metadata.
* @param string $lang The name of the language.
* @return mixed If $lang exists return key/value pair with the language metadata, otherwise return NULL.
$result =
self::parseXMLLanguageFile("$path/$file");
* Returns a list of known languages for an area
* @param string $basePath The basepath to use
* @return array key/value pair with the language file and real name.
$dir =
self::getLanguagePath($basePath);
$knownLanguages =
self::parseLanguageFiles($dir);
* Get the path to a language
* @param string $basePath The basepath to use.
* @param string $language The language tag.
* @return string language related path or null.
public static function getLanguagePath($basePath =
JPATH_BASE, $language =
null)
$dir =
$basePath .
'/language';
* Set the language attributes to the given language.
* Once called, the language still needs to be loaded using JLanguage::load().
* @param string $lang Language code.
* @return string Previous value.
* Get the language locale based on current language.
* @return array The locale according to the language.
* Get the first day of the week for this language.
* @return integer The first day of the week according to the language
return (int)
(isset
($this->metadata['firstDay']) ?
$this->metadata['firstDay'] :
0);
* Get the weekends days for this language.
* @return string The weekend days of the week separated by a comma according to the language
* Searches for language directories within a certain base dir.
* @param string $dir directory of files.
* @return array Array holding the found languages as filename => real name pairs.
$iterator =
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file)
$fileName =
$file->getFilename();
if (!$file->isFile() ||
!preg_match("/^([-_A-Za-z]*)\.xml$/", $fileName))
$metadata =
self::parseXMLLanguageFile($file->getRealPath());
$langs[$lang] =
$metadata;
catch
(RuntimeException $e)
* Parse XML file for language information.
* @param string $path Path to the XML files.
* @return array Array holding the found metadata as a key => value pair.
* @throws RuntimeException
throw
new RuntimeException('File not found or not readable');
// Check that it's a metadata file
if ((string)
$xml->getName() !=
'metafile')
foreach ($xml->metadata->children() as $child)
$metadata[$child->getName()] = (string)
$child;
Documentation generated on Tue, 19 Nov 2013 15:06:25 +0100 by phpDocumentor 1.4.3