1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class LanguageData
* @brief Stores language data in a class object
*
* This class stores data of the Language object. These are the paths to all
* relevant language files, the configured language and the default language.
* This object is designed to be stored in a PHP session. The Language
* object itself couldn't be stored in a Session because it uses PHP objects
* which couldn't stored in a PHP session.
* @par Examples
* @code // show how to use this class with the language class and sessions
* script_a.php
* // create a language data object and assign it to the language object
* $language = new Language();
* $languageData = new LanguageData('de');
* $language->addLanguageData($languageData);
* $session->addObject('languageData', $languageData);
*
* script_b.php
* // read language data from session and add it to language object
* $language = new Language();
* $language->addLanguageData($session->getObject('languageData')); @endcode
*/
class LanguageData
{
public $textCache = array(); ///< Stores all read text data in an array to get quick access if a text is required several times
private $languageFilePath = array(); ///< Array with all relevant language files
private $language; ///< The ISO code of the language that should be read in this object
private $referenceLanguage = 'en'; ///< The ISO code of the default language that should be read if in the current language the text id is not translated
private $countries = array(); ///< Array with all countries and their ISO codes e.g.: array('DEU' => 'Germany' ...)
/**
* Creates an object that stores all necessary language data and can be handled in session.
* Therefore the language must be set and optional a path where the language files are stored.
* @param string $language The ISO code of the language for which the texts should be read e.g. @b 'de'
* @param string $languagePath Optional a server path to the language files. If no path is set
* than the default Admidio language path @b adm_program/languages will be set.
*/
public function __construct($language, $languagePath = '')
{
if($languagePath === '')
{
$this->addLanguagePath(SERVER_PATH. '/adm_program/languages');
}
else
{
$this->addLanguagePath($languagePath);
}
$this->setLanguage($language);
}
/**
* Adds a new path of language files to the array with all language paths
* where Admidio should search for language files.
* @param string $path Server path where Admidio should search for language files.
*/
public function addLanguagePath($path)
{
if($path !== '' && !in_array($path, $this->languageFilePath, true))
{
$this->languageFilePath[] = $path;
}
}
/**
* Returns an array with all countries and their ISO codes
* @return string[] Array with all countries and their ISO codes e.g.: array('DEU' => 'Germany' ...)
*/
public function getCountriesArray()
{
return $this->countries;
}
/**
* Returns the language code of the language of this object. This is the code that is set within
* Admidio with some specials like de_sie. If you only want the ISO code then call getLanguageIsoCode().
* @param bool $referenceLanguage If set to @b true than the language code of the reference language will returned.
* @return string Returns the language code of the language of this object or the reference language.
*/
public function getLanguage($referenceLanguage = false)
{
if($referenceLanguage)
{
return $this->referenceLanguage;
}
return $this->language;
}
/**
* Returns an array with all language paths that were set.
* @return string[] with all language paths that were set.
*/
public function getLanguagePaths()
{
return $this->languageFilePath;
}
/**
* Save the array with all countries and their ISO codes in an internal parameter for later use
* @param string[] $countries Array with all countries and their ISO codes e.g.: array('DEU' => 'Germany' ...)
*/
public function setCountriesArray(array $countries)
{
$this->countries = $countries;
}
/**
* Set a language to this object. If there was a language before than initialize the cache
* @param string $language ISO code of the language that should be set to this object.
*/
public function setLanguage($language)
{
if($language !== $this->language)
{
// initialize all parameters
$this->textCache = array();
$this->countries = array();
$this->language = $language;
}
}
}