<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>SL Auto-Translater</name>
    <code>ATSL</code>
    <version>1.0</version>
    <author>illats</author>
    <link>https://nocturne.com.ua</link>
    <file path="catalog/view/theme/coloring/template/common/header.tpl">
        <operation>
            <search><![CDATA[<?php foreach ($analytics as $analytic) { ?>]]></search>
            <add position="before"><![CDATA[
                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
            <script src="//translate.google.com/translate_a/element.js?cb=TranslateInit"></script>]]></add>
        </operation>
    </file>
        <file path="catalog/controller/common/language.php">
        <operation>
            <search><![CDATA[return $this->load->view('common/language', $data);]]></search>
            <add position="before"><![CDATA[$data['auto_translater']['languages'] =  $this->config->get('auto_translater_languages');
            $data['auto_translater']['status'] =   $this->config->get('auto_translater_status');
            if(isset($_COOKIE['googtrans']) and $_COOKIE['googtrans'] !== "null"){
                $data['auto_translater']['current_lang'] = substr($_COOKIE['googtrans'],-2);
            }else{
                $data['auto_translater']['current_lang'] = substr($this->config->get('config_language'),0,2);
            } // в модификатор
            $data['auto_translater']['default_lang'] = substr($this->config->get('config_language'),0,2);]]></add>
        </operation>
    </file>
       
        <file path="catalog/view/theme/coloring/template/common/language.tpl">
        <operation>
            <search><![CDATA[<?php if (count($languages) > 1) { ?>]]></search>
            <add position="replace"><![CDATA[<?php if (count($languages) > 1 and $auto_translater['status'] != 1) { ?>]]></add>
        </operation>
        <operation>
            <search index="4"><![CDATA[<?php } ?>]]></search>
            <add position="replace"><![CDATA[<?php } elseif(count($auto_translater['languages']) > 1 and $auto_translater['status'] == 1){ ?>
<div class="pull-left">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="language">
  <div class="btn-group">
    <button class="btn dropdown-toggle" data-toggle="dropdown">
       <span class=""><strong><span class="fa fa fa-angle-down caretalt"></span>&nbsp;&nbsp;<img src="/catalog/view/javascript/auto_translater/<?=$auto_translater['current_lang']?>.jpg"></strong></span>&nbsp;</button>
    
    <ul class="dropdown-menu">
      <?php foreach($auto_translater['languages'] as $code => $name) { ?>
        <li><a href="#" alt="<?=$code?>" data-google-lang="<?=$code?>"><img src="/catalog/view/javascript/auto_translater/<?=$code?>.jpg"></a></li>
      <?php } ?>
    </ul>
  </div>
  <input type="hidden" name="code" value="" />
  <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</form>
</div>
<style>
  body{
    top: 0px !important;
  }
  .skiptranslate {
    display: none !important;
}
</style>
<script>
      if (typeof googleTranslateConfig == 'undefined') {
          var googleTranslateConfig = {
            lang: "<?=$auto_translater['default_lang']?>",
        }
    };

function TranslateInit() {

    let code = TranslateGetCode();
    if (code == googleTranslateConfig.lang) {
        // Если язык по умолчанию, совпадает с языком на который переводим
        // То очищаем куки
        TranslateClearCookie();
    }

    // Инициализируем виджет с языком по умолчанию
    new google.translate.TranslateElement({
        pageLanguage: googleTranslateConfig.lang,
    });

    // Вешаем событие клик на флаги
    $('[data-google-lang]').click(function () {
        TranslateSetCookie($(this).attr("data-google-lang"))

        // Перезагружаем страницу
        window.location.reload();
    });
}

function TranslateGetCode() {
    // Если куки нет, то передаем дефолтный язык
    let lang = ($.cookie('googtrans') != undefined && $.cookie('googtrans') != "null") ? $.cookie('googtrans') : googleTranslateConfig.lang;
    return lang.substr(-2);
}

function TranslateClearCookie() {
    $.cookie('googtrans', null);
    $.cookie("googtrans", null, {
        domain: "." + document.domain,
    });
}

function TranslateSetCookie(code) {
    // Записываем куки /язык_который_переводим/язык_на_который_переводим
    $.cookie('googtrans', "/auto/" + code);
    $.cookie("googtrans", "/auto/" + code, {
        domain: "." + document.domain,
    });
}
</script>
<?php } ?>]]></add>
        </operation>
    </file>
</modification>