<commit_before><?php
namespace AmidaMVC\Tools;

class i18n
{
    // +-------------------------------------------------------------+
    const FILE_HEADER_TEXT = 'i18n.text.';
    protected $config      = array();
    /** @var \AmidaMVC\Tools\Load */
    protected $load        = NULL;
    protected $directory   = '_Config/';
    protected $language    = '';
    protected $textSection = '';
    protected $textData    = array();
    protected $fileToLoad  = array();
    protected $listOfLang  = array(
        'en' => 'English',
        'es' => 'español',
        'fr' => 'French',
        'ja' => '日本語',
        'ko' => '贛語',
        'zh' => '中文',
    );
    // +-------------------------------------------------------------+
    function __construct( $config=array() ) {
        $this->config = $config;
    }
    function _init( $config=array() ) {
        $this->config = array_merge( $this->config, $config );
        $this->_setup();
    }
    function _setup() {
        if( empty( $this->config ) ) {
            return ;
        }
        foreach( $this->config as $key => $val ) {
            if( substr( $key, 0, 5 ) == 'file_' ) {
                $this->fileToLoad[ $val ] = TRUE;
            }
            elseif( $key == 'language' ) {
                $this->language( $val );
            }
            elseif( $key == 'directory' ) {
                $this->directory( $val );
            }
            elseif( $key == 'loadFiles' ) {
                $this->loadFiles();
            }
        }
    }
    function injectLoad( $load ) {
        $this->load = $load;
    }
    function loadFiles() {
        if( !empty( $this->fileToLoad ) ) {
            foreach( $this->fileToLoad as $file => $val ) {
                $this->_loadTextFile( $file );
            }
        }
    }
    function _loadTextFile( $filename ) {
        $loadFile = $this->directory . self::FILE_HEADER_TEXT . $filename . '.'.$this->language;
        $found = $this->load->findFile( $loadFile.'.ini' );
        if( $found ) {
            $textData = $this->load->parse_ini( $found );
            $this->_mergeText( $textData );
        }
    }
    function _mergeText( $textData ) {
        foreach( $textData as $section => $data ) {
            if( isset( $this->textData[ $section ] ) ) {
                $this->textData[ $section ] = array_merge( $this->textData[ $section ], $data );
            }
            else {
                $this->textData[ $section ] = $data;
            }
        }
    }
    // +-------------------------------------------------------------+
    function directory( $dir='_Config/' ) {
        $this->directory = (substr( $dir, -1, 1 )=='/')?$dir:$dir.'/';
    }
    function extension( $ext='ini' ) {
        $this->extension = $ext;
    }
    function language( $lang ) {
        $this->config[ 'language' ] = $lang;
        $this->language = substr( $lang, 0, 2 );
    }
    function langCode( $code ) {
        return (isset( $this->listOfLang[$code])) ? $this->listOfLang[$code]:FALSE;
    }
    // +-------------------------------------------------------------+
    function textSection( $section ) {
        $this->textSection = $section;
    }
    function text( $text ) {
        if( !isset( $this->textData[ $this->textSection ][ $text ] ) ) {
            return $text;
        }
        $words = $this->textData[ $this->textSection ][ $text ];
        $args  = func_get_args();
        array_shift( $args );
        if( !empty( $args ) ) $words = $this->_replace( $words, $args );
        return $words;
    }
    function _replace( $words, $args ) {
        foreach( $args as $key => $val ) {
            if( is_array( $val ) ) {
                $words = $this->_replace( $words, $val );
            }
            else {
                $words = str_replace( ":{$key}", $val, $words );
            }
        }
        return $words;
    }
    // +-------------------------------------------------------------+
}<commit_msg>add html method to return text as html (nl2br).<commit_after><?php
namespace AmidaMVC\Tools;

class i18n
{
    // +-------------------------------------------------------------+
    const FILE_HEADER_TEXT = 'i18n.text.';
    protected $config      = array();
    /** @var \AmidaMVC\Tools\Load */
    protected $load        = NULL;
    protected $directory   = '_Config/';
    protected $language    = '';
    protected $textSection = '';
    protected $textData    = array();
    protected $fileToLoad  = array();
    protected $listOfLang  = array(
        'en' => 'English',
        'es' => 'español',
        'fr' => 'French',
        'ja' => '日本語',
        'ko' => '贛語',
        'zh' => '中文',
    );
    // +-------------------------------------------------------------+
    function __construct( $config=array() ) {
        $this->config = $config;
    }
    function _init( $config=array() ) {
        $this->config = array_merge( $this->config, $config );
        $this->_setup();
    }
    function _setup() {
        if( empty( $this->config ) ) {
            return ;
        }
        foreach( $this->config as $key => $val ) {
            if( substr( $key, 0, 5 ) == 'file_' ) {
                $this->fileToLoad[ $val ] = TRUE;
            }
            elseif( $key == 'language' ) {
                $this->language( $val );
            }
            elseif( $key == 'directory' ) {
                $this->directory( $val );
            }
            elseif( $key == 'loadFiles' ) {
                $this->loadFiles();
            }
        }
    }
    function injectLoad( $load ) {
        $this->load = $load;
    }
    function loadFiles() {
        if( !empty( $this->fileToLoad ) ) {
            foreach( $this->fileToLoad as $file => $val ) {
                $this->_loadTextFile( $file );
            }
        }
    }
    function _loadTextFile( $filename ) {
        $loadFile = $this->directory . self::FILE_HEADER_TEXT . $filename . '.'.$this->language;
        $found = $this->load->findFile( $loadFile.'.ini' );
        if( $found ) {
            $textData = $this->load->parse_ini( $found );
            $this->_mergeText( $textData );
        }
    }
    function _mergeText( $textData ) {
        foreach( $textData as $section => $data ) {
            if( isset( $this->textData[ $section ] ) ) {
                $this->textData[ $section ] = array_merge( $this->textData[ $section ], $data );
            }
            else {
                $this->textData[ $section ] = $data;
            }
        }
    }
    // +-------------------------------------------------------------+
    function directory( $dir='_Config/' ) {
        $this->directory = (substr( $dir, -1, 1 )=='/')?$dir:$dir.'/';
    }
    function extension( $ext='ini' ) {
        $this->extension = $ext;
    }
    function language( $lang ) {
        $this->config[ 'language' ] = $lang;
        $this->language = substr( $lang, 0, 2 );
    }
    function langCode( $code ) {
        return (isset( $this->listOfLang[$code])) ? $this->listOfLang[$code]:FALSE;
    }
    // +-------------------------------------------------------------+
    function textSection( $section ) {
        $this->textSection = $section;
    }
    function html() {
        $args  = func_get_args();
        $text = call_user_func_array( array( $this, 'text' ), $args );
        return nl2br( $text );
    }
    function text( $text ) {
        if( !isset( $this->textData[ $this->textSection ][ $text ] ) ) {
            return $text;
        }
        $words = $this->textData[ $this->textSection ][ $text ];
        $args  = func_get_args();
        array_shift( $args );
        if( !empty( $args ) ) $words = $this->_replace( $words, $args );
        return $words;
    }
    function _replace( $words, $args ) {
        foreach( $args as $key => $val ) {
            if( is_array( $val ) ) {
                $words = $this->_replace( $words, $val );
            }
            else {
                $words = str_replace( ":{$key}", $val, $words );
            }
        }
        return $words;
    }
    // +-------------------------------------------------------------+
}
{'fraction_non_alphanumeric': 0.23811283567381128, 'fraction_numerical': 0.0027100271002710027, 'mean_word_length': 1.3417940582636285, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 99, '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': '4794594', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2380, 'n_words': 493}