Welcome Guest, Not a member yet? Register   Sign In
CI core language
#1

[eluser]sl3dg3hamm3r[/eluser]
Hello there

I hope I will get some answers with a new topic about this issue. I'm still seeking for a possibility to change CI's core language file path (./system/language/[language]/) during run time. I still couldn't figure out where and how this information is stored.

Xwero posted once a solution, which doesn't work (I guess due to newer CI version):
Code:
$CI =& get_instance();
if($lang != $CI->config->item('language'))
{
    $CI->config->set_item('language',$lang);
    $loaded = $CI->config->is_loaded;
    $CI->config->is_loaded = array();
            
    foreach($loaded as $file)
    {
        $CI->config->load(str_replace('_lang.php','',$file));    
    }
}

The array is_loaded is simply empty, nothing to change there. I'm a little bit suprised that CI doesn't offer any function to change the language path during run time?!? I'd say this is quite essential if you want to offer different languages on your web-page.

I searched already through all CI-files after 'language' (as it is defined in the config), but couldn't find any place where this would be called and set...

I'd appreciate any input to the right path...
sl3dg3
#2

[eluser]sl3dg3hamm3r[/eluser]
hmm... nobody around from EllisLab who could answer me that question? Should I put this issue to the feature-requests? Seems quite essential to me.

I'd love to know how others solved this with a language-picker on their CI-website!
#3

[eluser]Phil Sturgeon[/eluser]
I used to do this with a pre-controller hook but im not sure its working anymore. Either way, this is the approach you should be using:

Code:
// Load CI config class
    $CI_config =& load_class('Config');

    $CI_config->set_item('language', 'french');

I obviously don't use a hardcoded value but use various criteria to work out which language is being requested based on sessions, cookies, browser settings, etc.
#4

[eluser]sl3dg3hamm3r[/eluser]
Well, I will try it with a pre-controller, let's see if it will work. Thank you!

Edit: I realise now that the config-item 'language' needs to be overwritten BEFORE any of the libraries are loaded... I think the pre-controller will work for that matter.
#5

[eluser]sl3dg3hamm3r[/eluser]
Alright, this seems to do the job:

config/config:
Code:
$config['enable_hooks'] = true; // Enable hooks

config/hooks:
Code:
$hook['post_controller_constructor'] = array(            //pre_controller --> loading helpers or libraries wouldn't work yet
                                'class'    => 'Language',
                                'function' => 'SetLanguage',
                                'filename' => 'Language.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );

hooks/Language.php
Code:
class Language {

  
    function __construct()
    {
      
    }
  
    public function SetLanguage()
    {
        $CI =& get_instance();
        $CI->load->helper('lang'); // This is my own helper which reads the saved language from a cookie (if any, otherwise default)
        $lang = getLanguage();
        $CI->config->set_item('language',$lang);
    }
  
}
#6

[eluser]Phil Sturgeon[/eluser]
If that is litterally all you are doing then you could do this:

config/hooks:
Code:
$hook['post_controller_constructor'] = array(            //pre_controller --> loading helpers or libraries wouldn't work yet
                                'class'    => '',
                                'function' => 'SetLanguage',
                                'filename' => 'Language.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );

hooks/Language.php
Code:
function SetLanguage()
    {
        $CI =& get_instance();
        $CI->load->helper('lang'); // This is my own helper which reads the saved language from a cookie (if any, otherwise default)
        $lang = getLanguage();
        $CI->config->set_item('language',$lang);
    }

You dont need to set an empty constructor and if you arent actually using the hook as an object then you dont need to declare one. You can just run a function which is slightly quicker on runtime and keeps the source a little cleaner.

Possibly just nitpicking now. Glad to hear it works. :-)
#7

[eluser]sl3dg3hamm3r[/eluser]
Ah, I didn't know it would work without class. Since it is loaded on every call, it is certainly worth to think about such optimizations - thank you.




Theme © iAndrew 2016 - Forum software by © MyBB