Codeigniter 2: How to switch auto-loaded lang file to another language

From the CodeIgniter User Guide

If you find that you need a specific language globally for your application, you can tell CodeIgniter to automatically load it during system initialization. This is done by opening application / config / autoload.php and adding the language (s) to the autoload array.

I store information about the user's language in the session.

$this->session->set_userdata('lang', $lang);

      

How to change the language (to the language used by the user) for the autoloaded global lang file (the default language is loaded from config.php - which is understandable)

$autoload['language'] = array('global');

      

Is this possible, and if not, how to do it? Do I need to extend CI_Controller?

+3


source to share


2 answers


In the end, I think the best solution is to extend CI_controller Simple like this



class MY_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();

        // Global Lang File
        $this->lang->load('global', $this->session->userdata('lang'));

    }
}

      

+3


source


It is possible, but you will need to load the language you want from the session using:

$this->lang->load('filename', 'language');

      

From the manual:

Where filename is the name of the file you want to download (without the file extension) and language is the set of languages ​​that contains it (for example, English). If the second parameter is missing, the default language specified in your application / config / config.php file will be used.



After loading the desired language file, you can access any line of text using this function:

$this->lang->line('language_key');

      

Of course your languages ​​will be in "app / language"

+2


source







All Articles