Welcome Guest, Not a member yet? Register   Sign In
i18n library in CI2
#1

[eluser]Razican[/eluser]
I have been using this library in CI2 until last update.

http://maestric.com/doc/php/codeigniter_i18n

The problem is that since that update, I have changed the code to look like this:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MP_Config Class
*
* @subpackage    Libraries
* @author        Jérôme Jaglale
* @category    Libraries
* @link        http://maestric.com/en/doc/php/codeigniter_i18n
*/

class MP_Lang extends CI_Lang {

/*
|--------------------------------------------------------------------------
| Configuration
|--------------------------------------------------------------------------
|
| $languages    -> array with all the languaes inplemented
| $special        -> Not localized URIs
|
*/

    var $languages = array(
        'es' => 'spanish'/*,
        'en' => 'english',
        'eu' => 'basque'*/
    );

    var $special = array ('admin');

    function __construct()
    {

        parent::__construct();

        global $CFG;
        global $URI;
        global $RTR;

        $segment = $URI->segment(1);

        if (isset($this->languages[$segment]))
        {
            $language = $this->languages[$segment];
            $CFG->set_item('language', $language);
        }
        else if($this->is_special($segment))
        {
            return;
        }
        else
        {
            $CFG->set_item('language', $this->languages[$this->default_lang()]);

            header("Location: " . $CFG->site_url($this->lang($CFG->item('language')).$URI->uri_string()), TRUE, 302);
            exit;
        }
    }
    
    function lang()
    {
        global $CFG;        
        $language = $CFG->item('language');
        
        $lang = array_search($language, $this->languages);
        if ($lang)
        {
            return $lang;
        }
        
        return NULL;
    }

    function is_special($uri)
    {
        $exploded = explode('/', $uri);
        if (in_array($exploded[0], $this->special))
        {
            return TRUE;
        }
        if(isset($this->languages[$uri]))
        {
            return TRUE;
        }
        return FALSE;
    }

    function switch_uri($lang)
    {
        $CI =& get_instance();

        $uri = $CI->uri->uri_string();
        if ($uri != "")
        {
            $exploded = explode('/', $uri);
            if($exploded[1] == $this->lang())
            {
                $exploded[1] = $lang;
            }
            $uri = implode('/',$exploded);
        }
        return $uri;
    }

    function has_language($uri)
    {
        $first_segment = NULL;
        
        $exploded = explode('/', $uri);
        if(isset($exploded[0]))
        {
            if($exploded[0] != '')
            {
                $first_segment = $exploded[0];
            }
            else if(isset($exploded[1]) && $exploded[1] != '')
            {
                $first_segment = $exploded[1];
            }
        }
        
        if($first_segment != NULL)
        {
            return isset($this->languages[$first_segment]);
        }
        
        return FALSE;
    }

    function default_lang()
    {
        foreach ($this->languages as $lang => $language)
        {
            return $lang;
        }
    }

    function localized($uri)
    {
        if($this->has_language($uri)
        || $this->is_special($uri)
        || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
        {}
        else
        {
            $uri = $this->lang() . '/' . $uri;
        }

        return $uri;
    }
}

/* End of file MP_Language.php */
/* Location: ./application/core/MP_Language.php */

I get this error:
Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 210

How can I solve it?
#2

[eluser]WanWizard[/eluser]
That has nothing to do with this library. Your controllers are still extending 'Controller' instead of 'CI_Controller'.
You should also change all controller constructors to __construct() instead of using the class name.
#3

[eluser]Razican[/eluser]
This is my controller structure:

Code:
class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {...}
#4

[eluser]developer10[/eluser]
[quote author="Razican" date="1289618137"]I have been using this library in CI2 until last update.

http://maestric.com/doc/php/codeigniter_i18n

The problem is that since that update, I have changed the code to look like this:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MP_Config Class
*
* @subpackage    Libraries
* @author        Jérôme Jaglale
* @category    Libraries
* @link        http://maestric.com/en/doc/php/codeigniter_i18n
*/

class MP_Lang extends CI_Lang {

/*
|--------------------------------------------------------------------------
| Configuration
|--------------------------------------------------------------------------
|
| $languages    -> array with all the languaes inplemented
| $special        -> Not localized URIs
|
*/

    var $languages = array(
        'es' => 'spanish'/*,
        'en' => 'english',
        'eu' => 'basque'*/
    );

