<commit_before><?php 
/**
 * WLang.php
 */

defined('IN_WITY') or die('Access denied');

/**
 * WLang manages everything about languages.
 * 
 * Language values are defined within application in XML files.
 * This class will calculate the most suitable language for the current user
 * and load the corresponding language files.
 * 
 * @package System\WCore
 * @author xpLosIve
 * @author <NAME> <<EMAIL>>
 * @version 0.5.0-dev-06-03-2013
 */
class WLang {
	/**
	 * @var array Languages to use in order of priority
	 */
	private static $languages = array();
	
	/**
	 * @var array List of language directories registered
	 */
	private static $lang_dirs = array();
	
	/**
	 * @var array Language values associated to their constant
	 */
	private static $values = array();
	
	/**
	 * Initializes the Lang template compiler and the language of the user.
	 */
	public static function init() {
		// Init template handler
		WSystem::getTemplate();
		WTemplateCompiler::registerCompiler('lang', array('WLang', 'compile_lang'));
		
		// Set session lang as top priority
		if (!empty($_SESSION['lang'])) {
			self::addLang($_SESSION['lang']);
		}
		
		// Use browser languages
		self::addLang(self::getBrowserLang());
		
		// Add config lang
		self::addLang(WConfig::get('config.lang'));
	}
	
	/**
	 * Parses the languages received from the browser through header Accept-Language.
	 * See your browser language configuration to manage your priority list.
	 * 
	 * @return array List of languages prioritized
	 */
	public static function getBrowserLang() {
		if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
			return array();
		}
		
		// Exemple of $http_lang: 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4'
		// q-value is a ponderation: nothing or 1 means favorite lang, 0 means to avoid
		$http_lang = trim($_SERVER['HTTP_ACCEPT_LANGUAGE']);
		$priority_lang = array();
		
		if (!empty($http_lang)) {
			$accept_lang = explode(',', $http_lang);
			
			foreach ($accept_lang as $lang_and_q) {
				$detail = explode(';', $lang_and_q);
				$lang = strtolower(substr($detail[0], 0, 2));
				
				if (strlen($lang) == 2) { // Lang must contain 2 chars (ex: 'en')
					// Find q-value
					if (sizeof($detail) == 1) {
						$q_value = 1;
					} else {
						$q_value = floatval(substr($detail[1], 2, 4));
					}
					
					// Updates the $priority_lang array
					if ($q_value > 0) {
						if (array_key_exists($lang, $priority_lang)) {
							// Lang already found but q is higher
							if ($q_value > $priority_lang[$lang]) {
								$priority_lang[$lang] = $q_value;
							}
						} else {
							$priority_lang[$lang] = $q_value;
						}
					}
				}
			}
		}
		
		// q-values sorting and final extraction of the ordered keys
		arsort($priority_lang);
		return array_keys($priority_lang);
	}
	
	/**
	 * Adds a language to load for the current session.
	 * 
	 * @param string $lang Language name (2 letters identifier)
	 */
	public static function addLang($lang) {
		if (is_array($lang)) {
			foreach ($lang as $l) {
				self::addLang($l);
			}
		} else {
			$lang = strtolower(substr($lang, 0, 2));
			if (!in_array($lang, self::$languages)) {
				self::$languages[] = $lang;
			}
		}
	}
	
	/**
	 * Returns the top priority language for the user.
	 * 
	 * @return string Top priority language
	 */
	public static function getLang() {
		if (isset(self::$languages[0])) {
			return self::$languages[0];
		}
		
		return '';
	}
	
	/**
	 * Returns the list of the prefered languages according to the user configuration.
	 * 
	 * @return array List of prioritized languages
	 */
	public static function getPriorityLang() {
		return self::$languages;
	}
	
	/**
	 * Assigns a new language constant.
	 * If the constant was already defined, it will keep the previous value by default.
	 *
	 * @param string $name  name as it is in the lang file
	 * @param string $value value as it is after compiling the lang file
	 * @param bool $overwrite Forces the reassignement of a new value.<commit_msg>Updates WLang<commit_after><?php 
/**
 * WLang.php
 */

defined('IN_WITY') or die('Access denied');

