-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added translator interface and moved Bing translator to separate class
- Loading branch information
Showing
3 changed files
with
54 additions
and
23 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
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,38 @@ | ||
<?php | ||
|
||
namespace Efabrica\TranslationsAutomatization\Translator; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class BingTranslator implements TranslatorInterface | ||
{ | ||
private $from; | ||
|
||
private $to; | ||
|
||
public function __construct(string $from, string $to) | ||
{ | ||
$this->from = $from; | ||
$this->to = $to; | ||
} | ||
|
||
public function translate(array $strings): array | ||
{ | ||
$guzzleClient = new Client(); | ||
$options = [ | ||
'form_params' => [ | ||
'text' => implode(' | ', $strings), | ||
'from' => $this->from, | ||
'to' => $this->to, | ||
] | ||
]; | ||
$request = $guzzleClient->request('POST', 'https://www.bing.com/ttranslate', $options); | ||
|
||
$newStrings = []; | ||
$response = json_decode((string) $request->getBody(), true); | ||
if ($response['statusCode'] === 200) { | ||
$newStrings = explode(' | ', $response['translationResponse']); | ||
} | ||
return array_combine($strings, $newStrings); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Efabrica\TranslationsAutomatization\Translator; | ||
|
||
interface TranslatorInterface | ||
{ | ||
/** | ||
* @param array $strings - list of strings to be translated | ||
* @return array - list of translated strings in format old_string => new_string | ||
*/ | ||
public function translate(array $strings): array; | ||
} |