Welcome Guest, Not a member yet? Register   Sign In
HOWTO language array from DB in I18N library?
#1

[eluser]NiconPhantom[/eluser]
Hi,

I am using CI2.0.2 + HMVC5.4 + I18N library (http://maestric.com/en/doc/php/codeigniter_i18n) and I really love it. There is only one improvement which I'd like to implement in I18N library:

MY_Lang.php contains static array of languages in style:

var $languages = array(

'et' => 'estonian',
'en' => 'english'
);

I'd like to retrieve all this information directly from DB, but unfortunately I can use $ci =& get_instance(); and as result ActiveRecord Class, the only output I see:


Fatal error: Class 'CI_Controller' not found in C:\xampp\htdocs\site\system\core\CodeIgniter.php on line 231

Here is a code as example:

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 MY_Lang extends MX_Lang {

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

$ci =& get_instance(); /* No way to use it here */

    var $languages = array(
      
        'et' => 'estonian',
        'en' => 'english'
    );

    var $special = array ('admin');

    function __construct()
    {

        parent::__construct();

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

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

$ci =& get_instance(); /* No way to use it here */


        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_list()
    {
     $list = $this->languages;  
     if ($list)
        {
            return $list;
        }
    }
    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;
    }
}

Please help if you have any idea how can I use & get_instance(); and then ActiveRecord DB query here :-(
#2

[eluser]NiconPhantom[/eluser]
somebody?
#3

[eluser]Unknown[/eluser]
I'm a newbie and I have the same problem ... could someone help me?
#4

[eluser]InsiteFX[/eluser]
Should be like this:
Code:
class MY_Lang extends MX_Lang {

    private $CI;

    public __construct()
    {
        parent::__construct();

        $this->CI =& get_instance();
    }

}
Now access everthing using $this->CI->

InsiteFX
#5

[eluser]Constantin Iliescu[/eluser]
Any progress on this?
Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB