OpenCart Set language through URL

Although the built-in language selection in OpenCart close to perfect, If you need a language switcher extension for any text anywhere on the site, here are simple code to create links that direct users to the store in a specific language.

This code may use http referer as redirect URL, so it’s no redirect URL need.

Open file in catalog/controller/module/language.php

Add this code before line 4:


    	if (isset($this->request->get['lang'])) {

			$this->session->data['language'] = $this->request->get['lang'];

			if (isset($_SERVER['HTTP_REFERER']) && (strpos($_SERVER['HTTP_REFERER'], $this->config->get('config_url')) !== false) ) {

				$this->redirect($_SERVER['HTTP_REFERER']);

			} else {

				$this->redirect($this->url->link('common/home'));

			}

    	} else

Example:

<?php
   class ControllerModuleLanguage extends Controller {
  	protected function index() {
     	 		if (isset($this->request->get['lang'])) {

			$this->session->data['language'] = $this->request->get['lang'];

			if (isset($_SERVER['HTTP_REFERER']) && (strpos($_SERVER['HTTP_REFERER'], $this->config->get('config_url')) !== false) ) {

				$this->redirect($_SERVER['HTTP_REFERER']);

			} else {

				$this->redirect($this->url->link('common/home'));

			}

    	} elseif (isset($this->request->post['language_code'])) {
			$this->session->data['language'] = $this->request->post['language_code'];

			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect($this->url->link('common/home'));
			}
    	}

Now you may test your language in URL. If you have English and Spanish on your site and you would use:
http://yourstore.com/?lang=en (for English) or
http://yourstore.com/?lang=es (for Spanish)

Last Updated On: 5th July 2013

8 thoughts on “OpenCart Set language through URL

  1. Ariel on

    Mattias, you should change

    $this->redirect($_SERVER[‘HTTP_REFERER’]);

    for

    $this->redirect($this->url->link(‘common/home’));

  2. guys redirection is not working in opencart 2. so as soon as i get the new language by post the previous language is preserved until i go to any other page when the language change happens

  3. update:

    Insert code below after the line 4 in the file:

    catalog\controller\common\language.php

    CODE:

    if (isset($this->request->get[‘lang’])) {

    $this->session->data[‘language’] = $this->request->get[‘lang’];

    $this->response->redirect($this->url->link(‘common/home’));
    }

  4. Is there anything else that has to be done? Can’t get it to work, Running standard theme 2.1.0.1. Would like to direct from my Swedish “ordinary” website to the Swedish OC store and from the English ordinary website to the English OC store. Tried every possible way that I can come up with to have the link from my website to the Swedish / English shop respectively to no avail. Get very erratic behaviour.

Leave a Reply to Elmer Cancel reply