<?php
require_once('vendor/autoload.php');
use Gettext\Loader\PoLoader;

class translator{
    private $language = null;  //default
    private $translator= null;
    public function setlang($lang){
        $this->language=$lang;
    }
    public function getLanguage(){
        global $db;
        if(isset($_SESSION['locale'])){
            return $_SESSION['locale'];
        }
        else{
            //try to get from database
				$evp_options_sql = "SELECT option_name, option_value FROM evp_options";
				$evp_options_read = $db->select($evp_options_sql);
				if ($evp_options_read) {
					$evp_options_check = mysqli_num_rows($evp_options_read);
					if ($evp_options_check > 0) {
						 while($row = $evp_options_read->fetch_assoc()) {
							$option_name[$row['option_name']] = $row['option_value'];
					}
					$evp_options_read->free();
						$locale =  $option_name['locale'];		
					}
				}

            if(!empty($locale)){
                 return $locale;
            }
            else{
                return "en"; //default en
            }
        }
    }
	
    public function getTranslation($text){
        if(!$this->language){
            $this->language = $this->getLanguage();
        }
        if(file_exists(dirname(__FILE__).'/locale/'.$this->language.'.po')){
            //import from a .po file:
            $loader = new PoLoader();
            $translations = $loader->loadFile(dirname(__FILE__).'/locale/'.$this->language.'.po');
            $this->translator = $translations;
            $translation = $this->translator->find(null, $text);
            if($translation){
                return $translation->getTranslation();

            }
            else{
                return $text; //return same text if no translation for this text is set in po file
            }
        }
        else{
            throw new Exception("Translation for ".$this->language." does not exist");
        }

    }
    public function saveLanguage(){
        global $db;

        if(isset($_POST) && isset($_POST['locale'])){
            $locale='';
            if(isset($_SESSION['locale'])){
                $locale=$_SESSION['locale'];
            }
            $requested_locale = $_POST['locale'];
            //compare existing locale in session with requested
             if($locale!==$requested_locale){
                 //set new locale in database and in session
                $q = "UPDATE `evp_options` SET `option_value` = '$requested_locale' WHERE `option_name` = 'locale'";
                $updated = $db->update($q);
               if($updated){
                    $_SESSION['locale']=$requested_locale;
                    return "Language set successfully.";
                }
                else{
                    return "There was an error while setting language.";
                }
            }
        }

    }
}


//SIMPLIFIED USAGE Functions
$translator = new translator();
function userLang($lang){
    global $translator;
    $translator->setlang($lang);
    return true;
}
function _e($text){
    global $translator;
    echo  $translator->getTranslation($text);
}
function __($text){
    global $translator;
    return $translator->getTranslation($text);
}

?>