• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to load language files

#1
Sad 
Hello,
I am using CI for a few years now (love it, BTW), and every now and then I get stuck with a problem I cannot seem to solve.
This time I am trying to load language files but to no avail.

My controller looks like this:
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');

class Switch_lang extends CI_Controller {
    function __construct(){
        parent:: __construct();
    }

    public function index()
    {
        if($this->session->userdata('lang')== 'heb'){  // switch to English
            $this->session->set_userdata('lang', 'eng');
            $this->session->set_userdata('switch_to_lang', 'עבר');
            $this->lang->load('english','english');
            $this->session->set_userdata('lang_load', 'english');
        }else{
            $this->session->set_userdata('lang', 'heb'); // switch to Hebrew
            $this->session->set_userdata('switch_to_lang', 'Eng');
            $this->lang->load('hebrew','hebrew');
            $this->session->set_userdata('lang_load', 'hebrew');
        }
        
        redirect(base_url());
    }
}

Nothing happens.
The only way to see a language is when I autoload through config/autoload.php file, and even then only english works!
My language folder looks like this:

application
   >language
       >english
            >english_lang.php
       >hebrew
            >hebrew_lang.php

inside the files the phrases go like this:
<?php
/* Galley page */
$lang['gallery_header'] = 'Gallery';
$lang['food_gallery_header'] = 'Food Gallery';

Langage helper is auto-loaded also.

What gives? Angry
Thanks, Eyal
Reply

#2
What happens is $this->lang->load('hebrew','hebrew'); only loads language for a current request only. So after you do a "redirect", it loses loaded language and loads default language again.

What you have to do is also load language in your other controllers like this:
PHP Code:
if($lang $this->session->userdata('lang_load'))
{
 
   $this->lang->load($lang,$lang);

Reply

#3
Once you retrieve the language from your session, you can set the default language to be used when you call $this->lang->load() by calling $this->config->set_item('language', $lang); (where $lang is the language to load). Then every call to $this->lang->load('whatever_lang_file'); afterwards in the same request will work as if you passed $lang as the second argument.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.