Post by aroha.me.forum » Wed Mar 14, 2018 7:36 am

Hi there,

I have come across a weird problem when a site is setup as multilingual with SEO URL's enabled. The URl's don't change to the correct language. If you go to an opencart website that is multilingual and go to the 'About' page, say the language is in English (the page is called http://website.com/about-us) and then you select a diffrent language, e.g. french, the page will display the correct page but the URL is not updated, it still reads as http://website.com/about-us when it should read as http://website.com/à-propos-de-nous . Now if you select the breadcrumb link it will update the URL. The same happens with 'Categories' and 'Products'. If you reselect the same language this will also update/change to the correct SEO URL.

Current install/setup
Clean install (test site) - Opencart 3.0.2.0 - http://dekarski.org/websites/opencart3/

Using the default theme on Opencart 3.0.2.0

Use SEO URLs is set to Yes and .htaccess is setup (please let me know if I should post it). This is whats been changed in the .htaccess file:
# Use PHP7 Single php.ini as default
AddHandler application/x-httpd-php70s .php
RewriteBase /websites/opencart3/

Languages installed:
Chinese https://www.opencart.com/index.php?rout ... n_id=31879
Deutsch https://www.opencart.com/index.php?rout ... n_id=31059
English (Default)
Français https://www.opencart.com/index.php?rout ... n_id=31057
Korean https://www.opencart.com/index.php?rout ... n_id=31076

I have updated the pages under Information. If one of these pages is selected and then the language is changed the SEO URL won't.
About Us
Delivery Information
Privacy Policy
Terms & Conditions

How I test for the problem:
Go to the home page of the website, goto the "About Us" page listed under Information in the footer, then select a difrent language, the SEO URL should change, if you select the "About US" in the breadcrumb it does change.

I have found other sites with the same problem:
http://www.demo-chico.si/lajbi.si/
https://tavexdavanas.lv/
http://demo.sainent.com/

Would love any help or pointers on the above problem.

Thanks heaps


Posts

Joined
Wed Mar 14, 2018 7:25 am

Post by sorcer » Tue Jul 10, 2018 5:06 pm

Hello, you can add this in /catalog/controller/startup/startup.php
after:

Code: Select all

$languages = $this->model_localisation_language->getLanguages();
add this:

Code: Select all

		$url_path = explode('/', rawurldecode($_SERVER['REQUEST_URI']));
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE keyword = '" . $this->db->escape(end($url_path)) . "' ");
		
		if($query->row){
			$this->session->data['language'] = $this->model_localisation_language->getLanguage($query->row['language_id'])['code'];
		}
but you need remove <input type="hidden" name="redirect" value="{{ redirect }}" /> from common\language.twig template

User avatar
Newbie

Posts

Joined
Thu Sep 01, 2011 4:02 am

Post by straightlight » Tue Jul 10, 2018 6:18 pm

replace:

Code: Select all

. "' ");
with:

Code: Select all

. "'");

afterwards.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by sorcer » Tue Jul 10, 2018 8:04 pm

straightlight wrote:
Tue Jul 10, 2018 6:18 pm
replace:
And? where is no difference. This string will be sent to sql.
This code is work fine.


Language switch fix here:
catalog/view/theme/default/template/common/language.twig

Code: Select all

...
    <input type="hidden" name="code" value="" />
	{% for language in languages %}
	  <input type="hidden" name="redirect[{{ language.code}}]" value="{{ language.redirect }}" />
	{% endfor %}
...
catalog/controller/common/language.php

Code: Select all

class ControllerCommonLanguage extends Controller {
	public function index() {
		$this->load->language('common/language');

		$data['action'] = $this->url->link('common/language/language', '', $this->request->server['HTTPS']);
		$data['code'] = $this->session->data['language'];

		$this->load->model('localisation/language');
		$data['languages'] = array();
		$results = $this->model_localisation_language->getLanguages();

		foreach ($results as $result) {
			if ($result['status']) {
				$data['languages'][] = array(
					'name' => $result['name'],
					'code' => $result['code'],
					'redirect' => $this->getRedirect($result['language_id'])
				);
			}
		}



		return $this->load->view('common/language', $data);
	}

	private function getRedirect($language_id){
		if (!isset($this->request->get['route'])) {
			$redirect = $this->url->link('common/home');
		} else {
			$url_data = $this->request->get;
			unset($url_data['_route_']);			
			$route = $url_data['route'];
			unset($url_data['route']);
			$url = '';
			if ($url_data) {
				$url = '&' . urldecode(http_build_query($url_data, '', '&'));
			}
			$curr_lang = $this->config->get('config_language_id');
			$this->config->set('config_language_id',$language_id);
			$redirect = $this->url->link($route, $url, $this->request->server['HTTPS']);
			$this->config->set('config_language_id',$curr_lang);
		}
		return $redirect;
	}
	
	public function language() {
		if (isset($this->request->post['code'])) {
			$this->session->data['language'] = $this->request->post['code'];
		}

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

User avatar
Newbie

Posts

Joined
Thu Sep 01, 2011 4:02 am

Post by before » Mon May 10, 2021 5:26 pm

Hi,

Thanks for the solution. Works like a charm. I would like to make some more changes and I am struggling to get there since months. I also checked and did not find any extension that would do this.

I have multiple domains and I set up multi-store already. I have these domains and languages
domain.hu as default store and hungarian as default language
domain.de --> german
domain.at --> also german
domain.fr --> french
domain.sk --> slovak
Czech only as language
English only as language

I want to achieve a store change when I set a certain URL which makes a language switch like below.
german --> domain.de
french -->domain.fr
slovak or czech --> domain.sk
hungarian --> default store domain.hu

I appreciate any help or contribution.

Also I would like to make the front end language switch to work this way.

Newbie

Posts

Joined
Mon May 10, 2021 4:51 pm
Who is online

Users browsing this forum: Bing [Bot] and 107 guests