CodeIgniter receiving CI loads correct language file when validating forms

This is more of a call for testimonies than real questions. I would really appreciate the feedback from CI experts and fans.

I have searched the internet several times about this issue: getting the CI Form Validation class loads the correct language file dynamically.

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

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

      

This is indeed the default language. But I have implemented a settings controller that allows my users to set some values ​​and of course change their default language. I could store this setting in a session variable, but at the moment I don't, I just load this user language setting into each controller inside 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 am downloading all the language files I need for each controller with the corresponding language. The get_user_setting function is just a helper for my database query to get a specific install id.

I copied the form_validation_lang.php from the / system / language / english / directory and placed it in the / application / language / french / directory, and then I thought what to do next:

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

      

But no ... it doesn't change anything. I took a look at the form validation class in the main folder and saw the following:

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

      

This clearly makes me think that it will always load the default language file set in the config.php file. Right or Wrong?

Hence, the only way to get this working with my custom settings that I get from the database (and which I could also store in a session variable) is as follows:

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

      

I would really appreciate some feedback if some of you have already dealt with such requirements, and if you really have dealt with the same as I have or not. If I am wrong, I would certainly appreciate solutions.

Thank you so much for your help.

+3


source to share


2 answers


Yes form_validation

load the default language file. If you want to load another language, you can do a trick or change the source code of the form validation class.

The trick used, change the default language before using form validation (you can change it after form validation is complete if you need to.)

You can change the 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 specified.

+1


source


This is how I solved it:

  • Create your own application\libraries\My_Form_validation.php

    .
  • Copy the method run()

    from system\libraries\Form_validation.php

    to your file.
  • Change the following line in the method run()

    in your file:

$this->CI->lang->load('form_validation');



To:

$language = !empty($this->CI->session->language) ? $this->CI->session->language : 'english';
$this->CI->lang->load('form_validation', $language);

      

This assumes you are saving the language across sessions. In your case, you would $language = get_user_setting('language');

, I guess.

0


source







All Articles