Welcome Guest, Not a member yet? Register   Sign In
Improved Language class
#1

[eluser]xwero[/eluser]
Guess what i'm developing today Wink

I needed the possibility to load multiple files from different languages so i hacked the Language library
Code:
function load($langfile = '', $idiom = '')
    {    
        $langfile = str_replace(EXT, '', str_replace('_lang.', '', $langfile)).'_lang'.EXT;
        
        if ($idiom == '') // rearranged
        {
            $CI =& get_instance();
            $deft_lang = $CI->config->item('language');
            $idiom = ($deft_lang == '') ? 'english' : $deft_lang;
        }
        
        if (count($this->is_loaded) > 0 && is_array($this->is_loaded[$idiom]) && in_array($langfile, $this->is_loaded[$idiom], TRUE)) // changed *
        {
            return;
        }
        
        // Determine where the language file is and load it
        if (file_exists(APPPATH.'language/'.$idiom.'/'.$langfile))
        {
            include(APPPATH.'language/'.$idiom.'/'.$langfile);
        }
        else
        {        
            if (file_exists(BASEPATH.'language/'.$idiom.'/'.$langfile))
            {
                include(BASEPATH.'language/'.$idiom.'/'.$langfile);
            }
            else
            {
                show_error('Unable to load the requested language file: language/'.$langfile);
            }
        }

        if ( ! isset($lang))
        {
            log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
            return;
        }
        
        
        if(!isset($this->language[$idiom])) // added **
        {
                   $this->is_loaded[$idiom] = array($langfile);            
                   $this->language[$idiom] = $lang;
        }
        else
        {
              array_push($this->is_loaded[$idiom],$langfile);
                      $this->language[$idiom] = array_merge($this->language[$idiom], $lang); // original line changed
        }
        unset($lang);
        
        log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
        return TRUE;
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Fetch a single line of text from the language array
     *
     * @access    public
     * @param    string    the language line
     * @return    string
     */
    function line($line = '',$idiom = '') // changed
    {
        if ($idiom == '') // added
        {
            $CI =& get_instance();
            $idiom = $CI->config->item('language');
        }
        return ($line == '' OR ! isset($this->language[$idiom][$line])) ? FALSE : $this->language[$idiom][$line]; // changed
    }
I think this will make the library more flexible. And because it's for the most part an internal change no applications will break.

edit: * changed because it generated errors when there is an empty array or the idiom key wasn't found.
** newbie array mistake. overwriting values because same key.




Theme © iAndrew 2016 - Forum software by © MyBB