<?php
defined('VSIGN') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");

require_once("3rdparty/php-gettext/streams.php");
require_once("3rdparty/php-gettext/gettext.php");
$transEngine = '';
$stream = '';
 
class TranslationEngine
{	
	/**
	 * Gets and Sets the Local 
	 * @return 
	 */
	public static function InitLocale(database $db)
	{
            $localeDir	= 'locale';
            $default    = Config::GetSetting($db, 'DEFAULT_LANGUAGE');
            
            global $transEngine;
            global $stream;

            //Debug::LogEntry($db, 'audit', 'IN', 'TranslationEngine', 'InitLocal');

            // Try to get the local firstly from _REQUEST (post then get)
            $lang = Kit::GetParam('lang', _REQUEST, _WORD, '');

            // Build an array of supported languages
            $supportedLangs = scandir($localeDir);

            if ($lang != '')
            {
                // Set the language
                Debug::LogEntry($db, 'audit', 'Set the Language from REQUEST [' . $lang . ']', 'TranslationEngine', 'InitLocal');

                // Is this language supported?
                // if not just use the default (eb_GB).
                if (!in_array($lang . '.mo', $supportedLangs))
                {
                    trigger_error(sprintf('Language not supported. %s', $lang));

                    // Use the default language instead.
                    $lang = $default;
                }
            }
            else
            {
                $langs = Kit::GetParam('HTTP_ACCEPT_LANGUAGE', $_SERVER, _STRING);

                if ($langs != '')
                {
                    //Debug::LogEntry($db, 'audit', ' HTTP_ACCEPT_LANGUAGE [' . $langs . ']', 'TranslationEngine', 'InitLocal');
                    $langs = explode(',', $langs);

                    foreach ($langs as $lang)
                    {
                        // Remove any quality rating (as we aren't interested)
                        $rawLang = explode(';', $lang);
                        $lang = str_replace("-", "_", $rawLang[0]);

                        if (in_array($lang . '.mo', $supportedLangs))
                        {
                            //Debug::LogEntry($db, 'audit', 'Obtained the Language from HTTP_ACCEPT_LANGUAGE [' . $lang . ']', 'TranslationEngine', 'InitLocal');
                            break;
                        }

                        // Set lang as the default
                        $lang = $default;
                    }
                }
                else
                {
                    $lang = $default;
                }
            }

            // We have the language
            //Debug::LogEntry($db, 'audit', 'Creating new file streamer for '. $localeDir . '/' . $lang . '.mo', 'TranslationEngine', 'InitLocal');

            if (!$stream = new CachedFileReader($localeDir . '/' . $lang . '.mo'))
            {
                trigger_error('Unable to translate this language');
                $transEngine = false;
                
                return;
            }

            $transEngine    = new gettext_reader($stream);

            
            //Debug::LogEntry($db, 'audit', 'OUT', 'TranslationEngine', 'InitLocal');
	}
}

/**
 * Global Translation Function
 * @return 
 * @param $string Object
 */ 
function __($string)
{
	global $db;
        global $transEngine;
        $orignial = $string;

        if (!$transEngine) return $string;
	
	$string = $transEngine->translate($string);

        //Debug::LogEntry($db, 'audit', 'Translating [' . $orignial .']  to [' . $string .']', '', '__');
        
        return $string;
}
?>