<commit_before><?php


/**
 * File contains contains class creating simple translation
 *
 * @package    coslib
 */

/**
 * Class for doing simple translations
 *
 * @package    coslib
 */
class lang {
    
    /**
     * flag indicating if all translations are loaded from single file
     * @var type 
     */
    public static $allLoaded = false;

    /**
     * var holding the language to use for a site
     * @var string $language 
     */
    public static $language = '';

    /**
     * var holding the translation table
     * @var array $dict
     */
    public static $dict = array ();

    /**
     * method for getting the language of the site. 
     * @return string $language the language to be used
     */
    public static function getLanguage (){
        return self::$language;
    }
    
    /**
     * method for initing and loading correct language
     * includes translations found in database (system)
     * 
     */
    public static function init(){
        
        self::$language = config::getMainIni('language');

        $system_lang = array();
        $db = new db();
        $system_language = $db->select(
            'language',
            'language',
            config::$vars['coscms_main']['language']
        );

        // create system lanugage for all modules
        if (!empty($system_language)){
            foreach($system_language as $key => $val){
                $module_lang = unserialize($val['translation']);
                $system_lang+= $module_lang;
            }
        }      
        self::$dict = $system_lang;
    }


    /**
     * method for doing translations. If a translation is not found we
     * prepend the untranslated string with 'NT' (needs translation)
     *
     * @param   string  $sentence the sentence to translate.
     * @param   array   $substitute array with substitution to perform on sentence.
     *                  e.g. array ('100$', 'username')
     *                  in the string to be translated you will then have e.g.
     *                  $_COS_LANG_MODULE['module_string'] = 
     *                  "You will be charged %1% dear %2%"
     * @return  string  $str translated string
     *                  if no translation is found in translation registry,
     *                  the string suplied will have "NT: " prepended. 
     *                  (Not Translated)
     */
    public static function translate($sentence, $substitute = array(), $options = array ()){
        if (isset($options['no_translate'])) {
            return $sentence;
        }
        if (isset(self::$dict[$sentence])){
            if (!empty($substitute)){
                $i = 1;
                foreach ($substitute as $val) {
                    self::$dict[$sentence] = str_replace("%$i%", $val, self::$dict[$sentence]);
                    $i++;
                }
            }
            
            return self::$dict[$sentence];
        } else {
            // don't add NT
            if (isset(config::$vars['coscms_main']['translate_ignore'])) {
                return $sentence;
            } else {
                return "NT: $sentence";
            }
        }
    }
    
    /**
     * method for doing translations. The method calls translate. 
     * and it is an alias. But: In order to auto translate modules, 
     * you should use this function if you call translation found
     * in the system module. E.g. for default submit buttons.
     * This is because the ./coscli.sh translate commend looks 
     * for strings which uses lang::translate('string);   
     *
     * @param   string  $sentence the sentence to translate.
     * @param   array   array with substitution to perform on sentence.
     *                  e.g. array ('a name', 'a adresse')<commit_msg>change how to substitue vars in translation<commit_after><?php


/**
 * File contains contains class creating simple translation
 *
 * @package    coslib
 */

/**
 * Class for doing simple translations
 *
 * @package    coslib
 */
class lang {
    
    /**
     * flag indicating if all translations are loaded from single file
     * @var type 
     */
    public static $allLoaded = false;

    /**
     * var holding the language to use for a site
     * @var string $language 
     */
    public static $language = '';

    /**
     * var holding the translation table
     * @var array $dict
     */
    public static $dict = array ();

    /**
     * method for getting the language of the site. 
     * @return string $language the language to be used
     */
    public static function getLanguage (){
        return self::$language;
    }
    
    /**
     * method for initing and loading correct language
     * includes translations found in database (system)
     * 
     */
    public static function init(){
        
        self::$language = config::getMainIni('language');

        $system_lang = array();
        $db = new db();
        
        $system_language = db_q::select('language')->
                filter('language =', config::$vars['coscms_main']['language'])->
                condition('AND')->
                filter('module_name != ', 'language_all')->
                fetch();

        // create system lanugage for all modules
        if (!empty($system_language)){
            foreach($system_language as $key => $val){
                $module_lang = unserialize($val['translation']);
                
                $system_lang+= $module_lang;
            }
        }      
        self::$dict = $system_lang;
    }


    /**
     * method for doing translations. If a translation is not found we
     * prepend the untranslated string with 'NT' (needs translation)
     *
     * @param   string  $sentence the sentence to translate.
     * @param   array   $substitute array with substitution to perform on sentence.
     *                  e.g. array ('100$', 'username')
     *                  in the string to be translated you will then have e.g.
     *                  $_COS_LANG_MODULE['module_string'] = 
     *                  "You will be charged %1% dear %2%"
     * @return  string  $str translated string
     *                  if no translation is found in translation registry,
     *                  the string suplied will have "NT: " prepended. 
     *                  (Not Translated)
     */
    public static function translate($sentence, $substitute = array(), $options = array ()){
        if (isset($options['no_translate'])) {
            return $sentence;
        }
        if (isset(self::$dict[$sentence])){
            if (!empty($substitute)){
                foreach ($substitute as $key => $val) {
                    self::$dict[$sentence] = str_replace("{$key}", $val, self::$dict[$sentence]);
                }
            }
            
            return self::$dict[$sentence];
        } else {
            // don't add NT
            if (isset(config::$vars['coscms_main']['translate_ignore'])) {
                return $sentence;
            } else {
                return "NT: $sentence";
            }
        }
    }
    
    /**
     * method for doing translations. The method calls translate. 
     * and it is an alias. But: In order to auto translate modules, 
     * you should use this function if you call translation found
     * in the system module. E.g. for default submit buttons.
     * This is because the ./coscli.sh translate commend looks 
     * for strings which uses lang::translate('string);   
     *
     * @param   string  $sentence the sentence to translate.
     * @param   array   array with substitution to perform on sentence.
     *                  e.g. array ('a name', 'a adresse')
{'fraction_non_alphanumeric': 0.10950708804786058, 'fraction_numerical': 0.001430615164520744, 'mean_word_length': 1.7395796223726399, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '296850', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1913, 'n_words': 660}