Welcome Guest, Not a member yet? Register   Sign In
CI 2.0 internationalization of site_url() problem
#1

[eluser]Juris Malinens[/eluser]
Hi! I am using
http://maestric.com/doc/php/codeigniter_i18n

but it not worked by default and I modified it so now /en/test1 and /ru/test1 correctly shows English and Russian language but this code:

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

// CodeIgniter i18n library by Jérôme Jaglale
// http://maestric.com/en/doc/php/codeigniter_i18n
// version 6 - April 20, 2009

class MY_Config extends CI_Config {

    function site_url($uri = '')
    {    
        if (is_array($uri))
        {
            $uri = implode('/', $uri);
        }
        
        if (function_exists('get_instance'))        
        {
            $CI =& get_instance();
            $uri = $CI->lang->localized($uri);            
        }

        return parent::site_url($uri);
    }
        
}
// END MY_Config Class

/* End of file MY_Config.php */
/* Location: ./system/application/libraries/MY_Config.php */

Doesn't override site_url() function and when I use

<p>&lt;?=anchor('music','Shania Twain')?&gt;</p>

I get /music not /en/music or /ru/music

MY_Lang.php:

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

// CodeIgniter i18n library by Jérôme Jaglale
// http://maestric.com/en/doc/php/codeigniter_i18n
// version 6 - April 20, 2009

class MY_Lang extends CI_Lang {

    /**************************************************
     configuration
    ***************************************************/

    // languages
    var $languages = array(
        'en' => 'english',
        'lv' => 'latvian',
        'ru' => 'russian'
    );

    // special URIs (not localized)
    var $special = array (
        ""
    );
    
    // where to redirect if no language in URI
    var $default_uri = '';

    /**************************************************/
    
    
    function MY_Lang()
    {
        parent::CI_Lang();        
        
        global $CFG;
        global $URI;
        global $RTR;
        
        $segment = $URI->segment(1);
        
        if (isset($this->languages[$segment]))    // URI with language -> ok
        {
            $language = $this->languages[$segment];
            $CFG->set_item('language', $language);

        }
        else if($this->is_special($segment)) // special URI -> no redirect
        {
            return;
        }
        else    // URI without language -> redirect to default_uri
        {
            // set default language
            $CFG->set_item('language', $this->languages[$this->default_lang()]);

            // redirect
            header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
            exit;
        }
    }
    
    
    // get current language
    // ex: return 'en' if language in CI config is 'english'
    function lang()
    {
        global $CFG;        
        $language = $CFG->item('language');
        
        $lang = array_search($language, $this->languages);
        if ($lang)
        {
            return $lang;
        }
        
        return NULL;    // this should not happen
    }
    
    
    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 switch_uri($lang)
    {
        $CI =& get_instance();
        $uri = $CI->uri->uri_string();

        if ($uri != "")
        {
            $exploded = explode('/', $uri);

            // If we have an URI with a lang --&gt; es/controller/method
            if($exploded[0] == $this->lang())
            $exploded[0] = $lang;

            // If we have an URI without lang --&gt; /controller/method
            // "Default language"
            else if (strlen($exploded[0]) != 2)
            $exploded[0] = $lang . "/" . $exploded[0];

            $uri = implode('/',$exploded);
        }

        return $uri;
    }
    // is there a language segment in this $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;
    }
    
    
    // default language: first element of $this->languages
    function default_lang()
    {
        foreach ($this->languages as $lang => $language)
        {
            return $lang;
        }
    }
    
    // add language segment to $uri (if appropriate)
    function localized($uri)
    {
        if($this->has_language($uri)
                || $this->is_special($uri)
                || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
        {
            // we don't need a language segment because:
            // - there's already one
            // - or it's a special uri (set in $special)
            // - or that's a link to a file
        }
        else
        {
            $uri = $this->lang() . '/' . $uri;
        }
        
        return $this->lang() . '/' . $uri;
    }
    
}
#2

[eluser]InsiteFX[/eluser]
site_url is in the url_helper.php

Did you load the url helper?

If you need to change the site_url you need to create a MY_url_helper.php.

InsiteFX
#3

[eluser]Juris Malinens[/eluser]
[quote author="InsiteFX" date="1289934865"]site_url is in the url_helper.php

Did you load the url helper?

If you need to change the site_url you need to create a MY_url_helper.php.

InsiteFX[/quote]


Thank You!

Created MY_url_helper.php and added this code:
Code:
function site_url($uri = '')
    {    
        if (is_array($uri))
        {
            $uri = implode('/', $uri);
        }
        
        if (function_exists('get_instance'))        
        {
            $CI =& get_instance();
            $uri = $CI->my_lang->localized($uri);            
        }
    }

And now it does what I need =)
#4

[eluser]JulianM[/eluser]
Thanks. It worked for me after I added a missing line at the end.

Code:
return $uri;
#5

[eluser]Unknown[/eluser]
[quote author="InsiteFX" date="1289934865"]site_url is in the url_helper.php

Did you load the url helper?

If you need to change the site_url you need to create a MY_url_helper.php.

InsiteFX[/quote]

I'm sorry to say but this is bad advise. Both the helper and the config class host site_url.
What hat you want to do is to alter the method in de the config class instead of creating an extra function on top of it. In this case the result will be that both method/function will be executed end u end up with sandwiched url's when you use site_url like "en/site/page/en/site/page" It took me several hours of debugging to find out what was going on.

The main problem is the i18n class. It contains a few bugs and loops that will give you the CI_Controller 210 error.

Hope this helps.
I will attach the Codeigniter 2.0 + HMVC ready files. Hope someone will find them useful and safe a few hours.




Theme © iAndrew 2016 - Forum software by © MyBB