Welcome Guest, Not a member yet? Register   Sign In
URI Language Identifier
#1

[eluser]Ficoder[/eluser]
Hi,
I'm using this URI Language Identifier library to my site support multilanguage.

Cause this script doesn't use HTTP_ACCEPT_LANGUAGE, it will always show site with default language what i have defined in config file. I thought that i make little change, that every time when $lang_abbr is changed, it would be stored in cookie, and when user next time visits page, it would load that as default language from cookie.

But im stuck cause i can't use set_cookie helper in library, is it possible to use helpers in library or should i just use native PHP functions?

Btw.
I tried first Internationalization (i18n) library for CodeIgniter, but i couldn't get it work. When i used it, it always gives me redirect pages. If i tried to access site like server/site/controller i always got "Redirecting to". But if i accessed like this server/site/lang/controller, it worked, but all form actions etc. are pointing to urls without lang. If i submit forms i will get redirect pages.
#2

[eluser]bretticus[/eluser]
Checkout Utilizing CodeIgniter Resources within Your Library in the manual.
#3

[eluser]Ficoder[/eluser]
[quote author="bretticus" date="1283072688"]Checkout Utilizing CodeIgniter Resources within Your Library in the manual.[/quote]

Thanks. I think i just need to take couple of steps back and read all through again. I keep getting "Fatal error: Call to undefined function get_instance() in C:\xampp\htdocs\test\application\libraries\MY_Language.php on line 39" error when trying to set $CI =& get_instance(); variable.
#4

[eluser]Ficoder[/eluser]
Correct me if i'm wrong, but i think i can't use helpers in this case? Cause if i'm trying to assign CI object to variable, i always have this same error that there isn't function get_instance().

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

class MY_Language extends CI_Language
{
    function MY_Language() {
        
        global $RTR;
        
        $index_page    = $RTR->config->item('index_page');
        $lang_uri_abbr = $RTR->config->item('lang_uri_abbr');
        
        /* get the lang_abbr from uri segments */
        $lang_abbr = current($RTR->uri->segments);
            
        /* check for invalid abbreviation */
        if( ! isset($lang_uri_abbr[$lang_abbr])) {            
            
            $deft_abbr = $RTR->config->item('language_abbr');
            
            /* check for abbreviation to be ignored */
            if ($deft_abbr != $RTR->config->item('lang_ignore')) {
                
                /* check and set the default uri identifier */
                $index_page .= empty($index_page) ? $deft_abbr : "/$deft_abbr";
            
                /* redirect after inserting language id */
                header('Location: '.$RTR->config->item('base_url').$index_page.$RTR->uri->uri_string);
            }
            
            /* get the language name */
            $user_lang = $lang_uri_abbr[$deft_abbr];
        
        } else {
        
            /* get the language name */
            $user_lang = $lang_uri_abbr[$lang_abbr];
            
            /* reset config language to match the user language */
            $RTR->config->set_item('language', $user_lang);
            $RTR->config->set_item('language_abbr', $lang_abbr);
        
            /* check for abbreviation to be ignored */
            if ($lang_abbr != $RTR->config->item('lang_ignore')) {
            
                /* check and set the user uri identifier */
                   $index_page .= empty($index_page) ? $lang_abbr : "/$lang_abbr";
                
                /* reset uri segments and uri string */
                $RTR->uri->_reindex_segments(array_shift($RTR->uri->segments));
                $RTR->uri->uri_string = str_replace("/$lang_abbr/", '/', $RTR->uri->uri_string);
            }
        }
        
        /* reset the index_page value */
        $RTR->config->set_item('index_page', $index_page);        
        log_message('debug', "MY_Language Class Initialized");
    }
}

/* translate helper */
function t($line) {
    global $CI;
    return ($t = $CI->lang->line($line)) ? $t : $line;
}


So i have to make changes like this:


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

class MY_Language extends CI_Language
{
    function MY_Language() {
      parent::CI_Language();
    }
    
    function DoYourThing() {
        global $RTR;
        
        $CI =& get_instance();
        $CI->load->helper('cookie');
        
        $index_page    = $RTR->config->item('index_page');
        $lang_uri_abbr = $RTR->config->item('lang_uri_abbr');
        
        /* get the lang_abbr from uri segments */
        $lang_abbr = current($RTR->uri->segments);
            
        /* check for invalid abbreviation */
        if( ! isset($lang_uri_abbr[$lang_abbr])) {            
            
            $deft_abbr = $RTR->config->item('language_abbr');
            
            /* check for abbreviation to be ignored */
            if ($deft_abbr != $RTR->config->item('lang_ignore')) {
                
                /* check and set the default uri identifier */
                $index_page .= empty($index_page) ? $deft_abbr : "/$deft_abbr";
            
                /* redirect after inserting language id */
                header('Location: '.$RTR->config->item('base_url').$index_page.$RTR->uri->uri_string);
            }
            
            /* get the language name */
            $user_lang = $lang_uri_abbr[$deft_abbr];
        
        } else {
        
            /* get the language name */
            $user_lang = $lang_uri_abbr[$lang_abbr];
            
            /* reset config language to match the user language */
            $RTR->config->set_item('language', $user_lang);
            $RTR->config->set_item('language_abbr', $lang_abbr);
        
            /* check for abbreviation to be ignored */
            if ($lang_abbr != $RTR->config->item('lang_ignore')) {
            
                /* check and set the user uri identifier */
                   $index_page .= empty($index_page) ? $lang_abbr : "/$lang_abbr";
                
                /* reset uri segments and uri string */
                $RTR->uri->_reindex_segments(array_shift($RTR->uri->segments));
                $RTR->uri->uri_string = str_replace("/$lang_abbr/", '/', $RTR->uri->uri_string);
            }
        }
        
        /* reset the index_page value */
        $RTR->config->set_item('index_page', $index_page);        
        log_message('debug', "MY_Language Class Initialized");
    }
}

/* translate helper */
function t($line) {
    global $CI;
    return ($t = $CI->lang->line($line)) ? $t : $line;
}

But after that, i have to call DoYourThing() function manually and it doesn't serve at is purpose anymore. Cause my goal is that, i doesn't have to call some function in every controller. That's why i want this to be more "Core" thing.




Theme © iAndrew 2016 - Forum software by © MyBB