Faryal Khan
Faryal Khan

Reputation: 869

Changing the language of a page on clicking a link

I have three different languages link on my webpage

What I want is that when a user click on any of the language link the text on that page changes to the corresponding language.

Is it possible that I call one of the method from my controller let say 'changeLanguage' it changes the language and then it reload the same page with text in different language

Any idea how can I do it efficiently

My links are in the view and I m using codeigniter

Thanks

Upvotes: 0

Views: 3199

Answers (4)

ashraf mohammed
ashraf mohammed

Reputation: 1350

 //first here are your switching links
 <?php $base_url =  site_url()."/"; ?>
 <li>
      <a href='<?php echo $base_url; ?>langswitch/switchLanguage/arabic?redirect_to=<?php echo urlencode(current_url())?>'>العربية</a>
      </li>
      <li>
      <a href='<?php echo $base_url; ?>langswitch/switchLanguage/english?redirect_to=<?php echo urlencode(current_url())?>'>English</a>
      </li>

  //----------------------then you need to write your controller like this
 class LangSwitch extends CI_Controller {

private $redirect_to = '';

public function __construct() {
    parent::__construct();
    $this->redirect_to = base_url();
    $redirect_to = isset($_GET["redirect_to"])?$_GET["redirect_to"]:"";
    if(!empty($redirect_to)){
        $this->redirect_to = urldecode($redirect_to);
    }
    $this->load->helper('url');
}

function switchLanguage($language = "") {
    $language = ($language != "") ? $language : "english";
    $this->session->set_userdata('site_lang', $language);
    redirect($this->redirect_to);
}

}

Upvotes: 0

Ben Carey
Ben Carey

Reputation: 16958

There are many ways you can do this but it effectively comes down to how your site is configured. The best starting point would be to use PHP to retrieve a $_GET request and show the content based on the request.

You links will refer to http://yoursite.com/index.php?lang=en... Which you can convert to (using htaccess or web.config) http://yoursite.com/en

Something like:

// Set the default language to english if the language request is not set
$language = isset($_GET['lang']) ? $_GET['lang'] : 'en';

// Show the content base on the language
switch($_GET['lang']){
    case 'en':
        $content = "This is English";
    break;  
    case 'pt':
        $content = "Isto é Português";
    break;  
}

Upvotes: 1

W Kristianto
W Kristianto

Reputation: 9303

VIEW

<a href=<?php echo site_url('home?language=indonesian');?>>Indonesian language</a>

CONTROLLER

class Home extends CI_Controller {

    public function index()
    {
        $language = $this->input->get('language');
        if($language){
            // Put your code
            $this->session->set_userdata('language', $language);
            redirect('home');
        }

        if($this->session->userdata('language'))
        {
            var_dump($this->session->userdata('language'));
        }
        echo 'Hello World!';
    }

}

Source

Upvotes: 1

RPD
RPD

Reputation: 494

You can on event click refresh your website with Get [HTTP] www.mywebsite.com?lg=eng

Upvotes: 1

Related Questions