Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JaJuMa committed Nov 29, 2023
0 parents commit bfe86d1
Show file tree
Hide file tree
Showing 14 changed files with 864 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Block/QuickAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotGoogleTranslate\Block;

use Jajuma\PotGoogleTranslate\Helper\Config as HelperConfig;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Jajuma\PotGoogleTranslate\Model\Config\Source\Languages;

class QuickAction extends \Jajuma\PowerToys\Block\PowerToys\QuickAction
{
/**
* @var HelperConfig
*/
protected HelperConfig $helperConfig;

/**
* @var Languages
*/
protected Languages $languages;

/**
* @param Context $context
* @param HelperConfig $helperConfig
* @param Languages $languages
* @param array $data
*/
public function __construct(
Template\Context $context,
HelperConfig $helperConfig,
Languages $languages,
array $data = []
) {
parent::__construct($context, $data);
$this->helperConfig = $helperConfig;
$this->languages = $languages;
}

/**
* Is enable
*
* @return bool
*/
public function isEnable(): bool
{
return $this->helperConfig->isEnable();
}

/**
* Get approved languages
*
* @return array|string[]
*/
public function getApprovedLanguages(): array
{
$data = [];
$approved = $this->helperConfig->getApprovedLanguages();
if (!empty($approved)) {
$data = explode(',', $approved);
}

$defaultFrom = $this->getDefaultFromLanguage();
$defaultTo = $this->getDefaultToLanguage();

if (!in_array($defaultFrom, $data)) {
$data[] = $defaultFrom;
}

if (!in_array($defaultTo, $data)) {
$data[] = $defaultTo;
}

return $data;
}

/**
* Get default from language
*
* @return mixed
*/
public function getDefaultFromLanguage()
{
return $this->helperConfig->getDefaultFromLanguage();
}

/**
* Get default to language
*
* @return mixed
*/
public function getDefaultToLanguage()
{
return $this->helperConfig->getDefaultToLanguage();
}

/**
* Get all languages
*
* @return array[]
*/
public function getAllLanguages(): array
{
return $this->languages->toArray();
}
}
69 changes: 69 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotGoogleTranslate\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Config extends AbstractHelper
{
public const XML_PATH_ENABLE = 'power_toys/pot_google_translate/is_enabled';

public const XML_PATH_BASE_URL = 'power_toys/pot_google_translate/base_url';

public const XML_PATH_APPROVED_LANGUAGES = 'power_toys/pot_google_translate/approved_languages';

public const XML_PATH_APPROVED_DEFAULT_FROM_LANGUAGE = 'power_toys/pot_google_translate/default_from_language';

public const XML_PATH_APPROVED_DEFAULT_TO_LANGUAGE = 'power_toys/pot_google_translate/default_to_language';

/**
* Is enable
*
* @return bool
*/
public function isEnable(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLE);
}

/**
* Get base url
*
* @return mixed
*/
public function getBaseUrl()
{
return $this->scopeConfig->getValue(self::XML_PATH_BASE_URL);
}

/**
* Get approved languages
*
* @return mixed
*/
public function getApprovedLanguages()
{
return $this->scopeConfig->getValue(self::XML_PATH_APPROVED_LANGUAGES);
}

/**
* Get default from language
*
* @return mixed
*/
public function getDefaultFromLanguage()
{
return $this->scopeConfig->getValue(self::XML_PATH_APPROVED_DEFAULT_FROM_LANGUAGE);
}

