v1.7-v8x Tutorial: How to remove main language in URL

Biogent

Member
XNullUser
Joined
Jun 8, 2023
Messages
58
Reaction score
2
Points
8
Location
Ukraine
NullCash
200
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.
  1. Go to File Manager via your hosting control panel and find "override" folder.
  2. 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).
  3. Otherwise, you need to create or change files named Link.php and Tools.php. Do not forget about encoding (UTF-8).
  4. 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:

  1. 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);
        }
    }
  2. Login to the Prestashop back office, go to the Advance Parameter -> Performance and Clear Cache.
  3. 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

  • classes.zip
    1.8 KB · Views: 0
Last edited:

d-shilko

Well-known member
Pro
Master
Diamond
Elite
Joined
Jun 10, 2021
Messages
2,496
Reaction score
1,425
Points
113
NullCash
6,064
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.
  1. Go to File Manager via your hosting control panel and find "override" folder.
  2. 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).
  3. Otherwise, you need to create or change files named Link.php and Tools.php. Do not forget about encoding (UTF-8).
  4. 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:

  1. 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);
        }
    }
  2. Login to the Prestashop back office, go to the Advance Parameter -> Performance and Clear Cache.
  3. 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).
Hello. First of all, read the forum rules, please.
This kind of article has to be placed in the Request thread!
 

_sashok

Member
XNullUser
Joined
Dec 10, 2021
Messages
379
Reaction score
13
Points
18
NullCash
295
  1. 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).
 
Top