Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by wiredesignz )

URI Language Identifier

Category:Core | Category:Core -> Community | Category:Core -> Language | Category:Core -> URL

This language class extension allows you to automatically prefix all site urls with a language abbreviation that is pre-defined in your config file or from a link and automatically load the corresponding language translation file, the route will then be corrected by the route regex for everything to work as normal.
Somewhere in the site you can provide the user with links allowing them to change their desired language name. http://domain.tld/en/controller/method, http://domain.tld/es/controller/method, http://domain.tld/de/controller/method
application/config/routes.php

//route example: http://domain.tld/en/controller => http://domain.tld/controller
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];

application/config/config.php

$config['language'] = "english";

//default language abbreviation
$config['language_abbr'] = "en";

//set available language abbreviations
$config['lang_uri_abbr'] = array("es" => "spanish", "en" => "english");

//ignore this language abbreviation
$config['lang_ignore'] = "en";

application/libraries/MY_Language.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* URI Language Identifier
*
* Adds a language identifier prefix to all site_url links
*
* version 0.14 (c) Wiredesignz 2008-07-30
*/
class MY_Language extends CI_Language
{
    
function MY_Language()
    
{
        parent
::CI_Language();
        
        global
$RTR;
        
        
$index_page    = $RTR->config->item('index_page');
        
$lang_uri_abbr = $RTR->config->item('lang_uri_abbr');
        
        
//get the language from uri segment
        
$lang_abbr = $RTR->uri->segment(1);

        
//check for invalid abbreviation
        
if( ! isset($lang_uri_abbr[$lang_abbr]))
        
{            
            $base_url  
= $RTR->config->item('base_url');
            
$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 .= ($index_page == '') ? "{$deft_abbr}" : "/{$deft_abbr}";
            
                
$uri_string = $RTR->uri->uri_string;
            
                
//remove an invalid abbreviation from uri
                
if (strlen($lang_abbr) == 2)
                
{
                    $uri_string
= str_replace("/{$lang_abbr}", '', $uri_string);
                
}
        
                
//prefix forward-slash to content in uri_string
                
$uri_string = ($uri_string == '') ? '' : '/'.$uri_string;
                
                
//redirect after inserting language id
                
header("Location:".$base_url.$index_page.$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 .= ($index_page == '') ? "{$lang_abbr}" : "/{$lang_abbr}";
            
}
        }
        
        
//reset the the config index_page value
        
$index_page .= ($index_page == '') ? '' : '/';
        
        
$RTR->config->set_item('index_page', $index_page);
    
}
}

Note: Updated URI Language Identifier does not load language files
Use the $config[‘language’] value to determine the language files to load for your application or module.

Extending url helper to display links to content in another language


If you want to display links to another languages for your content, you can extend url helper.
Add to the config file a few lines like this:

//language descriptions
$config['lang_desc']=array("es"=>"Versión en español",
                           
"en" => "English version",
                           
"gl" => "Versión en galego");

Place this file in your application
MY_url_helper.php

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Alternative languages helper
*
* Returns a string with links to the content in alternative languages
*
* version 0.2
* @author Luis <luis@piezas.org.es>
*/

function alt_site_url($uri = '')
{
    $CI
=& get_instance();
    
$actual_lang=$CI->uri->segment(1);
    
$languages=$CI->config->item('lang_desc');
    
$ignore_lang=$CI->config->item('lang_ignore');
    if (empty(
$actual_lang))
    
{
        $uri
=$ignore_lang.$CI->uri->uri_string();
        
$actual_lang=$ignore_lang;
    
}
    
else
    
{
        
if (!array_key_exists($actual_lang,$languages))
        
{
            $uri
=$ignore_lang.$CI->uri->uri_string();
            
$actual_lang=$ignore_lang;
        
}
        
else
        
{
            $uri
=$CI->uri->uri_string();
            
$uri=substr_replace($uri,'',0,1);
        
}
    }
    $alt_url
='';
    foreach (
$languages as $lang=>$lang_desc)
    
{

         
if ($actual_lang!=$lang)
         
{
            $alt_url
.='<a >config->slash_item('base_url');
            if ($lang==$ignore_lang)
            {
                $new_uri=ereg_replace('
^'.$actual_lang,'',$uri);
                $new_uri=substr_replace($new_uri,'',0,1);
            }
            else
            {
                $new_uri=ereg_replace('
^'.$actual_lang,$lang,$uri);
            }
            $alt_url.=$new_uri;
            $alt_url.='">'.$lang_desc.'</a><br />';
         }
    }
    return $alt_url;
}

?>


and you are ready to use it in your view files:

<div id="alternative_languages">
<?php echo alt_site_url();?>
</div>


Now suppose that your default language is english and you are visiting, by example, the spanish version of your web http://example.com/es/mycontroller/myfunction/.  This is what you will obtain:

<div id="alternative_languages">
<
a href="http://example.com/mycontroller/myfunction/">English version</a><br />
<
a href="http://example.com/gl/mycontroller/myfunction/">Versión en galego</a><br />
</
div>


if you want to display images or text you can add to the config file

//language use images
$config['lang_useimg'] = true; //or false to use only text

change MY_url_helper.php to

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Alternative languages helper
*
* Returns a string with links to the content in alternative languages
*
* version 0.2
* @author Luis <luis@piezas.org.es>
* @modified by Ionut <contact@quasiperfect.eu>
*/

function alt_site_url($uri = '')
{
    $CI
=& get_instance();
    
$actual_lang=$CI->uri->segment(1);
    
$languages=$CI->config->item('lang_desc');
    
$languages_useimg=$CI->config->item('lang_useimg');
    
$ignore_lang=$CI->config->item('lang_ignore');
    if (empty(
$actual_lang))
    
{
        $uri
=$ignore_lang.$CI->uri->uri_string();
        
$actual_lang=$ignore_lang;
    
}
    
else
    
{
        
if (!array_key_exists($actual_lang,$languages))
        
{
            $uri
=$ignore_lang.$CI->uri->uri_string();
            
$actual_lang=$ignore_lang;
        
}
        
else
        
{
            $uri
=$CI->uri->uri_string();
            
$uri=substr_replace($uri,'',0,1);
        
}
    }
    $alt_url
='<ul>';
    
//i use ul because for me formating a list from css is easy
    
foreach ($languages as $lang=>$lang_desc)
    
{
         
if ($actual_lang!=$lang)
         
{
            $alt_url
.='<li><a >config->slash_item('base_url');
            if ($lang==$ignore_lang)
            {
                $new_uri=ereg_replace('
^'.$actual_lang,'',$uri);
                $new_uri=substr_replace($new_uri,'',0,1);
            }
            else
            {
                $new_uri=ereg_replace('
^'.$actual_lang,$lang,$uri);
            }
            $alt_url.=$new_uri.'">';
            if ($languages_useimg){
                //change the path on u'r needs
                //in images u need to have for example en.gif and so on for every   
                //language u use
                //the language description will be used as alternative
                $alt_url.= '<img src="'.base_url().'
images/'.$lang.'.gif" alt="'.$lang_desc.'"></a></li>';
            }
            else
            {
                $alt_url.= $lang_desc.'</a></li>';
            }
         }
    }
    $alt_url.='<ul>';
    return $alt_url;
}
?>

Categories: