Welcome Guest, Not a member yet? Register   Sign In
Language problem
#1

[eluser]AzizLight[/eluser]
Hi everybody,

I am trying to develop a multi-languages website. Here is my extended front controller that is supposed to get and set the language and store it in the session:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller
{
    public $main_view;
    public $lang;
    
    public function __construct()
    {
        parent::Controller();
        
        $this->output->enable_profiler(TRUE); // DEBUG <-
        
        $this->main_view = $this->config->item('main_view');
        $this->lang = $this->get_lang();
    }
    
    public function get_lang()
    {
        if (!$this->session->userdata('langue'))
        {
            $default_langue = $this->config->item('default_langue');
            return $this->set_lang($default_langue);
        }
        else
        {
            return $this->session->userdata('langue');
        }
    }
    
    public function set_lang($lang)
    {
        $langues = $this->config->item('langues');
        
        if (!in_array($lang, $langues))
        {
            $default_langue = $this->config->item('default_langue');
            $this->session->set_userdata('langue', $default_langue);
            return $default_langue;
        }
        else
        {
            $this->session->set_userdata('langue', $lang);
            return $lang;
        }
    }
    
}

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */

with this code, I get an error on every page:

Code:
Fatal error: Call to a member function load() on a non-object in /Users/aziz/Sites/Cellules/TATOU/tatou/system/libraries/Loader.php  on line 523

Here is line 523:

Code:
foreach ($file as $langfile)
{    
    $CI->lang->load($langfile, $lang); // line 523
}

If I remove the last line of MY_Controller, I don't get the error anymore:
Code:
$this->lang = $this->get_lang(); // If I remove this line -> no errors.

Also, here are the langues and default_langue config items that are in a custom config file MY_config.php (that I autoload):
Code:
$config['default_langue'] = 'fr';
$config['langues'] = array(
    'en' => 'english',
    'fr' => 'francais',
);

I also autoload the session library. Does anybody know what might be the problem please?

PS: Usually I comment my code very well, but I removed all the comments from the code to make it more readable for this forum post.
#2

[eluser]codeshadow[/eluser]
As I see this, there is a conflict between two separate variables $lang with the same name, but meant for different purposes. The one in your controller is conflicting with the one defined in the Form_validation library.

Change the name of $lang in your controller to something else...$langu or something for the time being %-P ...

Smile
#3

[eluser]AzizLight[/eluser]
Oh! Wow I'm an idiot!

Thanks so much, I'm pretty sure it's gonna work now! Well if it doesn't work I'll try to figure it out and eventually come back here. Anyway, thanks a lot!
#4

[eluser]axmed[/eluser]
i too am stuck trying to develop a site with two languages my problem is the caching, it will store the page in the first language user used, so am trying to find some solution, am thinking of using a separate controller for each language, that will solve the caching problem i guess, still working on it.
but here is what i did with session storing language i used the template library
http://www.williamsconcepts.com/ci/codei...rence.html

the app is set with one master template that has regions that the model"Gui_model" will load with the help ironically from a helper class"cheker" it checks to see what language is selected in the session and return a value to the model to load the right lang, then the controller will render it to the view using the template.

for the controler:
Code:
&lt;?php

class Home extends Controller {

    function Home()    {
        parent::Controller();
        $this->load->model('gui_model');
    }
    
    function index()    {
        //test the session lang in it (1 for arabic anything else english)
        $lan = $this->session->userdata('lan');
                //load a helper
        $this->load->helper('cheker');
        
            //load the page from model
        $this->gui_model->getGui_home(check_lang($lan));
        $this->template->render();
    }
        
}

now the cheker helper file
Code:
&lt;?php
    /*check and see if session is set and return a vaule of the lang*/
    function check_lang($ses)    {
        $obj =& get_instance();
        if($ses == FALSE){
            $obj->session->set_userdata('lan', '2');
        }
        if(!empty($_POST['lan']))    {
            if( $_POST['lan'] == '1'){
                 $obj->session->set_userdata('lan', '1');
                 $ses = '1';
            }else{
                $obj->session->set_userdata('lan', '2');
                $ses = '2';
            }
        }
        
        return $ses;
    }
?&gt;

now the model class that sets the template regions and the language based on the session
Code:
&lt;?php
class Gui_model extends Model {
    
    function Gui_model() {
        parent::Model();
    }
    
    function getGui_home($lan = 0){
        if ($lan == '1')    {
            $this->lang->load('gui', 'arabic');
            $this->template->add_css('css/arab_font.css');
        }else{
            $this->lang->load('gui', 'english');
            $this->template->add_css('css/eng_font.css');
        }    
        $this->template->add_css('css/style.css');
        $data = $this->lang->language;  
        
        $title = "Home";
        $this->template->write('title', $title, TRUE);
        $this->template->parse_view('menu', 'template/menu_home', $data,true);
        $this->template->parse_view('header', 'template/header', $data,true);
        $this->template->parse_view('footer', 'template/footer', $data,true);
    }
    
    
}
?&gt;


am still noobish but hope this helps




Theme © iAndrew 2016 - Forum software by © MyBB