Programmer
Programmer

Reputation: 157

How can i implement language switch through the button click?

The config file loads first and so the default language is picked from there. But I need to change the default language dynamically through button click. How can I do that? In my controller I have

public function __construct()
    {
        parent::__construct();

        $this->config->set_item('language', 'japanese');
        $this->load->helper('language');
        $this->lang->load('fileimport');
        var_dump(lang('message_missing_csvfile_import'));

    }

This will display the value in japanese language

config.php is as follows:

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

autoload.php is as follows:

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

My view is as follows:

<form action ='<?php echo current_url(); ?>' enctype =
 'multipart/form-data' method ='post' name="formtest" id="formtest">

             <div>
                 <select id="main_language" name ="main_language">
                     <option>English</option>
                   <option>Japanese</option>
                </select>
                <input id="main_lang_button" type="button" value="Set Main Language">
            </div></form>

Upvotes: 0

Views: 943

Answers (2)

NikuNj Rathod
NikuNj Rathod

Reputation: 1658

You can try this solution for your problem.

Please change your My_controller for Language changes.

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if(!$this->session->userdata('language')) {
            $this->session->set_userdata('language','japanese');
        }
        $this->lang->load('language_name',$this->session->userdata('language'));
    }
} ?>

You can add set session for language Any your controller for ex. Dashboard Controller.

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
   public function change_language($lang = 'japasese') {
        if($this->input->is_ajax_request()) {
            $this->session->set_userdata('language',(($lang=='japasese') ? 'japasese' : 'english'));
            echo true;
        } else {
            redirect('login');
        }
    }
} ?>

You can set your view file like

<form action ='#' enctype ='multipart/form-data' method ='post' name="formtest" id="formtest">
    <select id="main_language" name ="main_language">
        <option value="english">English</option>
        <option value="japasese">Japanese</option>
    </select>
</form>

Add Scrip file.

<script type="text/javascript">
    base_url = '<?=base_url()?>';
    $(document).on('change', '#main_language', function () {
        lang = $('#main_language').val();
        $.ajax
         ({
             'type': 'GET',
             'url': base_url + 'dashboard/change_language/'+lang,
             'success': function(response){
                 window.location.reload();
             }
         });
    });
</script>

I hope this will helps you.

Upvotes: 1

Narf
Narf

Reputation: 14752

When you get the value from the form, put it in a cookie. Something like that:

/* form validation here */

// ...

// After you know it's a valid value:
setcookie('language', $_POST['main_language'], 0, '/');

Then use that cookie in your application/config/config.php:

$allowed_languages = array('English', 'Japanese');
if (isset($_COOKIE['language']) && in_array($_COOKIE['language'], $allowed_languages, true)) {
    $config['language'] = strtolower($_COOKIE['language']);
} else {
    $config['language'] = 'english';
}

Note that you need to validate the cookie as well. It is still user input, regardless of the fact that you set it.

Of course, this configuration will default to 'english' on the request that you're still processing the form. Just make sure to do a redirect after you set the cookie, and you'll get around that issue.

Upvotes: 2

Related Questions