<?php

namespace FOP;

class I18n {
  
  

    // --- Statische Eigenschaften ---
  
  static private $locale ;

    // --- Statische Methoden ---
  
  public static function hasLocale($locale) { 
    
    
    // --- BLOCK: main ---
    
		  return ($locale == 'de' || $locale == 'en' || $locale == 'en_GB' || $locale == 'en_SG');
	 
    
  }

  public static function acceptFromHttp($locales) { 
    
    
    // --- BLOCK: main ---
    
			foreach (explode(';', $locales) AS $list2) {
				$list2 = explode(',', $list2);
				foreach ($list2 AS $locale) {
					$locale = trim($locale);
					if ($locale[0] != 'q' && $locale[1] != '=' && self::hasLocale($locale)) {
						return $locale;
					}
				}
			}
			// default
			return 'de';
	 
    
  }

  public static function getShortLocale() { 
    
    
    // --- BLOCK: main ---
    
		return substr(self::$locale, 0, 2);
	 
    
  }

  public static function getAngularLocale() { 
    
    
    // --- BLOCK: main ---
    
		return str_replace('_', '-', strtolower(self::$locale));
	
    
  }

  public static function getLocaleUTF8() { 
    
    
    // --- BLOCK: main ---
    
		return self::$locale . '.UTF-8';
	 
    
  }

  public static function ensureIsLoaded() { 
    
    
    // --- BLOCK: main ---
    
  // Can be called to ensure class is loaded and initialized

    
  }

  public static function formatNumber($n) { 
    
    
    // --- BLOCK: main ---
    
        return $n;
    
    
  }

  public static function parseNumber($x) { 
    
    
    // --- BLOCK: main ---
    
        return intval($x);
    
    
  }

  public static function parseFloat($x) { 
    
    
    // --- BLOCK: main ---
    
        return floatval($x);
    
    
  }

  public static function setLocale($l) { 
    
    
    // --- BLOCK: main ---
    
        self::$locale = $l;
        if(self::$locale == 'en') self::$locale = 'en_US';
        if(self::$locale == 'de') self::$locale = 'de_DE';
        
        if(self::$locale != 'de_DE' && self::$locale != 'de_CH' && self::$locale != 'en_US' && self::$locale != 'en_SG') self::$locale = ('de_DE');
        putenv("LANGUAGE=" . self::getLocaleUTF8());
        putenv("LC_ALL=" . self::getLocaleUTF8());
        $r = setlocale(LC_ALL, self::getLocaleUTF8());
        if(!$r){
            error_log('Could not set locale to ' . self::getLocaleUTF8());
            self::$locale = "de_DE";
            putenv("LC_ALL=" . self::getLocaleUTF8());
            $r = setlocale(LC_ALL, self::getLocaleUTF8());
        }

        bindtextdomain('messages', dirname(__FILE__) . '/../../i18n/');
        textdomain('messages');
        bind_textdomain_codeset('messages', 'UTF-8');
		  if (basename($_SERVER['SCRIPT_NAME']) != "setup.php" && basename($_SERVER['SCRIPT_NAME']) != "dbcheck.php") {
			  if (\FOP\Database::getDriver() == 'mysql') {
				  \FOP\Database::query('SELECT @fop_locale := ' . \FOP\Database::quote(self::getLocale()));
			  } else {
				  \FOP\Database::query('SET SESSION "fop.locale" = ' . \FOP\Database::quote(self::getLocale()));
			  }
		  }
    
    
  }

  public static function getLocale() { 
    
    
    // --- BLOCK: getter ---
    return self::$locale;
    
  }

  public static function initialize() { 
    
    
    // --- BLOCK: setlang ---
    $l = ('de_DE');
        self::setLocale($l);
    
    // --- BLOCK: z initmoment ---
    
	if (!\FOP\Configuration::isActive() && basename($_SERVER['SCRIPT_NAME']) != "dbcheck.php") {
		/* TODO: Dieser Code ist broken für z.B. LaunixInternERP. Bitte prüfen @tumm
		try {
			\Moment\Moment::setLocale(\FOP\I18n::getLocale());
		} catch (\Moment\MomentException $e) {
			\Moment\Moment::setLocale('en_US');
		}
		*/
	}

    
  }


  
  

  
  
}

\FOP\I18n::initialize(); 

