<?php
######################
#
#    Copyright (C) 2015 EngSaS - Engineering Soluations and Services Langenbach. All rights reserved.
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#    MA 02110-1301  USA.
#
######################

// Require per-http2
// Require php5-gettext

define("LOCALE_SESSION_KEY", "gyl_locale");
define("LOCALE_DEFAULT", "en_US");
define("LOCALE_REQUEST_PARAN", "lang");
define("LOCALE_DOMAIN", "messages"); // Must be the same name, as the po and mo files and the directory they are in

require_once("HTTP2.php");

$config = new SiWeather\Webservice\WebserviceConfig();

$numberDelimiterDecimal = array();
$numberDelimiterDecimal["en_US"] = ".";
$numberDelimiterDecimal["de_DE"] = ",";

$_localeCurrent = LOCALE_DEFAULT;
if(isset($_SESSION)){
	if(array_key_exists(LOCALE_REQUEST_PARAN, $_REQUEST)){
		$_localeCurrent = $_REQUEST[LOCALE_REQUEST_PARAN];
		$_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
	}
	elseif(array_key_exists(LOCALE_SESSION_KEY, $_SESSION)){
		$_localeCurrent = $_SESSION[LOCALE_SESSION_KEY];
	}
	else{
		$supportedLanguages = Array(LOCALE_DEFAULT);
		$transDirHandle = opendir($config->getConfig("translationdirectory"));
		if($transDirHandle){
			while(false !== ($entry = readdir($transDirHandle))){
				if($entry != "." && $entry != ".."){
					$supportedLanguages[str_replace("_", "-", $entry)] = true;
				}
			}
			
			closedir($transDirHandle);
		}
		$http = new HTTP2();
		$_localeCurrent = str_replace("-", "_", $http->negotiateLanguage($supportedLanguages, LOCALE_DEFAULT));
		$_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
	}
}

$results = putenv("LC_ALL0=". $_localeCurrent);
if(!$results){
	error_log("putenv failed (". $result .")");
}
$result = setlocale(LC_ALL, $_localeCurrent);
if(!$results){
	error_log("setlocale failed (". $result .")");
}
$results = bindtextdomain(LOCALE_DOMAIN, $config->getConfig("translationdirectory"));
if(!$results){
	error_log("bindtextdomain failed (". $result .")");
}
$results = bind_textdomain_codeset(LOCALE_DOMAIN, "UTF-8");
if(!$results){
	error_log("bind_textdomain_codeset failed (". $result .")");
}
$results = textdomain(LOCALE_DOMAIN);
if(!$results){
	error_log("textdomain failed (". $result .")");
}

class Translator {
	public static function swGettext($id)
	{
		return vsprintf(gettext($id), array_slice(func_get_args(), 1));
	}
}
