Welcome Guest, Not a member yet? Register   Sign In
Add, Remove, and Change Language Items Dynamically(Tested in 2.2.6)
#1

I found that I had to manipulate my CI language files from forms when managing database content.  My situation was that I had a database of listings which were grouped into categories.  My site was obviously multilingual and I wanted the titles of the categories to be dictated by language files so all the language items were in the same place.  

With this extension, you can easily find which languages are installed in your application.  Optionally, you can specify to look in the system/language folder instead if you are using languages that are shared by multiple applications.  You can also look for languages in other applications by adding FCPATH.'/myapplication/language to the parameters when calling the find method.

Example
Code:
//This code finds languages in the language path of the current application
$existing_languages = $this->lang->find();
foreach($existing_languages as $lang){
   $options[$lang] = ucfirst($lang);
}
echo form_dropdown('languages', $options);


You can add lines to either a specific language(english, german, etc) or in all languages(not sure why you would want to do this, but hey, why not?)  See below.

Code:
$existing_languages = $this->lang->find();
foreach($existing_languages as $lang){
   echo '<input type="text" name="'.$lang.'_title.'">
}
foreach($existing_languages as $lang){
  $this->lang->add_line('cat_'.$this->db->insert_id(), $this->input->post($lang.'_title'), 'category_names', $lang);
}

Changing existing lines is just as easy.
Code:
$existing_languages = $this->lang->find();
foreach($existing_languages as $lang){
 $this->lang->change_line('cat_'.$cat_id, $this->input->post($lang.'_title'), 'category_names', $lang);
}

Deleting lines...  Probably the only time you'll want to make the same changes to all languages.
Code:
$existing_languages = $this->lang->find();
foreach($existing_languages as $lang){
 $this->lang->delete_line('cat_'.$cat_id, 'category_names');
}

When you make changes to a language file and don't specify a language path(e.g. myapplication/language), it will first look in the language folder of the current application.  If no file is found, it will then check the system/language folder for your file.

I still use CI 2.2.6, so this hasn't been tested in CI 3 or anything else.  I'm not sure how different(if at all) the language class is in versions >= 3.

Make sure all files inside your language directory are writable.

Copy and paste this code into a file named "My_Lang.php" and save it to your application's core directory(e.g. application/core)

Enjoy!  Ask if you have any questions.
Code:
<?php

class MY_Lang extends CI_Lang {

    var $existing_langs = array();
    var $lang_path = APPPATH.'/language';
    var $CI;

    function __construct(){
        parent::__construct();
        $this->existing_langs = $this->find();
    }
    
    function find($alt_path = '')
    {

        $results = ($alt_path != '') ? $alt_path : $this->lang_path;

        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;

            if (is_dir(BASEPATH.'/language' . '/' . $result)) {
                $dirs[] = $result;
            }
        }
        return $dirs;
    }

    public function remove_line($line, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }

                $lang_contents = read_file($file_path);

                $new_contents = preg_replace("^\n\\$"."lang\['$line'\] = '(.*?)';^", '', $lang_contents);

                write_file($file_path, $new_contents, 'w+');

            }

        } else { //Apply only to specified language

            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error("Could not find the requested language file.");

                }

            }

            $lang_contents = read_file($file_path);

            $new_contents = preg_replace("^\n\\$"."lang\['$line'\] = '(.*?)';^", '', $lang_contents);

            write_file($file_path, $new_contents, 'w+');

        }

    }

    public function add_line($line, $value, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }

                $lang_contents = read_file($file_path);

                $new_contents = $lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";

                write_file($file_path, $new_contents, 'w+');

            }

        } else { //Apply only to specified languages


            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error($file_path);

                }

            }

            $lang_contents = read_file($file_path);

            $new_contents = $lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";

            write_file($file_path, $new_contents, 'w+');

        }

    }

    public function change_line($line, $value, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }
                $lang_contents = read_file($file_path);
                $new_contents = preg_replace("^\\$"."lang\['$line'\] = '(.*?)';^", '$lang'."['".$line."'] = '$value';", $lang_contents);

                write_file($file_path, $new_contents, 'w+');
            }

        } else {

            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'/language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error("Could not find the requested language file.");

                }

            }
            $lang_contents = read_file($file_path);
            $new_contents = preg_replace("^\\$"."lang\['$line'\] = '(.*?)';^", '$lang'."['".$line."'] = '$value';", $lang_contents);

            write_file($file_path, $new_contents, 'w+');

        }

    }
}
Reply
#2

(This post was last modified: 01-28-2018, 12:10 AM by ciadmin. Edit Reason: fix bbcode tags )

(02-26-2016, 11:41 PM)ShoeLace1291 Wrote: Hey ShoeLace1291! 

Thanks a lot for this awesome class! I used it in version 3 and I had to remove extra slashes after BASEPATH and APPPATH. I updated the code below
Code:
class MY_Lang extends CI_Lang {

    var $existing_langs = array();
    var $lang_path = APPPATH.'language';
    var $CI;

    function __construct(){
        parent::__construct();
        $this->existing_langs = $this->find();
    }
    
    function find($alt_path = '') {

        $results = scandir(($alt_path != '') ? $alt_path : $this->lang_path);

        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;

            if (is_dir(BASEPATH.'language' . '/' . $result)) {
                $dirs[] = $result;
            }
        }
        return $dirs;
    }

    public function remove_line($line, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }

                $lang_contents = read_file($file_path);

                $new_contents = preg_replace("^\n\\$"."lang\['$line'\] = '(.*?)';^", '', $lang_contents);

                write_file($file_path, $new_contents, 'w+');

            }

        } else { //Apply only to specified language

            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error("Could not find the requested language file.");

                }

            }

            $lang_contents = read_file($file_path);

            $new_contents = preg_replace("^\n\\$"."lang\['$line'\] = '(.*?)';^", '', $lang_contents);

            write_file($file_path, $new_contents, 'w+');

        }

    }

    public function add_line($line, $value, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }

                $lang_contents = read_file($file_path);

                $new_contents = $lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";

                write_file($file_path, $new_contents, 'w+');

            }

        } else { //Apply only to specified languages


            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error($file_path);

                }

            }

            $lang_contents = read_file($file_path);

            $new_contents = $lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";

            write_file($file_path, $new_contents, 'w+');

        }

    }

    public function change_line($line, $value, $file, $lang = ''){

        $CI =& get_instance();
        $CI->load->helper('file');

        if($lang == ''){ //Apply to all languages

            foreach($this->existing_langs as $lang){

                $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                    if(!file_exists($file_path)){

                        show_error("Could not find the requested language file.");

                    }

                }
                $lang_contents = read_file($file_path);
                $new_contents = preg_replace("^\\$"."lang\['$line'\] = '(.*?)';^", '$lang'."['".$line."'] = '$value';", $lang_contents);

                write_file($file_path, $new_contents, 'w+');
            }

        } else {

            $file_path = $this->lang_path.'/'.$lang.'/'.$file.'_lang.php';
            if(!file_exists($file_path)){

                $file_path = BASEPATH.'language/'.$lang.'/'.$file.'_lang.php';
                if(!file_exists($file_path)){

                    show_error("Could not find the requested language file.");

                }

            }
            $lang_contents = read_file($file_path);
            $new_contents = preg_replace("^\\$"."lang\['$line'\] = '(.*?)';^", '$lang'."['".$line."'] = '$value';", $lang_contents);

            write_file($file_path, $new_contents, 'w+');

        }

    }
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB