How to set language through url in opencart

2020-06-04 01:24发布

问题:

I'd like to change language through url so my site can appear in different languages in search engines.
e.g: I'd like the user to change language through a link like this:
www.mysite.com/lang=ar

I'm using opencart 3.0.2
Currently the user change through a form menu that submit post request, so search engine can't index the other languages.
How can I do that?

回答1:

I successfully applied these steps:
1- open catalog/controller/startup/startup.php
after the line:

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

add the following code:

if(isset($this->request->get['lng'])){
  $this->session->data['language'] = $this->request->get['lng'];
}

2- added the following lines to .htaccess file:

RewriteRule ^en/([^?]*) index.php?_route_=$1&lng=en [L,QSA]
RewriteRule ^ar/([^?]*) index.php?_route_=$1&lng=ar [L,QSA]

3- added languages flags to the template file:

<div class="languageFlags">
 {% for language in languages %}
  <a href="/{{ language['code'] }}">
   <img src="catalog/language/{{ language['code'] }}/{{ language['code'] }}.png" alt="{{ language['name'] }}" title="{{ language['name'] }}" />
  </a>
 {% endfor %}
</div>

Now when I navigate to www.mysite.com/en it will go to English language and this is true for other languages, and now search engine can index other languages pages.