/**
 * WLang manages everything about languages.
 * 
 * Language values are defined within application in XML files.
 * This class will calculate the most suitable language for the current user
 * and load the corresponding language files.
 * 
 * @package System\WCore
 * @author xpLosIve
 * @author <NAME> <<EMAIL>>
 * @version 0.5.0-dev-06-03-2013
 */
class WLang {
	/**
	 * @var array Languages to use in order of priority
	 */
	private static $languages = array();
	
	/**
	 * @var array List of language directories registered
	 */
	private static $lang_dirs = array();
	
	/**
	 * @var array Language values associated to their constant
	 */
	private static $values = array();
	
	/**
	 * Initializes the Lang template compiler and the language of the user.
	 */
	public static function init() {
		// Init template handler
		WSystem::getTemplate();
		WTemplateCompiler::registerCompiler('lang', array('WLang', 'compile_lang'));
		
		// Set session lang as top priority
		if (!empty($_SESSION['lang'])) {
			self::addLang($_SESSION['lang']);
		}
		
		// Use browser languages
		self::addLang(self::getBrowserLang());
		
		// Add config lang
		self::addLang(WConfig::get('config.lang'));
	}
	
	/**
	 * Parses the languages received from the browser through header Accept-Language.
	 * See your browser language configuration to manage your priority list.
	 * 
	 * @return array List of languages prioritized
	 */
	public static function getBrowserLang() {
		if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
			return array();
		}
		
		// Exemple of $http_lang: 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4'
		// q-value is a ponderation: nothing or 1 means favorite lang, 0 means to avoid
		$http_lang = trim($_SERVER['HTTP_ACCEPT_LANGUAGE']);
		$priority_lang = array();
		
		if (!empty($http_lang)) {
			$accept_lang = explode(',', $http_lang);
			
			foreach ($accept_lang as $lang_and_q) {
				$detail = explode(';', $lang_and_q);
				$lang = strtolower(substr($detail[0], 0, 2));
				
				if (strlen($lang) == 2) { // Lang must contain 2 chars (ex: 'en')
					// Find q-value
					if (sizeof($detail) == 1) {
						$q_value = 1;
					} else {
						$q_value = floatval(substr($detail[1], 2, 4));
					}
					
					// Updates the $priority_lang array
					if ($q_value > 0) {
						if (array_key_exists($lang, $priority_lang)) {
							// Lang already found but q is higher
							if ($q_value > $priority_lang[$lang]) {
								$priority_lang[$lang] = $q_value;
							}
						} else {
							$priority_lang[$lang] = $q_value;
						}
					}
				}
			}
		}
		
		// q-values sorting and final extraction of the ordered keys
		arsort($priority_lang);
		return array_keys($priority_lang);
	}
	
	/**
	 * Adds a language to load for the current session.
	 * 
	 * @param string $lang Language name (2 letters identifier)
	 */
	public static function addLang($lang) {
		if (is_array($lang)) {
			foreach ($lang as $l) {
				self::addLang($l);
			}
		} else {
			$lang = strtolower(substr($lang, 0, 2));
			if (!in_array($lang, self::$languages)) {
				self::$languages[] = $lang;
			}
		}
	}
	
	/**
	 * Returns the top priority language for the user.
	 * 
	 * @return string Top priority language
	 */
	public static function getLang() {
		if (!empty($_SESSION['lang'])) {
			return $_SESSION['lang'];
		} else {
			if (isset(self::$languages[0])) {
				return self::$languages[0];
			}
			
			return '';
		}
	}
	
	public static function getLangID() {
		$lang = self::getLang();
		
		switch ($lang) {
			default:
			case 'fr':
				return 1;
			
			case 'en':
				return 2;
			
			case 'es':
				return 3;
		}
	}
	
	/**
	 * Returns the list of the prefered languages according to the user configuration.
	 * 
	 * @return array List of prioritized languages
	 */
	public static function getPriorityLang() {
		return self::$languages;
	}
	
	/**
	 * Assigns a new language constant.
	 * If the constant was already defined, it will keep the previous value by default.
	 *
	 * @param string $name  name as it is in the lang file
	 * @param string $value value as it is after compiling the lang file
	 * @param bool $overwrite Forces the reassignement of a new value.
{'fraction_non_alphanumeric': 0.13592114869798005, 'fraction_numerical': 0.00888293988805062, 'mean_word_length': 3.060770750988142, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1422999', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2504, 'n_words': 808}