Jump to content

Problem ps_languageselector - language_code  instead of  iso_code


Recommended Posts

Hi, 

I have a problem with the module ps_languageselector.  I need to change the   hreflang="en" to hreflang="en-US". I know I have to use the variable language_code  instead of  iso_code but I don't know where I have to change it. Could you help me please? 

 

Final Code

<div id="language_selector" class="d-inline-block">
    <div class="language-selector-wrapper d-inline-block">
        <div class="language-selector dropdown js-dropdown">
            <a class="expand-more" data-toggle="dropdown" data-iso-code="es"><img src="https://url/img/l/3.jpg" alt="Español" class="img-fluid lang-flag" /> Español <i class="fa fa-angle-down" aria-hidden="true"></i></a>
            <div class="dropdown-menu">
                <ul>
                                            <li >
                            <a href="https://url/en/" rel="alternate" hreflang="en"
                               class="dropdown-item"><img src="https://url/img/l/1.jpg" alt="English" class="img-fluid lang-flag"  data-iso-code="en"/> English</a>
                        </li>
                                            <li  class="current" >
                            <a href="https://url/es/" rel="alternate" hreflang="es"
                               class="dropdown-item"><img src="https://url/img/l/3.jpg" alt="Español" class="img-fluid lang-flag"  data-iso-code="es"/> Español</a>
                        </li>
                                    </ul>
            </div>
        </div>
    </div>
</div>

 

ps_languageselector.tpl 

<div class="language-selector">
  <span>{$current_language.name_simple}</span>
  <ul>
    {foreach from=$languages item=language}
      <li {if $language.id_lang == $current_language.id_lang} class="current" {/if}>
        <a href="{$link->getLanguageLink($language.id_lang)}">{$language.name_simple}</a>
      </li>
    {/foreach}
  </ul>
</div>

 

ps_languageselector.php

if (!defined('_PS_VERSION_')) {
    exit;
}

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class Ps_Languageselector extends Module implements WidgetInterface
{
    /**
     * @var string Name of the module running on PS 1.6.x. Used for data migration.
     */
    const PS_16_EQUIVALENT_MODULE = 'blocklanguages';

    private $templateFile;

    public function __construct()
    {
        $this->name = 'ps_languageselector';
        $this->author = 'PrestaShop';
        $this->version = '2.1.0';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->trans('Language selector block', array(), 'Modules.Languageselector.Admin');
        $this->description = $this->trans('Adds a block allowing customers to select a language for your store\'s content.', array(), 'Modules.Languageselector.Admin');

        $this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);

        $this->templateFile = 'module:ps_languageselector/ps_languageselector.tpl';
    }

    public function install()
    {
        // Migrate data from 1.6 equivalent module (if applicable), then uninstall
        if (Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
            $oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
            if ($oldModule) {
                $oldModule->uninstall();
            }
        }

        return parent::install();
    }

    public function renderWidget($hookName = null, array $configuration = [])
    {
        $languages = Language::getLanguages(true, $this->context->shop->id);

        if (1 < count($languages)) {
            $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));

            return $this->fetch($this->templateFile);
        }

        return false;
    }

    public function getWidgetVariables($hookName = null, array $configuration = [])
    {
        $languages = Language::getLanguages(true, $this->context->shop->id);

        foreach ($languages as &$lang) {
            $lang['name_simple'] = $this->getNameSimple($lang['name']);
        }

        return array(
            'languages' => $languages,
            'current_language' => array(
                'id_lang' => $this->context->language->id,
                'name' => $this->context->language->name,
                'name_simple' => $this->getNameSimple($this->context->language->name),
				'iso_code' => $this->context->language->iso_code
            )
        );
    }

    private function getNameSimple($name)
    {
        return preg_replace('/\s\(.*\)$/', '', $name);
    }
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...