/**
* Get default to language
*
* @return mixed
*/
public function getDefaultToLanguage()
{
return $this->scopeConfig->getValue(self::XML_PATH_APPROVED_DEFAULT_TO_LANGUAGE);
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 JaJuMa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
140 changes: 140 additions & 0 deletions Magewire/QuickAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotGoogleTranslate\Magewire;

use Magento\Framework\Phrase;
use Magewirephp\Magewire\Component;
use Jajuma\PotGoogleTranslate\Helper\Config as HelperConfig;
use Magento\Framework\Serialize\Serializer\Json as JsonSerialize;

class QuickAction extends Component
{
protected const DEFAULT_URL = 'https://translate.googleapis.com/translate_a/single';

protected $listeners = ['sendGoogleTranslateRequest'];

/**
* @var HelperConfig
*/
protected HelperConfig $helperConfig;

/**
* @var JsonSerialize
*/
protected JsonSerialize $jsonSerialize;

/**
* @param HelperConfig $helperConfig
* @param JsonSerialize $jsonSerialize
*/
public function __construct(
HelperConfig $helperConfig,
JsonSerialize $jsonSerialize
) {
$this->helperConfig = $helperConfig;
$this->jsonSerialize = $jsonSerialize;
}

/**
* Send google translate request
*
* @param $request
* @param string $from
* @param string $to
* @return void
*/
public function sendGoogleTranslateRequest($request, string $from = '', string $to = '')
{
$requestContent = '';
if (is_array($request)) {
if (!empty($request['request_content'])) {
$requestContent = $request['request_content'];
}
if (!empty($request['from_lang'])) {
$from = $request['from_lang'];
}
if (!empty($request['to_lang'])) {
$to = $request['to_lang'];
}
} else {
$requestContent = $request;
}

if (!$requestContent) {
return;
}

if (strlen($requestContent) >= 5000) {
$message = 'Maximum number of characters exceeded: 5000';
$this->dispatchBrowserEvent('finish-request-pot-google-translate', ['message' => $message]);
return;
}

$from = !empty($from) ? $from : $this->helperConfig->getDefaultFromLanguage();
$to = !empty($to) ? $to : $this->helperConfig->getDefaultToLanguage();

$message = '';
$response = $this->sendRequest($requestContent, $from, $to);
$message = $this->getResult($response);

$this->dispatchBrowserEvent('finish-request-pot-google-translate', ['message' => $message]);
}

protected function sendRequest($requestContent, $from, $to)
{
$baseUrl = $this->helperConfig->getBaseUrl();
if (empty($baseUrl)) {
$baseUrl = self::DEFAULT_URL;
}

$params = [
'client' => 'gtx',
'dt' => 't',
'sl' => $from,
'tl' => $to,
'q' => $requestContent
];

$paramStr = '';
foreach ($params as $key => $value) {
$paramStr .= '&' . $key . '=' . $value;
}
rtrim($paramStr, '&');

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $baseUrl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $paramStr);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

$response = curl_exec($curl);
curl_close($curl);

return $response;
}

/**
* Get result
*
* @param $response
* @return Phrase|string
*/
protected function getResult($response)
{
$sentences = '';
$sentencesArray = $this->jsonSerialize->unserialize($response);
if (!$sentencesArray || !isset($sentencesArray[0])) {
return 'Please try latter';
}

foreach ($sentencesArray[0] as $s) {
$sentences .= $s[0] ?? '';
}

return $sentences;
}
}
46 changes: 46 additions & 0 deletions Model/Config/Source/Languages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotGoogleTranslate\Model\Config\Source;

class Languages
{
/**
* To option array
*
* @return array[]
*/
public function toOptionArray(): array
{
return [
['value' => 'auto', 'label' => 'Auto'],
['value' => 'en', 'label' => 'English'],
['value' => 'vi', 'label' => 'VietNam'],
['value' => 'de', 'label' => 'German'],
['value' => 'fr', 'label' => 'France'],
['value' => 'ja', 'label' => 'Japan'],
['value' => 'zh-CN', 'label' => 'Chinese'],
['value' => 'es', 'label' => 'Spanish'],
['value' => 'uk', 'label' => 'Ukraine'],
['value' => 'ru', 'label' => 'Russia'],
];
}

/**
* To array
*
* @return array
*/
public function toArray(): array
{
$array = [];
$languages = $this->toOptionArray();
foreach ($languages as $language) {
$value = $language['value'];
$label = $language['label'];
$array[$value] = $label;
}

return $array;
}
}
Loading

0 comments on commit bfe86d1

Please sign in to comment.