-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T_COMMITTER_DATE="Thu Dec 13 11:25 2018 +0100" git commit --amend:#
- Loading branch information
0 parents
commit f68fd74
Showing
11 changed files
with
1,081 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: php | ||
php: | ||
- 7.2 | ||
before_install: | ||
- travis_retry composer self-update | ||
install: | ||
- travis_retry composer update --prefer-source | ||
script: | ||
- phpunit | ||
|
||
branches: | ||
only: | ||
- master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Raed Yak | ||
|
||
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. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "yak0d3/mirza_yandex_translator", | ||
"description": "Mirza Translator For Laravel gives you the ability to easily translate and manipulate text using the Yandex.Translate API.", | ||
"keywords": [ | ||
"laravel translator", | ||
"laravel yandex translate", | ||
"yandex translate", | ||
"Laravel translation package", | ||
"yandex translate", | ||
"yandex translate api" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "yak0d3", | ||
"email": "contact.raedyak@gmail.com" | ||
} | ||
], | ||
"require": {}, | ||
"autoload": { | ||
"psr-4": { | ||
"yak0d3\\mirza_yandex_translator\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"yak0d3\\mirza_yandex_translator\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.5" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"yak0d3\\mirza_yandex_translator\\MirzaServiceProvider" | ||
], | ||
"aliases": { | ||
"Mirza": "yak0d3\\mirza_yandex_translator\\MirzaFacade" | ||
}, | ||
"branch-alias": { | ||
"dev-master": "1.0.0-dev" | ||
} | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Mirza Yandex Translator Configuration File | ||
* | ||
* Tutorial: | ||
* 1. Publish this file by executing `php artisan vendor:publish --provider:"yak0d3/Mirza_Yandex_Translator/MirzaServiceProvider"` | ||
* 2. Add `YANDEX_KEY` variable to your .env file and set it to your own Yandex.Translate API Key | ||
* | ||
*/ | ||
|
||
|
||
return [ | ||
'secret' => env('YANDEX_KEY'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Package Tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
<?php | ||
/** | ||
* Mirza Yandex Translator For Laravel | ||
* Mirza makes it easy to translate and manipulate text using the Yandex.Translate API. | ||
* | ||
* @version 1.0.0 | ||
* @author yak0d3 <contact.raedyak@gmail.com> | ||
* @license MIT | ||
* @link https://github.com/yak0d3/Mirza_Yandex_Translator | ||
* @copyright 2018 Yak0d3 | ||
*/ | ||
|
||
namespace yak0d3\mirza_yandex_translator; | ||
|
||
use yak0d3\mirza_yandex_translator\MirzaClient as MirzaClient; | ||
|
||
use Exception; | ||
|
||
class Mirza{ | ||
/** | ||
* The variable that will contain the MirzaClient instance | ||
* | ||
* @var MirzaClient | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* Construct | ||
* | ||
* @param MirzaClient $MirzaClient | ||
*/ | ||
public function __construct(MirzaClient $MirzaClient){ | ||
$this->client = $MirzaClient; | ||
} | ||
/** | ||
* Translates string to the given language | ||
* | ||
* @param string $text | ||
* @param string $lang | ||
* @return string | ||
*/ | ||
public function translate(string $text,string $lang){ | ||
return $this->client->translate($text,$lang); | ||
} | ||
/** | ||
* Translates an array of text to the given language | ||
* The array can be associative or sequential | ||
* If the array is associative, an array with the same index names will be returned in a json encoded string | ||
* If the $assoc param is set to `true` and the given array is sequential an exception will be thrown | ||
* | ||
* @param array $textArray | ||
* @param string $lang | ||
* @param boolean $assoc | ||
* @return string | ||
* @throws Exception if the target language is not supported | ||
*/ | ||
public function translateArray(array $textArray, string $lang, $assoc = false){ | ||
if(!$this->isSupportedLang($lang)){ | ||
throw new Exception('The target language is not supported. Run getSupportedLanguages() to get the list of supported languages.'); | ||
} | ||
$keys = array_keys($textArray); | ||
$translated = array(); | ||
|
||
$index = 0; | ||
if($assoc) $this->isAssoc($textArray); | ||
foreach($textArray as $key => $text){ | ||
$translated[$key] = [ | ||
'originalText' => $text, | ||
'translatedText' => $this->client->translate($text,$lang) | ||
]; | ||
$index++; | ||
} | ||
|
||
return json_encode($translated); | ||
|
||
} | ||
/** | ||
* Translate a string to multiple languages | ||
* | ||
* @param string $text | ||
* @param array $langs | ||
* @return string | ||
* @throws Exception if one or more target languages are not supported | ||
*/ | ||
public function translateTo(string $text, array $langs){ | ||
$notSupported = array(); | ||
foreach($langs as $lang){ | ||
if(!$this->isSupportedLang($lang)){ | ||
array_push($notSupported, $lang); | ||
} | ||
} | ||
if(count($notSupported) > 0){ | ||
throw new Exception('The following languages are not supported: '.implode("\n", $notSupported)); | ||
} | ||
$textLang = $this->detectLanguage($text); | ||
$translatedText = ['originalText' => $text, 'originalLanguage' => $textLang,'text' => array()]; | ||
foreach($langs as $lang){ | ||
try{ | ||
$translatedText['text'][$lang] = $this->client->translate($text,$lang); | ||
} | ||
catch(Exception $e){ | ||
$translatedText['text'][$lang] = ''; | ||
} | ||
} | ||
unset($translatedText['text'][$textLang]); | ||
|
||
return json_encode($translatedText); | ||
} | ||
/** | ||
* Detects the language of the provided string | ||
* An exception will be thrown if the language could not be found. | ||
* | ||
* @param string $text | ||
* @param boolean $langName | ||
* @return string | ||
* @throws Exception if the language name is not found | ||
*/ | ||
public function detectLanguage(string $text,$langName = false){ | ||
$langCode = $this->client->detectLanguage($text); | ||
if($langName){ | ||
$allLanguages = $this->client->getLanguages(); | ||
|
||
if(array_key_exists($langCode,$allLanguages)) | ||
return $allLanguages->$langCode; | ||
else | ||
throw new Exception('Language name could not be found.'); | ||
} | ||
else | ||
return $langCode; | ||
} | ||
/** | ||
* Returns the list of all supported languages | ||
* | ||
* @return string | ||
*/ | ||
public function getSupportedLanguages(){ | ||
return json_encode($this->client->getLanguages()); | ||
} | ||
/** | ||
* Translates a string to all supported languages | ||
* This may take some time and cause a timeout exception | ||
* | ||
* @param string $text | ||
* @return string | ||
*/ | ||
public function translateToAll(string $text){ | ||
$langs = $this->client->getLanguages(true); | ||
$textLang = $this->detectLanguage($text); | ||
$translatedText = ['originalText' => $text, 'originalLanguage' => $textLang,'text' => array()]; | ||
unset($langs[array_search($textLang, $langs)]); | ||
|
||
foreach($langs as $lang){ | ||
try{ | ||
$translatedText['text'][$lang] = $this->client->translate($text,$lang); | ||
} | ||
catch(Exception $e){ | ||
$translatedText['text'][$lang] = ''; | ||
} | ||
} | ||
|
||
return json_encode($translatedText); | ||
} | ||
/** | ||
* Generates the "Powered by Yandex.Translate" link | ||
* | ||
* @param string $color | ||
* @param string $fontsize | ||
* @return string | ||
*/ | ||
public function yandex_rights(string $color = "#fff", string $fontsize = "14px"){ | ||
$copyrights = "Powered by Yandex.Translate"; | ||
|
||
return "<a href='https://translate.yandex.com/' target='_blank' style='font-size:$fontsize;color:$color;'>".$copyrights."</a>"; | ||
} | ||
/** | ||
* Generates an HTML `<select>` with the list of all supported languages | ||
* | ||
* @return string | ||
*/ | ||
public function languages_select(){ | ||
$select = '<select>'; | ||
$option = '<option value="_lang_code_">_lang_name_</option>'; | ||
foreach($this->client->supportedLanguages as $langCode => $langName){ | ||
$optionTemp = str_replace('_lang_code_',$langCode, $option); | ||
$optionTemp = str_replace('_lang_name_',$langName, $optionTemp); | ||
$select .= $optionTemp; | ||
} | ||
$select .= '</select>'; | ||
return $select; | ||
} | ||
/** | ||
* Checks if the array given is associative | ||
* | ||
* @param array $array | ||
* @return boolean | ||
* @throws Exception if the array given is not associative | ||
*/ | ||
private function isAssoc(array $array){ | ||
if(array() == $array || array_keys($array) === range(0, count($array) - 1)){ | ||
throw new Exception('Argument 1 given to translateArray is a sequential array, an associative array is expected.'); | ||
} | ||
return true; | ||
} | ||
/** | ||
* Checks if the language given is supported | ||
* | ||
* @param string $lang | ||
* @return boolean | ||
*/ | ||
private function isSupportedLang(string $lang){ | ||
if(!in_array($lang,json_decode(json_encode($this->client->supportedLanguages),true))) | ||
return false; | ||
else | ||
return true; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.