Welcome Guest, Not a member yet? Register   Sign In
Hook that changes the current language dynamicly
#1

[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.


Messages In This Thread
Hook that changes the current language dynamicly - by El Forum - 11-10-2007, 10:28 AM
Hook that changes the current language dynamicly - by El Forum - 11-21-2007, 12:57 PM
Hook that changes the current language dynamicly - by El Forum - 11-21-2007, 01:45 PM
Hook that changes the current language dynamicly - by El Forum - 11-21-2007, 01:56 PM
Hook that changes the current language dynamicly - by El Forum - 11-21-2007, 02:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB