0
votes

I'm using latest codeigniter 3.1.3

There is instruction how to use languages https://www.codeigniter.com/userguide3/libraries/language.html#handling-multiple-languages

$idiom = $this->session->get_userdata('language');
$this->lang->load('error_messages', $idiom);  --> gives ERROR (home_view line 14)
$oops = $this->lang->line('message_key'); 
echo $oops;

but if i write that lines of code I get following error:

A PHP Error was encountered

Severity: Warning

Message: preg_match() expects parameter 2 to be string, array given

Filename: core/Lang.php

Line Number: 109

Backtrace:

File: .../application/views/home_view.php Line: 14 Function: load

File: .../controllers/Home.php Line: 14 Function: view

File: .../project/index.php Line: 315 Function: require_once

If I remove

$idiom = $this->session->get_userdata('language');
$this->lang->load('error_messages', $idiom);  --> ERROR (home_view 

and place in autoload.php

$autoload['language'] = array('error_messages');

everything is fine.

But I don't want to autoload all language files since I don't know if user needs everyone of them.

Anyone can help?

thanks

1
did you set session.. - Hikmat Sijapati
check your session data if its array so please convert into string or get specific data from session - Trushar Narodia
well, thanks it was session problem - flashore

1 Answers

0
votes

You can achieve like this.Access session variable using $this->session->userdata('item_name').

$idiom = $this->session->userdata('language');
$this->lang->load('error_messages', $idiom);
$oops = $this->lang->line('message_key'); 
echo $oops;

OR

$this->config->set_item('language', $idiom );//dynamically set language
$this->lang->load('error_messages');//load lang file

$oops = $this->lang->line('message_key'); 
echo $oops;

Hope it helps you.

For more see here https://codeigniter.com/userguide3/libraries/sessions.html