Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mutiple strings in same request #14

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"require": {
"php": "^5.5 || ^7.0",
"ext-mbstring": "*",
"php-http/httplug": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.0",
Expand Down
5 changes: 2 additions & 3 deletions src/Service/BingTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
use Http\Message\RequestFactory;
use Psr\Http\Message\ResponseInterface;
use Translation\Translator\Exception\ResponseException;
use Translation\Translator\TranslatorService;

/**
* @author Baptiste Leduc <[email protected]>
*/
class BingTranslator extends HttpTranslator implements TranslatorService
class BingTranslator extends HttpTranslator
{
/**
* @var string
Expand Down Expand Up @@ -73,7 +72,7 @@ public function translate($string, $from, $to)
}

foreach ($data as $details) {
return $details['translations'][0]['text'];
return $this->format($string, $details['translations'][0]['text']);
}
}

Expand Down
23 changes: 1 addition & 22 deletions src/Service/GoogleTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
use Http\Message\RequestFactory;
use Psr\Http\Message\ResponseInterface;
use Translation\Translator\Exception\ResponseException;
use Translation\Translator\TranslatorService;

/**
* @author Tobias Nyholm <[email protected]>
*/
class GoogleTranslator extends HttpTranslator implements TranslatorService
class GoogleTranslator extends HttpTranslator
{
/**
* @var string
Expand Down Expand Up @@ -88,24 +87,4 @@ private function getUrl($string, $from, $to, $key)
urlencode($string)
);
}

/**
* @param string $original
* @param string $translationHtmlEncoded
*
* @return string
*/
private function format($original, $translationHtmlEncoded)
{
$translation = html_entity_decode($translationHtmlEncoded, ENT_QUOTES | ENT_HTML401, 'UTF-8');

// if capitalized, make sure we also capitalize.
$firstChar = mb_substr($original, 0, 1);
if (mb_strtoupper($firstChar) === $firstChar) {
$first = mb_strtoupper(mb_substr($translation, 0, 1));
$translation = $first.mb_substr($translation, 1);
}

return $translation;
}
}
48 changes: 47 additions & 1 deletion src/Service/HttpTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Translation\Translator\TranslatorService;

/**
* @author Tobias Nyholm <[email protected]>
*/
abstract class HttpTranslator
abstract class HttpTranslator implements TranslatorService
{
/**
* @var HttpClient
Expand Down Expand Up @@ -57,4 +58,49 @@ protected function getRequestFactory()
{
return $this->requestFactory;
}

/**
* {@inheritdoc}
*/
public function translateArray($strings, $from, $to)
{
$array = [];

foreach ($strings as $string) {
$array[] = $this->translate($string, $from, $to);
}

return $array;
}

/**
* @param string $original
* @param string $translationHtmlEncoded
*
* @return string
*/
protected function format($original, $translationHtmlEncoded)
{
$translation = htmlspecialchars_decode($translationHtmlEncoded);

// if capitalized, make sure we also capitalize.
$firstChar = \mb_substr($original, 0, 1);
$originalIsUpper = \mb_strtoupper($firstChar) === $firstChar;

if ($originalIsUpper) {
$first = \mb_strtoupper(\mb_substr($translation, 0, 1));
$translation = $first.\mb_substr($translation, 1);
}

// also check on translated if capitalize and original isn't
$transFirstChar = \mb_substr($translationHtmlEncoded, 0, 1);
$translationIsUpper = \mb_strtoupper($transFirstChar) === $transFirstChar;

if (!$originalIsUpper && $translationIsUpper) {
$first = \mb_strtolower(\mb_substr($translation, 0, 1));
$translation = $first.\mb_substr($translation, 1);
}

return $translation;
}
}
23 changes: 1 addition & 22 deletions src/Service/YandexTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
use Http\Message\RequestFactory;
use Psr\Http\Message\ResponseInterface;
use Translation\Translator\Exception\ResponseException;
use Translation\Translator\TranslatorService;

/**
* @author Tobias Nyholm <[email protected]>
*/
class YandexTranslator extends HttpTranslator implements TranslatorService
class YandexTranslator extends HttpTranslator
{
/**
* @var string
Expand Down Expand Up @@ -88,24 +87,4 @@ private function getUrl($string, $from, $to, $key)
urlencode($string)
);
}

/**
* @param string $original
* @param string $translationHtmlEncoded
*
* @return string
*/
private function format($original, $translationHtmlEncoded)
{
$translation = htmlspecialchars_decode($translationHtmlEncoded);

// if capitalized, make sure we also capitalize.
$firstChar = mb_substr($original, 0, 1);
if (mb_strtoupper($firstChar) === $firstChar) {
$first = mb_strtoupper(mb_substr($translation, 0, 1));
$translation = $first.mb_substr($translation, 1);
}

return $translation;
}
}
16 changes: 15 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ final class Translator implements LoggerAwareInterface, TranslatorService
* @return null|string Null is return when all translators failed.
*/
public function translate($string, $from, $to)
{
list($result) = $this->translateArray([$string], $from, $to);

return $result;
}

/**
* @param array $strings
* @param string $from
* @param string $to
*
* @return null|array Null is return when all translators failed.
*/
public function translateArray($strings, $from, $to)
{
foreach ($this->translatorServices as $service) {
try {
return $service->translate($string, $from, $to);
return $service->translateArray($strings, $from, $to);
} catch (TranslatorException\NoTranslationFoundException $e) {
// Do nothing, try again.
} catch (TranslatorException $e) {
Expand Down
11 changes: 11 additions & 0 deletions src/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ interface TranslatorService
* @throws \Translation\Translator\Exception if we could not translate string
*/
public function translate($string, $from, $to);

/**
* @param array $strings array of strings to translate
* @param string $from from what locale
* @param string $to to what locale
*
* @return array Return the translated strings
*
* @throws \Translation\Translator\Exception if we could not translate string
*/
public function translateArray($strings, $from, $to);
}
60 changes: 60 additions & 0 deletions tests/Integration/AbstractTranslatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

namespace Translation\translator\tests\Integration;

use PHPUnit\Framework\TestCase;
use Translation\Translator\TranslatorService;

/**
* @author Baptiste Leduc <[email protected]>
*/
abstract class AbstractTranslatorTest extends TestCase
{
/**
* @var TranslatorService
*/
protected $translator = null;

/**
* @var array
*/
protected $requested = ['apple', 'cherry'];

/**
* @var array
*/
protected $expected = ['pomme', 'cerise'];

/**
* @var string
*/
protected $from = 'en';

/**
* @var string
*/
protected $to = 'fr';

public function testTranslate()
{
if (null === $this->translator) {
$this->markTestSkipped('No translator set.');
}

$result = $this->translator->translate($this->requested[0], $this->from, $this->to);
$this->assertEquals($this->expected[0], $result);

$results = $this->translator->translateArray($this->requested, $this->from, $this->to);
$this->assertEquals($this->expected, $results);
}
}
13 changes: 4 additions & 9 deletions tests/Integration/BingTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@

namespace Translation\translator\tests\Integration;

use PHPUnit\Framework\TestCase;
use Translation\Translator\Service\BingTranslator;

/**
* @author Baptiste Leduc <[email protected]>
*/
class BingTranslatorTest extends TestCase
class BingTranslatorTest extends AbstractTranslatorTest
{
public function testTranslate()
public function setUp()
{
$key = getenv('BING_KEY');
if (empty($key)) {
$this->markTestSkipped('No Bing key in environment');
if (!empty($key)) {
$this->translator = new BingTranslator($key);
}

$translator = new BingTranslator($key);
$result = $translator->translate('apple', 'en', 'fr');
$this->assertEquals('pomme', $result);
}
}
13 changes: 4 additions & 9 deletions tests/Integration/GoogleTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@

namespace Translation\translator\tests\Integration;

use PHPUnit\Framework\TestCase;
use Translation\Translator\Service\GoogleTranslator;

/**
* @author Tobias Nyholm <[email protected]>
*/
class GoogleTranslatorTest extends TestCase
class GoogleTranslatorTest extends AbstractTranslatorTest
{
public function testTranslate()
public function setUp()
{
$key = getenv('GOOGLE_KEY');
if (empty($key)) {
$this->markTestSkipped('No Google key in environment');
if (!empty($key)) {
$this->translator = new GoogleTranslator($key);
}

$translator = new GoogleTranslator($key);
$result = $translator->translate('Grattis, du är klar!', 'sv', 'en');
$this->assertEquals('Congratulations, you\'re done!', $result);
}
}
23 changes: 14 additions & 9 deletions tests/Integration/YandexTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@

namespace Translation\translator\tests\Integration;

use PHPUnit\Framework\TestCase;
use Translation\Translator\Service\YandexTranslator;

/**
* @author Tobias Nyholm <[email protected]>
*/
class YandexTranslatorTest extends TestCase
class YandexTranslatorTest extends AbstractTranslatorTest
{
public function testTranslate()
/**
* @var array
*/
protected $expected = ['яблоко', 'вишня'];

/**
* @var string
*/
protected $to = 'ru';

public function setUp()
{
$key = getenv('YANDEX_KEY');
if (empty($key)) {
$this->markTestSkipped('No Yandex key in environment');
if (!empty($key)) {
$this->translator = new YandexTranslator($key);
}

$translator = new YandexTranslator($key);
$result = $translator->translate('apple', 'en', 'ru');
$this->assertEquals('яблоко', $result);
}
}