Monday, 4 March 2019

Magento2 Google Translation

1.Create file registration.php in app/code/Google/Translator/registration.php

<?php
/**
 * Ansar Husain
 * Google_Translator
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Google_Translator',
    __DIR__
);



2. Create file module.xml app/code/Google/Translator/etc/module.xml

<?xml version="1.0"?>
<!--
/**
* Ansar Husain
* Google Translator
**/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Google_Translator" setup_version="1.0.0" />
</config>

3. Create Block file Translate.php app/code/Google/Translator/Block/Translate.php

<?php
/**
 * Ansar Husain
 * Google Translator
 */

namespace Google\Translator\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
/**
 * Class Translate
 * @package Google\Translator\Block
 */
class Translate extends Template
{

    /**
     * @var ScopeConfigInterface
     */
    protected  $_scopeConfig;

    /**
     * Translate constructor.
     * @param Template\Context $context
     * @param ScopeConfigInterface $scopeConfig
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        ScopeConfigInterface $scopeConfig,
        array $data = []
     )
    {
        $this->_scopeConfig = $scopeConfig;

        parent::__construct($context, $data);
    }

    /**
     * @return mixed
     */
    public function isEnable()
    {
        return (bool)$this->_scopeConfig->getValue('translator/general/enable', ScopeInterface::SCOPE_STORE);
    }

    /**
     * @return mixed
     */
    public function getCountry(){

        return $this->_scopeConfig->getValue('translator/general/country', ScopeInterface::SCOPE_STORE);

    }

}


4. Create Model file Country.php app/code/Google/Translator/Model/Config/Source/Country.php

<?php
/**
 * Ansar Husain
 * Google Translator
 */
namespace Google\Translator\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

/**
 * Class Layout
 * @package Google\Translator\Model\Config\Source
 */
class Country implements  ArrayInterface
{

    /**
     * @return array
     */
    public function toOptionArray()
    {
        return [
            ['value' => 'af', 'label' => __('Afrikaans')],
            ['value' => 'sq', 'label' => __('Albanian')],
            ['value' => 'ar', 'label' => __('Arabic')],
            ['value' => 'az', 'label' => __('Azerbaijani')],
            ['value' => 'eu', 'label' => __('Basque')],
            ['value' => 'bn', 'label' => __('Bengali')],
            ['value' => 'be', 'label' => __('Belarusian')],
            ['value' => 'bg', 'label' => __('Bulgarian')],
            ['value' => 'ca', 'label' => __('Catalan')],
            ['value' => 'zh-CN', 'label' => __('Chinese Simplified')],
            ['value' => 'zh-TW', 'label' => __('Chinese Traditional')],
            ['value' => 'hr', 'label' => __('Croatian')],
            ['value' => 'cs', 'label' => __('Czech')],
            ['value' => 'da', 'label' => __('Danish')],
            ['value' => 'nl', 'label' => __('Dutch')],
            ['value' => 'en', 'label' => __('English')],
            ['value' => 'eo', 'label' => __('Esperanto')],
            ['value' => 'et', 'label' => __('Estonian')],
            ['value' => 'tl', 'label' => __('Filipino')],
            ['value' => 'fi', 'label' => __('Finnish')],
            ['value' => 'fr', 'label' => __('French')],
            ['value' => 'gl', 'label' => __('Galician')],
            ['value' => 'ka', 'label' => __('Georgian')],
            ['value' => 'de', 'label' => __('German')],
            ['value' => 'el', 'label' => __('Greek')],
            ['value' => 'gu', 'label' => __('Gujarati')],
            ['value' => 'ht', 'label' => __('Haitian Creole')],
            ['value' => 'iw', 'label' => __('Hebrew')],
            ['value' => 'hi', 'label' => __('Hindi')],
            ['value' => 'hu', 'label' => __('Hungarian')],
            ['value' => 'is', 'label' => __('Icelandic')],
            ['value' => 'id', 'label' => __('Indonesian')],
            ['value' => 'ga', 'label' => __('Irish')],
            ['value' => 'it', 'label' => __('Italian')],
            ['value' => 'ja', 'label' => __('Japanese')],
            ['value' => 'kn', 'label' => __('Kannada')],
            ['value' => 'ko', 'label' => __('Korean')],
            ['value' => 'la', 'label' => __('Latin')],
            ['value' => 'lv', 'label' => __('Latvian')],
            ['value' => 'lt', 'label' => __('Lithuanian')],
            ['value' => 'mk', 'label' => __('Macedonian')],
            ['value' => 'ms', 'label' => __('Malay')],
            ['value' => 'mt', 'label' => __('Maltese')],
            ['value' => 'no', 'label' => __('Norwegian')],
            ['value' => 'fa', 'label' => __('Persian')],
            ['value' => 'pl', 'label' => __('Polish')],
            ['value' => 'pt', 'label' => __('Portuguese')],
            ['value' => 'ro', 'label' => __('Romanian')],
            ['value' => 'ru', 'label' => __('Russian')],
            ['value' => 'sr', 'label' => __('Serbian')],
            ['value' => 'sk', 'label' => __('Slovak')],
            ['value' => 'sl', 'label' => __('Slovenian')],
            ['value' => 'es', 'label' => __('Spanish')],
            ['value' => 'sw', 'label' => __('Swahili')],
            ['value' => 'sv', 'label' => __('Swedish')],
            ['value' => 'ta', 'label' => __('Tamil')],
            ['value' => 'te', 'label' => __('Telugu')],
            ['value' => 'th', 'label' => __('Thai')],
            ['value' => 'tr', 'label' => __('Turkish')],
            ['value' => 'uk', 'label' => __('Ukrainian')],
            ['value' => 'ur', 'label' => __('Urdu')],
            ['value' => 'vi', 'label' => __('Vietnamese')],
            ['value' => 'cy', 'label' => __('Welsh')],
            ['value' => 'yi', 'label' => __('Yiddish')]

        ];
    }

}

