CodeIgniter Forums

Full Version: Remove Default Language Name in URL Multi Language Codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to remove default language name from url when using multi language in code igniter. I have 3 languages : English , Italy , Arabic and English is my default language . in other word when language is set to default that “en” must remove from link. I just used this articles
and
but got so many errors
many times by clicking on the link it changes from this “domain.com/ar” to this one “domain.com/en/ar” or from this link “domain.com/en/main/contact/” after clicked it turns to “domain.com/en/main/contact/main/contact”. How can I do that? Any help will be useful Thanks in Advance.
switch link


PHP Code:
echo anchor('en''english'); 
my route:
PHP Code:
$route['^it/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
$route['^ar/(.+)$'] = "$1";
// '/en' and '/ar' -> use default controller
$route['^(en|it|ar|fa)$'] = $route['default_controller'];
$route['^(en|ar|it)/main/contact'] = "main/contact"


MY_Config.php


PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Language in URL for CodeIgniter.
 *
 * @author        Walid Aqleh <waleedakleh23@hotmail.com>
 * @version        1.1.2
 * @based on            Internationalization (i18n) library for CodeIgniter 2 by Jerome Jaglale (http://jeromejaglale.com/doc/php/codeigniter_i18n)
 * @link https://github.com/waqleh/CodeIgniter-Language-In-URL-Internationalization-
 */
class MY_Config extends CI_Config {
    function site_url($uri ''$protocol NULL) {
        if (is_array($uri)) {
            $uri implode('/'$uri);
        }
        if (class_exists('CI_Controller')) {
            $CI = & get_instance();
            $uri $CI->lang->localized($uri);
        }
        return parent::site_url($uri);
    }


MY_Lang.php

PHP Code:
class MY_Lang extends CI_Lang {
    /**************************************************
     configuration
    ***************************************************/
    // languages
    var $languages = array(
        'en' => 'english',
        'fr' => 'french'
        'ar' => 'arabic'

    );
    // special URIs (not localized)
    var $special = array (
        ""
    );
// where to redirect if no language in URI
    var $default_uri ''
    /**************************************************/
    function __construct()
    {
        parent::__construct();      
        
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
        {
$CFG->set_item('language'$this->languages[$this->default_lang()]);
header("Location: " $CFG->site_url($this->localized($this->default_uri)), TRUE302);
            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[0] == $this->lang())
            {
                $exploded[0] = $lang;
            }
            $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 $uri;
    }

PHP Code:
        $lang$this->input->cookie('test_lang');
        if(isset($lang)){
            $this->config->set_item('language'$lang);
            $this->lang->load('site'$lang);
        }else{
            $this->PageModel->_set_cookie('test_lang''english');
            $this->config->set_item('language''english');  
            
//default codeigniter language for forms message etc, system/language etc
            $this->lang->load('site''english');
        
i use cookies to save the language and the code above on __contrust function