CodeIgniter Forums

Full Version: Set config before language is autoloaded
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]freshface[/eluser]
Hey

How do I set the config file before the language files are autoloaded?
I fetch the default language from my database, set the config file and then the load the files from the config file?

Regards

El Forum

[eluser]pistolPete[/eluser]
Look at this thread:
http://ellislab.com/forums/viewthread/103684/

El Forum

[eluser]TheFuzzy0ne[/eluser]
You could add some logic to your config file that will set it to the right value as it's loaded.

How do you know which language should be loaded? Subdomain? URI segment? Cookie?

El Forum

[eluser]freshface[/eluser]
If there is a cookie set it is the cookie lang else it is my default lang from the db.

El Forum

[eluser]freshface[/eluser]
When I want to load the session library, it gives an error. My Hook:

Code:
<?php

class Language
{
    
    var $ci;
    
    function __construct()
    {
        $this->ci =& get_instance();
        $this->set();
    }
    
    function set()
    {
        $this->ci->load->library('session');
        
        $languages = $this->ci->db->get_where('languages',array('active' => '1'))->result(); // haal alle actieve talen op
        
        if(count($languages) == 0) { die('No languages are defined, check your database'); } // zijn er wel talen actief?
        
        foreach($languages as $language) { $alanguages[$language->abbrevation] = $language->language; }
        $this->ci->config->set_item('languages',$alanguages); // steek de array in de config file.
        
        if($this->ci->session->userdata('lang_id') == FALSE) // als er geen cookie is, dwz dat de site nog niet is bezocht dus defaults uit db halen
        {
            $this->set_default_lang();
        }
        else
        {
            $this->lang_id = $this->ci->session->userdata('lang_id');
            $d = $this->ci->db->get_where('languages',array('active' => '1', 'id' => $this->lang_id))->row();
            
            if(count($d) > 0) // als de taal moet huidige cookie niet meer actief staat
            {
                $this->lang                 = $d->language;
                $this->lang_abbreviation     = $d->abbrevation;
                $this->ci->config->set_item('language',$this->lang);
                $this->ci->config->set_item('language_abbrevation',$this->lang_abbreviation);

            }
            else // mocht om een of andere reden de cookie lang niet meer actief staan
            {
                $this->set_default_lang();
            }
        }
        
        log_message('debug', 'Language: ' . $this->lang . ' is now active');
        
        // define constants
        define('LANG_ID', $this->lang_id);
        define('LANG', $this->lang);
        define('LANG_ABBREVIATION', $this->lang_abbreviation);
    }
    
    function set_default_lang()
    {
        $d = $this->ci->db->get_where('languages',array('active' => '1', 'default'  => '1'))->row();
        
        if(count($d) > 0)
        {
            $this->lang_id                 = $d->id;
            $this->lang                 = $d->language;
            $this->lang_abbreviation     = $d->abbrevation;

            $this->ci->config->set_item('language',$this->lang);
            $this->ci->config->set_item('language_abbrevation',$this->lang_abbreviation);

            // set the cookie
            $this->ci->session->set_userdata('lang_id', $this->lang_id );
            
        }
        else
        {
            log_message('error', 'There is no default language.');
        }
    }
}

Error:

Fatal error: Call to a member function library() on a non-object in /Applications/MAMP/htdocs/webshop/app_backend/hooks/Language.php on line 16

El Forum

[eluser]TheFuzzy0ne[/eluser]
Darn it. Language files are loaded before the session libraries, so that's my idea out of the window...

El Forum

[eluser]freshface[/eluser]
@TheFuzzy0ne your idea was with hooks?

El Forum

[eluser]TheFuzzy0ne[/eluser]
No, it was to add some logic to the config file itself.

For example, if the language was the first segment of the URI, you could do something like this in your config file:
Code:
$CI =& get_instance();
$config['language'] = $CI->uri->segment(1);

El Forum

[eluser]freshface[/eluser]
ok, it seems harder to do then it seems.