1) the language in the session if present
2) the language in the cookie if present
3) the languages in browser language accept header if one of those is supported by the site
4) the site default language
Default OC does not determine language from the url itself.
As such:
no matter what url you send a user, the language of the page displayed will be based on the order above.
This is also why search engines do not index multi-lingual seo urls, only the default one as they do not retain a session, cookie and send no language accept headers and therefore always receive the page in the default language.
Normally that would mean duplicate content but since most OC pages set a canonical url, that is not the case, still that canonical url is also in your default language hence that is the only url indexed.
So, the solution is to have a language identifier in your urls be it /en/ or /ru/ or lng=en or lng=ru, does not matter which as long as you can determine the requested language from it and it does not clash with used seo url keywords.
The only other alternative would be to determine which language a matching seo url keyword has when translating the seo url to get variables and override the default language with that language. Currently the seo url class does not care what the language is for a matching keyword-query pair, which is a shame.
That solution only works for seo urls and when you make sure that keywords are unique across languages.
There is a niche solution which only works if you only have one single-byte character language and one double-byte character language, which we have (en & zh), where you can simply check for that to determine the language, i.e. check if there is a double-byte character in the url.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
language indication in seo urls using first 2 characters of the language code:
catalog/config.php
add:
Code: Select all
define('URL_WITH_LANGUAGE', false);
catalog/controller/startup/startup.php
after:
Code: Select all
$languages = $this->model_localisation_language->getLanguages();
Code: Select all
/********** detect language indicator in the seo url *******/
if (URL_WITH_LANGUAGE && $this->config->get('config_seo_url')) {
if (isset($this->request->get['_route_'])) {
$url_parts = explode('/',$this->request->get['_route_']);
foreach ($languages as $key => $value) {
// only active languages
if ($value['status']) {
// using first part of language locale
$indicator = substr($value['code'],0,2);
foreach ($url_parts as $url_part) {
if ($indicator == $url_part) {
$code = $value['code'];
// remove language indicator
$this->request->get['_route_'] = str_replace($indicator.'/','',$this->request->get['_route_']);
break(2);
}
}
}
}
}
}
/**********************************************************/
Code: Select all
if (isset($this->session->data['language'])) {
Code: Select all
if ($code == '') {
Code: Select all
if (!array_key_exists($code, $languages)) {
$code = $this->config->get('config_language');
}
Code: Select all
}
catalog/controller/startup/seo_url.php
replace:
Code: Select all
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
Code: Select all
if (URL_WITH_LANGUAGE) {
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '/'.substr($this->session->data['language'],0,2), $url_info['path']) . $url . $query;
} else {
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
}
Since the language switcher is flawed when it comes to seo urls by default (it already sets the redirect url before a new language is selected making you redirect to the old language url with the new language page) and certainly when adding language indicators to the urls we need to rewrite the whole thing.
catalog/controller/common/language.php
replace everything with:
Code: Select all
<?php
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']
);
}
}
$this->session->data['save_get'] = $this->request->get;
return $this->load->view('common/language', $data);
}
public function language() {
$this->load->model('localisation/language');
$languages = $this->model_localisation_language->getLanguages();
$url = '';
if (isset($this->request->post['code']) && array_key_exists($this->request->post['code'],$languages) && $languages[$this->request->post['code']]['status']) {
$this->session->data['language'] = $this->request->post['code'];
if (isset($this->session->data['save_get'])) {
$xlang_id = $languages[$this->request->post['code']]['language_id'];
$curr_lang = $this->config->get('config_language_id');
$this->config->set('config_language_id',$xlang_id);
if (!isset($this->session->data['save_get']['route'])) {
$url_data = $this->session->data['save_get'];
unset($url_data['_route_']);
if ($url_data) $url = '&' . urldecode(http_build_query($url_data, '', '&'));
$redirect = $this->url->link('common/home', $url, $this->request->server['HTTPS']);
} else {
$url_data = $this->session->data['save_get'];
unset($url_data['_route_']);
$route = $url_data['route'];
unset($url_data['route']);
if ($url_data) $url = '&' . urldecode(http_build_query($url_data, '', '&'));
$redirect = $this->url->link($route, $url, $this->request->server['HTTPS']);
}
$this->config->set('config_language_id',$curr_lang);
unset($this->session->data['save_get']);
$this->response->redirect($redirect);
} else {
$this->response->redirect($this->url->link('common/home', $url, $this->request->server['HTTPS']));
}
} else {
$this->response->redirect($this->url->link('common/home', $url, $this->request->server['HTTPS']));
}
}
}
now you can go back to catalog/config.php
and turn it on by changing:
Code: Select all
define('URL_WITH_LANGUAGE', false);
Code: Select all
define('URL_WITH_LANGUAGE', true);
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
I am coping with the same problem...
Tried different options it did not work..
The code you kindly posted here - will it add a 2 letters suffix for the default language as well, right? If so, how to make the 2 letters suffix only for the language, which is not the default one?
And maybe there is a way to fix it with hreflang.. Some code for header, to auto display a correct hreflang, deppending on the url? Just a thought..
in catalog/controller/startup/startup.php
before this:
Code: Select all
// Customer
$customer = new Cart\Customer($this->registry);
$this->registry->set('customer', $customer);
Code: Select all
// get the default language and set in session
$this->load->model('setting/setting');
$this->session->data['language_default'] = $this->model_setting_setting->getSettingValue('config_language', (int)$this->config->get('config_store_id'));
you change this:
Code: Select all
if (URL_WITH_LANGUAGE) {
Code: Select all
// add language indicator to seo urls if not default language
if (URL_WITH_LANGUAGE && $this->session->data['language'] != $this->session->data['language_default']) {
x-hreflang has no meaning here, for that you still have to change your header controller and view with a loop thru all your languages.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Had 500 errors, when trying to fix this before... Trying to avoid extensions, not to ruin all the work, but with the language,
I decided to try this russian extension https://opencartadmin.com/seo-langmark ... ab-review .
No idea whether it is good or not, but on the forum i saw someone recommending it.
Do you know anything about this one maybe?
If it does not work ill get back to experiments with the code..
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
However, not sure about the main page... Will it work for the multilanguage sitemap? The default language is yourdomain.com and the other language main page will be yourdomain.com/en/ , for example?
Both main pages have the same - index.php?route=common/home...
Will the automatic sitemap feed show it all? Or i should make a sitemap myself? And one sitemap for both or two sitemaps for each? If two, then where to put the second one for the second language?
Sorry for so many questions...

wittom.in (maybe some other advices on what should i fix here... )
P.S. Checked sitemap generator and it does not see the pages in other language... (((
P.P.S. The instructions you gave are awesome!

P.P.S. The funny thing that happens now - when you put products in cart, you go to cart, (added) - change language in the cart!, then back to the homepage through logo, from there you change the language to the other one using flag, you get redirected back to the cart... Why can that be?! (OC 3.0.3.6, theme default) (added- happens from time to time, not only on cart, simply redirects to last page visited)
P.P.P.S. Managed to get the sitemap for other language - if i put for the url yourdomain.com/en/and the name for english version i set for it in seo urls...
viewtopic.php?p=798357#p798357
OC google sitemap does not do multi-lingual by default but uses the config language setting, best to copy the original file so you have google_sitemap_en.php and google_sitemap_de.php or any name you wish.
Then in each you set the language with:
Code: Select all
$this->config->set('config_language_id', x);
Code: Select all
public function index() {
that is because you are using an external cache (varnish), when you get a cache hit, it does not set a new save get so it retains the get variables of the previous-previous page.P.P.S. The funny thing that happens now - when you put products in cart, you go to cart, (added) - change language in the cart!, then back to the homepage through logo, from there you change the language to the other one using flag, you get redirected back to the cart... Why can that be?! (OC 3.0.3.6, theme default) (added- happens from time to time, not only on cart, simply redirects to last page visited)
as I said many times, additional external cache modules are more trouble than they are worth.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Gosh, you have just made my day and saved lots of my time and effort!!!!!
With you it is all so easy.. One hundred thousand thank you!

Any advice on the website in general, if i dont ask for too much?

P.S. Good, if you do not experience this, ill check maybe its something on my end... Thank you for looking into it!
1) I would implement http/2.0
2) update your cookies for samesite chrome
3) getting some 400 return code on an image
4) getting a 502 bad gateway on your youtube video
other than that, looks good.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Read my correction in that post, you are caching so the save get variables do not get updated if your varnish external cache gets a hit.P.S. Good, if you do not experience this, ill check maybe its something on my end... Thank you for looking into it!
you could possibly avoid this by putting this:
Code: Select all
$this->session->data['save_get'] = $this->request->get;
as that is the first opportunity to obtain the get variables when using seo urls and that is not cached.
Then take that statement out of the common/language class though I don't think it is necessary.
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
I will research what you mean about those 4 points.. (quit a noob, so need to google what you write about
I will try to look into caching as well! Thanks again for very valuable hints!!!!! I want to make it work finally!
Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.
Users browsing this forum: No registered users and 5 guests