    var $special = array ('admin');

    function __construct()
    {

        parent::__construct();

        global $CFG;
        global $URI;
        global $RTR;

        $segment = $URI->segment(1);

        if (isset($this->languages[$segment]))
        {
            $language = $this->languages[$segment];
            $CFG->set_item('language', $language);
        }
        else if($this->is_special($segment))
        {
            return;
        }
        else
        {
            $CFG->set_item('language', $this->languages[$this->default_lang()]);

            header("Location: " . $CFG->site_url($this->lang($CFG->item('language')).$URI->uri_string()), TRUE, 302);
            exit;
        }
    }
    
    function lang()
    {
        global $CFG;        
        $language = $CFG->item('language');
        
        $lang = array_search($language, $this->languages);
        if ($lang)
        {
            return $lang;
        }
        
        return NULL;
    }

    function is_special($uri)
    {
        $exploded = explode('/', $uri);
        if (in_array($exploded[0], $this->special))
        {
            return TRUE;
        }
        if(isset($this->languages[$uri]))
        {
            return TRUE;
        }
        return FALSE;
    }

    function switch_uri($lang)
    {
        $CI =& get_instance();

        $uri = $CI->uri->uri_string();
        if ($uri != "")
        {
            $exploded = explode('/', $uri);
            if($exploded[1] == $this->lang())
            {
                $exploded[1] = $lang;
            }
            $uri = implode('/',$exploded);
        }
        return $uri;
    }

    function has_language($uri)
    {
        $first_segment = NULL;
        
        $exploded = explode('/', $uri);
        if(isset($exploded[0]))
        {
            if($exploded[0] != '')
            {
                $first_segment = $exploded[0];
            }
            else if(isset($exploded[1]) && $exploded[1] != '')
            {
                $first_segment = $exploded[1];
            }
        }
        
        if($first_segment != NULL)
        {
            return isset($this->languages[$first_segment]);
        }
        
        return FALSE;
    }

    function default_lang()
    {
        foreach ($this->languages as $lang => $language)
        {
            return $lang;
        }
    }

    function localized($uri)
    {
        if($this->has_language($uri)
        || $this->is_special($uri)
        || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
        {}
        else
        {
            $uri = $this->lang() . '/' . $uri;
        }

        return $uri;
    }
}

/* End of file MP_Language.php */
/* Location: ./application/core/MP_Language.php */

I get this error:
Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 210

How can I solve it?[/quote]

it is obvious what the error is saying: why dont you try to lookup the file itself where it is supposed to be. If it's in your files, then locate it in your localhost root and make sure it is named correctly.

That's what i would do if i had the same problem
#5

[eluser]InsiteFX[/eluser]
Did you change the CodeIgniter prefix ?
Did you copy your Classes to application/core ?

InsiteFX
#6

[eluser]Razican[/eluser]
[quote author="InsiteFX" date="1289699457"]Did you change the CodeIgniter prefix ?
Did you copy your Classes to application/core ?
[/quote]

I changed the $config['subclass_prefix'] = 'MP_';
I have the MP_Lang.php in /application/core/

It worked fine with the old version of CI2.
#7

[eluser]Razican[/eluser]
UP
#8

[eluser]Razican[/eluser]
Is there any solution?
#9

[eluser]n0xie[/eluser]
The problem is that in the latest CI2.0 build the get_instance() is a wrapper for CI_Controller::get_instance(), a static call to the (new) CI superobject instance. The i18n library does the call before the instance is created. This wasn't a problem before since get_instance() only existed if the CI_Base object was created, but since CI_Base is gone and 'replaced' by CI_Controller, the call is now done before instantiating the object.

tl;dr: new PHP4 drop screwed up loading order of things for i18n library

You can simply fix it by editing your MY_Config, which is part of the i18n library:
Code:
class MY_Config extends CI_Config {

    function site_url($uri = '')
    {    
        if (is_array($uri))
        {
            $uri = implode('/', $uri);
        }

        // make it compatible with CI 2.0
        if (class_exists('CI_Controller'))
        {
            $uri = get_instance()->lang->localized($uri);
        }
        
        return parent::site_url($uri);
    }
        
}
#10

[eluser]Razican[/eluser]
Thanks, now it works perfectly.




Theme © iAndrew 2016 - Forum software by © MyBB