5. Create Laout file default.xml app/code/Google/Translator/view/frontend/layout/default.xml

<?xml version="1.0"?>
<!--
/**
* Ansar Husain
* Google Translator
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="header.panel">
            <block class="Google\Translator\Block\Translate" name="google.translator" template="Google_Translator::html/translate.phtml" before="-"/>
        </referenceContainer>
    </body>
</page>

6. Create Template file translate.phtml app/code/Google/Translator/view/frontend/templates/translate.phtml

<?php

/**
 * Ansar Husain
 * Google Translator
 */
?>
<?php if($block->isEnable()) /** if module enable  */ { ?>
<?php /** @var $block \Google\Translator\Block\Translate */ ?>

<div id="google_translate_element"></div>


    <script type="text/javascript">
        function googleTranslateElementInit()
        {
            
            new google.translate.TranslateElement(
                {
                    pageLanguage: 'en',
                    includedLanguages: '<?php echo strtolower($block->getCountry()); ?>',
                    layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL
                },'google_translate_element');
        }
</script>

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<style type="text/css">
    #google_translate_element{

        width:15%;float: left;
    }

    iframe.goog-te-banner-frame{ display: none !important;}

    .goog-logo-link {
        display:none !important;
    }

    .goog-te-gadget {
        color: transparent !important;
    }

    .goog-te-gadget .goog-te-combo {
        color: blue !important;
    }

    body {position: static !important; top:0px !important;}

    .goog-tooltip {
        display: none !important;
    }
    .goog-tooltip:hover {
        display: none !important;
    }
    .goog-text-highlight {
        background-color: transparent !important;
        border: none !important;
        box-shadow: none !important;
    }
</style>

<?php } /** module enable check close */?>

7. Create configration  file system.xml app/code/Google/Translator/etc/adminhtml/system.xml

<?xml version="1.0"?>
<!--
/**
* Ansar Husain
* Google_Translator
**/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
    <tab id="google_translator" translate="label" sortOrder="0">
        <label>Google Translator</label>
    </tab>
    <section id="translator" translate="label"  sortOrder="100"  showInDefault="1" showInWebsite="1" showInStore="1">
        <class>google-translator</class>
        <label>Settings</label>
        <tab>google_translator</tab>
        <resource>Google_Translator::translator_config</resource>
        <group id="general" translate="label" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>General Configuration</label>
            <field id="enable" translate="label" type="select" showInDefault="1" showInStore="1" showInWebsite="1">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="country" translate="label" type="multiselect" showInDefault="1" showInStore="1" showInWebsite="1">
                     <label>Choose Languages</label>
                     <source_model>Google\Translator\Model\Config\Source\Country</source_model>
                     <can_be_empty>1</can_be_empty>
                     <comment>Please choose languages which you want to show in language drop down frontend.</comment>
            </field>
        </group>
    </section>
    </system>
</config>

Final Result will be 

Admin Config will be 


No comments:

Post a Comment