<?php
namespace gnomephp\helper\lang;
class Lang{
const SESSION_LANG_NAME = 'gnomephp.language';
/**
* Array of loaded language LangFile objects.
* @var array Array of loaded LangFile objects.
*/
private $files = array();
/**
* Session stream.
* @var gnomephp\Session
*/
protected $session;
public function __construct($session){
$this->session = $session;
}
/**
* Gets the current language from a session variable, if not set, we will get it from the configuration file application, default_language.
*/
public function getCurrentLanguage(){
if ($lang = $this->session->get(Lang::SESSION_LANG_NAME)){
return $lang;
}else{
return \gnomephp\Configuration::get('application', 'default_language');
}
}
/**
* Sets the current language to a session variable.
* @param string $language The language, eg. "english"
*/
public function setCurrentLanguage($language){
$this->session->set(Lang::SESSION_LANG_NAME, $language);
$this->session->save();
}
/**
* Loads a lang file into the stack.
*
* @param string $file Filename ( without extension )
* @param string $lang The language name ( eg. english ) If none is set, we will get the language from the getCurrentLanguage() method.
* @throws LangFileException
*/
public function load($file, $lang = false){
if (!array_key_exists($file,$this->files)){
$lang = new LangFile($file, $lang ? $lang : $this->getCurrentLanguage());
$this->files[$file] = $lang;
}
return $this->files[$file];
}
/**
* Gets a specific lang file that is loaded with the load() method.
*
* @param string $file The filename ( without extension )
* @throws LangFileNotLoadedException
*/
public function get($file){
if (!array_key_exists($file, $this->files)){
throw new LangFileNotLoadedException("Lang file $file is not loaded yet, it should be loaded with ->load($file).");
}
return $this->files[$file];
}
/**
* Unloads the file from the array buffer.
* @param string $file The filename ( without extension )
*/
public function unload($file){
if (isset($this->files[$file]))unset($this->files[$file]);
}
}