<commit_before><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2009-14 <NAME>
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version. 
 *
 * Xibo 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('XIBO') 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
{
    private static $locale;
    private static $jsLocale;

	/**
	 * Gets and Sets the Locale
     * @param $language string[optional] The Language to Load
	 */
	public static function InitLocale($language = NULL)
	{
        $localeDir	= 'locale';
        $default = ($language == NULL) ? Config::GetSetting('DEFAULT_LANGUAGE') : $language;

        global $transEngine;
        global $stream;

        //Debug::LogEntry('audit', 'IN', 'TranslationEngine', 'InitLocal');
        // Build an array of supported languages
        $supportedLanguages = scandir($localeDir);

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

        // If we don't have a language, try from HTTP accept
        if ($requestedLanguage == '' && Config::GetSetting('DETECT_LANGUAGE') == 1) {
            // Parse the language header and build a preference array
            $languagePreferenceArray = TranslationEngine::parseHttpAcceptLanguageHeader();

            if (count($languagePreferenceArray) > 0) {

                // Go through the list until we have a match
                foreach ($languagePreferenceArray as $languagePreference => $preferenceRating) {

                    // We don't ship an en.mo, so fudge in a case where we automatically convert that to en_GB
                    if ($languagePreference == 'en')
                        $languagePreference = 'en_GB';

                    if (in_array(str_replace('-', '_', $languagePreference) . '.mo', $supportedLanguages)) {
                        $foundLanguage = $languagePreference;
                        break;
                    }
                }
            }
        }
        else {
            // Requested language is not empty, so check it
            // This may also be because the headers are empty for some reason

            // Firstly, Sanitize it
            $requestedLanguage = str_replace('-', '_', $requestedLanguage);

            // Check its valid
            if (in_array($requestedLanguage . '.mo', $supportedLanguages)) {
                $foundLanguage = $requestedLanguage;
            }
            else {
                // Fall back
                $foundLanguage = 'en_GB';
            }
        }

        // Are we still empty?
        if ($foundLanguage == '')
            $foundLanguage = $default;

        //Debug::LogEntry('audit', 'Creating new file streamer for '. $localeDir . '/' . $foundLanguage . '.mo', 'TranslationEngine', 'InitLocal');
        if (!$stream = new CachedFileReader($localeDir . '/' . $foundLanguage . '.mo')) {
            $transEngine = false;
            return;
        }

        $transEngine = new gettext_reader($stream);
        self::$locale = $foundLanguage;
        self::$jsLocale = str_replace('_', '-', $foundLanguage);
	}

    /**<commit_msg>Regression in default language selector. xibosignage/xibo#97<commit_after><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2009-14 <NAME>
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version. 
 *
 * Xibo 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('XIBO') 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
{
    private static $locale;
    private static $jsLocale;

	/**
	 * Gets and Sets the Locale
     * @param $language string[optional] The Language to Load
	 */
	public static function InitLocale($language = NULL)
	{
        $localeDir	= 'locale';
        $default = ($language == NULL) ? Config::GetSetting('DEFAULT_LANGUAGE') : $language;

        global $transEngine;
        global $stream;

        //Debug::LogEntry('audit', 'IN', 'TranslationEngine', 'InitLocal');
        // Build an array of supported languages
        $supportedLanguages = scandir($localeDir);

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

        // If we don't have a language, try from HTTP accept
        if ($requestedLanguage == '' && Config::GetSetting('DETECT_LANGUAGE') == 1) {
            // Parse the language header and build a preference array
            $languagePreferenceArray = TranslationEngine::parseHttpAcceptLanguageHeader();

            if (count($languagePreferenceArray) > 0) {

                // Go through the list until we have a match
                foreach ($languagePreferenceArray as $languagePreference => $preferenceRating) {

                    // We don't ship an en.mo, so fudge in a case where we automatically convert that to en_GB
                    if ($languagePreference == 'en')
                        $languagePreference = 'en_GB';

                    if (in_array(str_replace('-', '_', $languagePreference) . '.mo', $supportedLanguages)) {
                        $foundLanguage = $languagePreference;
                        break;
                    }
                }
            }
        }
        else if ($requestedLanguage == '') {
            // Set the language to the default
            $foundLanguage = $default;
        }
        else {
            // Requested language is not empty, so check it
            // This may also be because the headers are empty for some reason

            // Firstly, Sanitize it
            $requestedLanguage = str_replace('-', '_', $requestedLanguage);

            // Check its valid
            if (in_array($requestedLanguage . '.mo', $supportedLanguages)) {
                $foundLanguage = $requestedLanguage;
            }
            else {
                // Fall back
                $foundLanguage = 'en_GB';
            }
        }

        // Are we still empty?
        if ($foundLanguage == '')
            $foundLanguage = $default;

        //Debug::LogEntry('audit', 'Creating new file streamer for '. $localeDir . '/' . $foundLanguage . '.mo', 'TranslationEngine', 'InitLocal');
        if (!$stream = new CachedFileReader($localeDir . '/' . $foundLanguage . '.mo')) {
            Debug::Info('Resolved language ' . $foundLanguage . ' not available.');
            $transEngine = false;
            return;
        }

        $transEngine = new gettext_reader($stream);
        self::$locale = $foundLanguage;
        self::$jsLocale = str_replace('_', '-', $foundLanguage);
	}

    /**
{'fraction_non_alphanumeric': 0.11212563915266618, 'fraction_numerical': 0.0029218407596785976, 'mean_word_length': 2.2304364923318913, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2869566', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2086, 'n_words': 717}