CodeIgniter Forums
Hook that changes the current language dynamicly - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Hook that changes the current language dynamicly (/showthread.php?tid=4166)



Hook that changes the current language dynamicly - El Forum - 11-10-2007

[eluser]Gacha[/eluser]
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:
Code:
<?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:
Code:
$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.


Hook that changes the current language dynamicly - El Forum - 11-21-2007

[eluser]Grahack[/eluser]
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:
Code:
<?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) > 1 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!


Hook that changes the current language dynamicly - El Forum - 11-21-2007

[eluser]Gacha[/eluser]
Your example is better (this was my first day with codeigniter). The short string is sometimes useful, like "some_image_en.jpg"


Hook that changes the current language dynamicly - El Forum - 11-21-2007

[eluser]Grahack[/eluser]
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.


Hook that changes the current language dynamicly - El Forum - 11-21-2007

[eluser]Gacha[/eluser]
You are right, thanks for recommendation.