Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter getting CI load the correct language file when validating forms

This is rather a call for testimonies than a real questions. I would greatly appreciate feedbacks from CI experts and fans.

I have been searching the Internet for hours about this issue of mine: getting CI Form Validation class load the proper language file dynamically.

Let me explain. In my config.php file, I have:

$config['language'] = 'english';

Which is indeed the default language. But I have implemented a Settings controller which is letting my users set some values and of course change their default language. I could have stored that setting into a session variable but for the moment I don't, I just load that language user setting into each controller within the constructor :

$this->idiom = get_user_setting('language');
$this->lang->load('main', $this->idiom);
$this->lang->load('settings', $this->idiom);
$this->lang->load('cst', $this->idiom);

and as you can see I then load all the language files I need for each controller with the appropriate language. The 'get_user_setting' function is just a helper of mine querying the database to get a particular setting id.

I have copied the form_validation_lang.php from the /system/language/english/ directory and put it into my /application/language/french/ directory, and then I thought doing the following would do the magic:

$this->lang->load('form_validation', $this->idiom);

But nope... does not change anything. I took a look at the Form Validation class in the core folder and saw the following:

// Load the language file containing error messages
$this->CI->lang->load('form_validation');

THis clearly makes me think it will always load the file of the default language set in the config.php file. Am I right or wrong?

Hence, the only way I got to have this work with my user defined settings I fetch from the database (and which I could also store in a session variable), is to do the following:

$this->idiom = get_user_setting('language');
$this->config->set_item('language', $this->idiom);
...

I would greatly appreciate some feedbacks if some of you already had to cope with this kind of requirements and if you indeed managed that the same way I did or not. If I'm totally wrong, I'd appreciate solutions of course.

THanks a lot for your help.

like image 912
Nicolas C. Avatar asked Feb 28 '26 21:02

Nicolas C.


1 Answers

Yes form_validation load default language file.If you want to load different language you can do a trick or change the source code of the Form validation class.

The trick I use, change the default language before using form validation(you can change back after form validation complete if you need.)

You can change default language with this code.

$this->config->set_item('language', 'YOUR_LANGUAGE');

After this code you can use form validation which will load the language you set.

like image 191
Shaiful Islam Avatar answered Mar 03 '26 12:03

Shaiful Islam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!