Hi there!
I want to share with everyone a simple method of removal add lang prefix to URL (example.com/en/, example.com/fr/ etc.) for main language. Personally, I needed this when moving from another CMS and I chose Prestashop in particular because of multilanguage OOTB.
I want to share with everyone a simple method of removal add lang prefix to URL (example.com/en/, example.com/fr/ etc.) for main language. Personally, I needed this when moving from another CMS and I chose Prestashop in particular because of multilanguage OOTB.
- Go to File Manager via your hosting control panel and find "override" folder.
- If you don't have files inside except .htaccess and index.php you can simply download attachment and unzip it at override folder (should be ../override/classes/ and files inside classes folder).
- Otherwise, you need to create or change files named Link.php and Tools.php. Do not forget about encoding (UTF-8).
- Copy this code to Link.php:
PHP:
<?php class Link extends LinkCore { protected function getLangLink($idLang = null, Context $context = null, $idShop = null) { static $psRewritingSettings = null; if ($psRewritingSettings === null) { $psRewritingSettings = (int) Configuration::get('PS_REWRITING_SETTINGS', null, null, $idShop); } if (!$context) { $context = Context::getContext(); } if ((!$this->allow && in_array($idShop, [$context->shop->id, null])) || !Language::isMultiLanguageActivated($idShop) || !$psRewritingSettings) { return ''; } if (!$idLang) { $idLang = $context->language->id; } if (Language::getIsoById($idLang) === 'ru') { return ''; } else { return Language::getIsoById($idLang) . '/'; } } }
Post automatically merged:
- Copy this code to Tools.php:
Code:
<?phpclass Tools extends ToolsCore { public static function switchLanguage(Context $context = null) { if (null === $context) { $context = Context::getContext(); } // On PrestaShop installations Dispatcher::__construct() gets called (and so Tools::switchLanguage()) // Stop in this case by checking the cookie if (!isset($context->cookie)) { return; } if ( ($iso = Tools::getValue('isolang')) && Validate::isLanguageIsoCode($iso) && ($id_lang = (int) Language::getIdByIso($iso)) ) { $_GET['id_lang'] = $id_lang; } else { $_GET['id_lang'] = (int) Configuration::get('PS_LANG_DEFAULT'); } // Only switch if new ID is different from old ID $newLanguageId = (int) Tools::getValue('id_lang'); if ( Validate::isUnsignedId($newLanguageId) && $newLanguageId !== 0 && $context->cookie->id_lang !== $newLanguageId ) { $context->cookie->id_lang = $newLanguageId; $language = new Language($newLanguageId); if (Validate::isLoadedObject($language) && $language->active && $language->isAssociatedToShop()) { $context->language = $language; } } Tools::setCookieLanguage($context->cookie); } }
- Login to the Prestashop back office, go to the Advance Parameter -> Performance and Clear Cache.
- Done! Try enter to your front and check language switching (with main language URL will look as example.com/ and for other as usual example.com/uk/ (additional lang), example.com/pl/ (additional lang).
Attachments
Last edited: