Skip to content

Commit

Permalink
Added translator interface and moved Bing translator to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco committed May 11, 2018
1 parent 840f9e8 commit 8106ec1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
27 changes: 4 additions & 23 deletions src/TokenModifier/BingTranslateTokenModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
namespace Efabrica\TranslationsAutomatization\TokenModifier;

use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
use GuzzleHttp\Client;
use Efabrica\TranslationsAutomatization\Translator\BingTranslator;

class BingTranslateTokenModifier implements TokenModifierInterface
{
private $from;

private $to;
private $translator;

public function __construct(string $from, string $to)
{
$this->from = $from;
$this->to = $to;
$this->translator = new BingTranslator($from, $to);
}

public function modifyAll(TokenCollection $tokenCollection): TokenCollection
Expand All @@ -28,23 +25,7 @@ public function modifyAll(TokenCollection $tokenCollection): TokenCollection
return $tokenCollection;
}

$guzzleClient = new Client();
$options = [
'form_params' => [
'text' => implode(' | ', $oldKeys),
'from' => $this->from,
'to' => $this->to,
]
];
$request = $guzzleClient->request('POST', 'https://www.bing.com/ttranslate', $options);

$newKeys = [];
$response = json_decode((string) $request->getBody(), true);
if ($response['statusCode'] === 200) {
$newKeys = explode(' | ', $response['translationResponse']);
}
$oldToNewKeys = array_combine($oldKeys, $newKeys);

$oldToNewKeys = $this->translator->translate($oldKeys);
foreach ($tokenCollection->getTokens() as $token) {
if (isset($oldToNewKeys[$token->getTranslationKey()])) {
$token->changeTranslationKey($oldToNewKeys[$token->getTranslationKey()]);
Expand Down
38 changes: 38 additions & 0 deletions src/Translator/BingTranslator.php
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);
}
}
12 changes: 12 additions & 0 deletions src/Translator/TranslatorInterface.php
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;
}

0 comments on commit 8106ec1

Please sign in to comment.