Skip to content
Derek Jones edited this page Jul 5, 2012 · 9 revisions

Category:Libraries::Extended If you want to add your own methods, go ahead

class MY_Language extends CI_Language
{
    // sqllang variables
    var $_get = array();
    var $_set = array();
    var $_in = array();
    
    function MY_Language()
    {
        parent::CI_Language();
    }
    /**
    * Makes switching between languages easier
    *
    *  example : http://codeigniter.com/forums/viewreply/339962/
    */
    function switch_to($idiom)
    {
        $CI =& get_instance();
        if(is_string($idiom) && $idiom != $CI->config->item('language'))
        {
            $CI->config->set_item('language',$idiom);
            $loaded = $this->is_loaded;
            $this->is_loaded = array();
                
            foreach($loaded as $file)
            {
                $this->load(str_replace('_lang.php','',$file));    
            }
        }
    }
    /**
     *  sqllang
     *  sql like syntax to manipulate items from the language library 
     *
     */
     /*
      * select more than one line using the full line keys
      */
    function select($select,$prefix='')
    {
        
        if(is_string($select))
        {
            $temp = explode(',',$select);
            array_walk($temp, create_function('&$v,$k,$pre', '$v = $pre.$v;'),$prefix);
            $this->_get = $temp;
        }
        elseif(is_array($select))
        {
            array_walk($select, create_function('&$v,$k,$pre', '$v = $pre.$v;'),$prefix);
            $this->_get = $select;
        }
    }
    /*
     * select one or more lines using a part of the line key
     */
    function like($like,$side = 'after')
    {
        switch($side)
        {
            case 'before': $pattern = '/^'.$like.'/'; break;
            case 'after': $pattern = '/'.$like.'$/'; break;
            default: $pattern = '/'.$like.'/'; break;
        }
        $keys = array_keys($this->language);
        foreach($keys as $key)
        {
            if(preg_match($pattern,$key) && !in_array($key,$this->_get))
            {
                $this->_get[] = $key;
            }
        }
    }
    /*
     * select one line using the parameter or process select and/or like method
     *
     * examples (php5):
     *
     *  $this->lang->get('line');
     *  $this->lang->select('line1,line2')->get();
     *  $this->lang->select('1,2','line')->get();
     *  $this->lang->like('line')->get(); // gets line,line1,line2
     *  $this->lang->like('1','before')->get(); // gets line1
     *  $this->lang->select('line1','line2')->like('line')->get(); // gets line, line1,line2
     */
    function get($line=null)
    {
        // line key added to get method
        if(is_string($line))
        {
            return (! isset($this->language[$line])) ? FALSE : $this->language[$line];
        }
        // select or like method is used
        if(count($this->_get) > 0)
        {
            $temp = array();
            foreach($this->_get as $line)
            {
                $temp[$line] = (! isset($this->language[$line])) ? FALSE : $this->language[$line];
            }
                return $temp;
        }
        // if none of the possibilities are met
        return false;
    }
    /*
     * set a key-value pair or an array to replace placeholders with actual values
     */
     function set($set,$value=null)
     {
         if(is_string($set) && is_string($value))
         {
             $this->_set[$set] = $value;
         }
         if(is_array($set))
         {
             foreach($set as $key=>$val)
             {
                $this->_set[$key] = $val; 
             }
             
         }
     }
     /*
      * inserts line(s) using 2 parameters or uses the _set array
      *
      */
     function insert($insert,$value=null)
     {
        if(is_string($insert) && is_string($value))
        {
            if(!isset($this->language[$key]))
            {
                $this->language[$insert] = $value;
            }
        }
        if(count($this->_set) > 0)
        {
            foreach($this->_set as $key=>$val)
            {
                if(!isset($this->language[$key]))
                {
                    $this->language[$key] = $val;
                }
            }
        }
     }
     /*
      * update line when parameter is string else update lines set by select,like
      *
      * examples (php5):
      *
      * $this->lang->set('[0]','10')->update('line');
      *
      * $set = array('[0]'=>10,'[1]'=>'you');
      * $this->lang->set($set)->update('line');
      *
      * $this->lang->select('line1','line2')->set('[0]','10')->update();
      * $this->lang->like('line')->set('[0]','10')->update();
      */
     function update($line = null)
     {
         if(is_string($line) && count($this->_set) > 0)
         {
             foreach($this->_set as $key=> $val)
             {
                 $this->language[$line] = str_replace($key,$val,$this->language[$line]);
             }
             return true;
         }
         if(count($this->_get) > 0 && count($this->_set) > 0)
         {
             $i = 0;
             foreach($this->_get as $line)
             {
                 foreach($this->_set as $key2=> $val2)
                 {
                     if(is_array($val2))
                     {
                        $this->language[$line] = str_replace($key2,$val2[$i],$this->language[$line]);
                     }
                     else
                     {
                        $this->language[$line] = str_replace($key2,$val2,$this->language[$line]);
                     }
                 }
                 $i++;
             }
             return true;
         }
         return false;
     }
    /*
      *  add line keys for deletion
      *
      */
     function in($in)
     {
        if(is_string($in))
        {
            $this->_in = array_merge($this->_in,explode(',',$in));
        }
        elseif(is_array($in))
        {
            $this->_in = array_merge($this->_in,$in);
        }
     }
    /*
     * releases file lines or other added lines
     *
     * examples (php5):
     *
     * delete file lines :
     * $this->lang->delete('lang_file');
     * $this->lang->delete('lang_file','language');
     *
     * delete lines :
     * $this->lang->in('line1,line2')->delete();
     * $this->lang->delete('line1');
     */
    function delete($file=null,$idiom = '')
    {
        $langfile = $file.'_lang'.EXT;
        if ($idiom == '')
        {
            $CI =& get_instance();
            $deft_lang = $CI->config->item('language');
            $idiom = ($deft_lang == '') ? 'english' : $deft_lang;
        }
        if(!is_file(APPPATH.'language/'.$idiom.'/'.$langfile))
        {
            if(is_string($file))
            {
                unset($this->language[$file]);
            }
            if(count($this->_in)>0)
            {
                foreach($this->_in as $key)
                {
                   unset($this->language[$key]); 
                }
            }
        }
        else
        {
            if(is_string($file) && in_array($langfile,$this->is_loaded))
            {
                
                include(APPPATH.'language/'.$idiom.'/'.$langfile);
                $keys = array_keys($lang);
                unset($lang);
                foreach($keys as $key)
                {
                    unset($this->language[$key]);
                }
                return true;
            }
        }
        return false;
    }
    
}
Clone this wiki locally