EllisLab text mark
Advanced Search
     
Hook that changes the current language dynamicly
Posted: 10 November 2007 01:28 PM   [ Ignore ]
Joined: 2007-11-10
3 posts

When you will enable this hook, you will get two new global variables $GLOBALS[‘language’] with long language name as “english” and $GLOBALS[‘language-short’] as short language name as “en”.

1. enable hooks in config.php
2. change uri_protocol in config.php to “PATH_INFO”
3. load url helper in autoload.php
4. create a file named language.php in hooks directory with this content:

<?php

class Language {
    
var $languages = array (
        
'en' => 'english',
        
'lv' => 'latvian',
        
'ru' => 'russian'
    
);
        
    function 
get_language() {
    
// include the config to get the default language
    
include("system/application/config/config.php");
    
    
// removes the index.php from the url
    
$url str_replace('/index.php','',$_SERVER["REQUEST_URI"]);
    
    
// trying to get the language and set as global variable
    
$pieces explode('/',$url);
    if(
sizeof($pieces) > 1){
        
if(key_exists($pieces[1],$this->languages)){
        $this
->set_language($this->languages[$pieces[1]]);
        
}else{
        $this
->set_language($config['language']);
        
}
    }else{
        $this
->set_language($config['language']);
    
}
    }
    
    
function set_language($lang){
    $GLOBALS[
'language'$lang;
    foreach(
array_keys($this->languages) as $l)
        if(
$lang == $this->languages[$l])
        
$GLOBALS['language-short'$l;
    
    
}
}
?> 

and modify the $languages array to suit your needs.

5. Add these lines in hooks.php config file:

$hook['pre_system'= array(
    
'class'    => 'Language',
    
'function' => 'get_language',
    
'filename' => 'language.php',
    
'filepath' => 'hooks'
); 

Now when you call yoursite.com/en/ or yoursite.com/index.php/en/ it defines the global variable for you.


Please correct me if I did something wrong, because I’m new to CI.

 
Posted: 21 November 2007 03:57 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-21
273 posts

Hi, I didn’t test anything, so maybe everything you coded was necessary. Just thinking loud…

I don’t understand why you want it to act BEFORE the config loads, and I don’t understand the use of $GLOBALS[‘language’].

If we execute if later, say at ‘pre_controller’ time, could we simplify it this way:

<?php
class Language {
    
var $languages = array (
        
'en' => 'english',
        
'lv' => 'latvian',
        
'ru' => 'russian'
    
);
        
    function 
get_language() {
    
    
// removes the index.php from the url
    
$url str_replace('/index.php','',$_SERVER["REQUEST_URI"]);
    
    
// trying to get the language and set the config accordingly
    
$pieces explode('/',$url);
    if(
sizeof($pieces) > AND key_exists($pieces[1]$this->languages)
            
$this->config->set_item('language'$this->languages[$pieces[1]]);
    
}

Your set_language function seems tricky to me. You have the key/short-string in the url, why sending the value/long-string to a function that has to find back the key/short-string in the table.

One thing is sure, you’d rather work with the short-string here!

Cheers!

 
Posted: 21 November 2007 04:45 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-11-10
3 posts

Your example is better (this was my first day with codeigniter). The short string is sometimes useful, like “some_image_en.jpg”

 
Posted: 21 November 2007 04:56 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-21
273 posts

ok, I understand now the need of the short one.
I think that the use of $this->config->set_item(‘item_name’, ‘item_value’) and $this->config->item(‘item name’); is to be prefered to the $GLOBALS array.

 
Posted: 21 November 2007 05:05 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-11-10
3 posts

You are right, thanks for recommendation.