If you have multiple languages installed in your shop, then all URLs will include language code. And even when you open the home page of your site you’ll get that language suffix, even for default language. It doesn’t look fine but fortunately you can fix this behavior.
This tutorial describes how to remove default language code from your URLs (usually “/en”).
Works for PrestaShop 1.5 / 1.6
You have to use overrides to do this.
-
Create (or edit if exists) file /override/classes/Link.php
Paste this code into that file (or merge with existing code):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<?php
class Link extends LinkCore
{
protected function getLangLink($id_lang = null, Context $context = null, $id_shop = null)
{
$parent_result = parent::getLangLink($id_lang, $context, $id_shop);
if ($parent_result) {
if ($id_lang == Configuration::get('PS_LANG_DEFAULT')) {
return '';
} else {
return $parent_result;
}
}
}
} -
Create (or edit if exists) file /override/classes/Tools.php
Paste this code into that file (or merge with existing code):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29<?php
class Tools extends ToolsCore
{
public static function setCookieLanguage($cookie = null)
{
parent::setCookieLanguage($cookie);
if (!$cookie) {
$cookie = Context::getContext()->cookie;
}
if(!Tools::getValue('isolang') && !Tools::getValue('id_lang') && !Tools::isSubmit('id_category_layered')) {
$cookie->id_lang = Configuration::get('PS_LANG_DEFAULT');
Context::getContext()->language = new Language($cookie->id_lang);
}
/* If language file not present, you must use default language file */
if (!$cookie->id_lang || !Validate::isUnsignedId($cookie->id_lang)) {
$cookie->id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
Context::getContext()->language = new Language($cookie->id_lang);
}
$iso = Language::getIsoById((int)$cookie->id_lang);
@include_once(_PS_THEME_DIR_.'lang/'.$iso.'.php');
return $iso;
}
}Note that on line #13 we use Tools::isSubmit(‘id_category_layered’) expression. It helps Blocklayered module to correctly determine the language. You can also add to this line your other modules that have problems with determining language.
- Now clear the shop cache (how to clear the cache).
All done! Open your website and check the result.
after this edit switching language working…
but email sended to customer is in wrong language. 🙁
PS. 1.6.1.22
what is the cause of this /en/ issue?, why is it suddenly appear on the url?
Hello, will this work on Prestashop 1.7 also?
yes it works, please see my postings above.
Copy past Link.php on override.
After, remplace
return Language::getIsoById($idLang).’/’;
by
if (Language::getIsoById($id_lang)==’fr’) return ”;
else
return Language::getIsoById($idLang).’/’;
Juste modify function getLangLink
Hello Bruno,
first of all for presta 1.7.x i think the $id_Lang in the IF should be like $idLang (without the _)
second, because i copy/pasted the code, i realized that it needs ” ‘ ” and not ” ’ ”
third, i tried it, i see that something is happening but somehow the IF does not work and it always shows me the ELSE statement result.
any idea what could be wrong?
here is the code i used for a presta 1.7.6.2 installation (on local host)
if (Language::getIsoById($idlang) === ‘ro’) {
return ”;
} else {
return Language::getIsoById($idLang).’/’;
}
somehow i always get back the Language::getIsoById($idLang).’/’; even if site is set to “ro” language it gives ……ro/
(i added some wording in both cases in order to know what is executed and it is always executed the ELSE statement like if the comparison inside IF is never true.
my mistake, the idLang is case sensitive.
so this code worked.
if (Language::getIsoById($idLang) === ‘ro’) {
return ”;
} else {
return Language::getIsoById($idLang).’/’;
}
of course where is ‘ro’ should be the initials of the language you want to not appear.
i didnt tested further to see what happens with customer emails etc etc.
so, the problem is like this:
if i switch from RO to EN, all is ok, website (i am working on localhost) turns to EN.
when i go up to the language selector and i chose RO, then of course the link does not include the /ro/ so since i had selected i guess EN, it thinks that it should serve me in english, so no change is happening and i am stuck with the EN.
even if i go and i replace manually the /en/ from the link and put /ro/ still it does not change to RO language.
so, i am getting stuck the first time the language changes.
Bruno, any idea would be helpful in this one.
The alteration that i did was only in the link.php file and only this:
if (Language::getIsoById($idLang) === ‘ro’) {
return ”;
} else {
return Language::getIsoById($idLang).’/’;
}
should i do also what is mentioned at the beggining of this thread about the presta 1.6 ?
i did the first 2 steps also and so far it seems to work!
email send to customer for order confirmation also seems to work ok.
the only problem that exists is that in whatever language i click the Home button (the logo lets say of the shop) it always returns to the default language.
Does anyone know how to make that link (its in the header.tpl to actually give the correct URL including (or not in case of the default shop) the /en/ ?
that would be of great help.
solution found:
go and find the header.tpl or however it is named.
you should find relatively on top (this depends i guess on the theme you are using) somewhere to be written inside an this: {$urls.base_url}
replace this with {$urls.pages.index} and you should be all ok.
at least it seems that it worked for me.
Hello Nikos!
I have change the link.php file.
But when i change the language it cannot be switched to the default language again.
How you fix it???? THANKS!!!
allow && in_array($idShop, array($context->shop->id, null))) || !Language::isMultiLanguageActivated($idShop) || !$psRewritingSettings) {
return ”;
}
if (!$idLang) {
$idLang = $context->language->id;
}
if (Language::getIsoById($id_lang)==’fr’) return ”;
else
return Language::getIsoById($idLang).’/’;
}
}
Solution for 1.7
allow && in_array($idShop, array($context->shop->id, null))) || !Language::isMultiLanguageActivated($idShop) || !$psRewritingSettings) {
return ”;
}
if (!$idLang) {
$idLang = $context->language->id;
}
if (Language::getIsoById($id_lang)==’fr’) return ”;
else
return Language::getIsoById($idLang).’/’;
}
}
Hi. I tried it, it works in most parts, but once I switch to another language from the ps_languageswitcher module, I cannot return to the default language. PS 1.7.4.2
Hi,
Sorry, this post was written a long time ago for PrestaShop 1.5/1.6…
I’ll try to find a solution for PS1.7
Works but the link in the logo is fix