class Language
{
private $_lang = array();
private $_language = '';
private $_languages = array();
private $_language_path = array();
public function __construct()
{
$path = ROOT.DS.'lang'.DS;
$this->setPath($path);
if(file_exists('language_cache.data'))
{
$this->languages = unserialize(file_get_contents('language_cache.data'));
}
else
{
// loading languages
foreach($this->_language_path as $path)
{
if ($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
array_push($this->_languages, str_replace(".php", "", $file));
}
}
closedir($handle);
array_unique($this->_languages);
}
else
{
continue;
}
}
file_put_contents('language_cache.data', serialize($this->_languages));
}
$this->detectLanguage();
}
public function setPath($path)
{
if(is_dir($path))
{
if(!array_key_exists($path, $this->_language_path))
{
array_push($this->_language_path, $path);
}
}
$this->loadLanguage();
}
private function getPath()
{
return $this->_language_path;
}
public function setLanguage($lang)
{
if(in_array($lang, $this->_languages))
{
$this->_language = strtolower($lang);
}
else
{
$this->_language = 'en';
}
$this->loadLanguage();
}
public function getLanguage()
{
return in_array($this->_language, $this->_languages) ? $this->_language : "en";
}
public function getLanguageList()
{
return $this->_languages;
}
private function detectLanguage()
{
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$this->setLanguage($lang);
return $lang;
}
private function loadLanguage()
{
foreach($this->_language_path as $path)
{
###
$file = $path . $this->getLanguage() . '.php';
if(!file_exists($file))
{
$file = str_replace($this->getLanguage(), "en", $file);
}
include($file);
$this->_lang = array_merge($this->_lang, $lang);
}
}
public function get($tag)
{
return array_key_exists(strtoupper($tag), $this->_lang) ? $this->_lang[strtoupper($tag)] : '';
}
public function set()
{
$args = func_get_args();
if(is_array($args[0]))
{
foreach($args[0] as $key => $value)
{
if(!array_key_exists(strtoupper($key), $this->_lang))
{
$this->_lang[strtoupper($key)] = $value;
}
}
}
else
{
if(!array_key_exists(strtoupper($args[0]), $this->_lang))
{
$this->_lang[strtoupper($args[0])] = (isset($args[1])) ? $args[1] : "";
}
}
}
}