Skip to content

Commit

Permalink
Merge pull request #17 from prugala/feature/deepl-formality-and-changes
Browse files Browse the repository at this point in the history
Allow setting formality parameter for DeepL providers + new provider for free version of DeepL
  • Loading branch information
bramalho authored May 27, 2021
2 parents 1d20351 + ff22ca2 commit 75d07ae
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Analysis
on: [push]
on: [push, pull_request]

jobs:
php-cs-fixer:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Tests
on: [push]
on: [push, pull_request]

jobs:
build-test:
Expand Down
19 changes: 17 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ composer require divante-ltd/pimcore-translation-bundle
Available providers:
- `google_translate`
- `deepl`
- `deepl_free` - free version of DeepL API
- `microsoft_translate`

```
divante_translation:
api_key:
source_lang:
provider: # default provider: google_translate
provider: # default provider: google_translate
formality: # working for providers deepl and deepl_free only.
```

Enable the Bundle:
#### DeepL Formality:
Sets whether the translated text should lean towards formal or informal language.\
This feature currently only works for target languages "DE" (German), "FR" (French), "IT" (Italian), "ES" (Spanish), "NL" (Dutch), "PL" (Polish), "PT-PT", "PT-BR" (Portuguese) and "RU" (Russian).

Possible options are:\
"default" (default)\
"more" - for a more formal language\
"less" - for a more informal language\

### Enable the Bundle:
```bash
bin/console pimcore:bundle:enable DivanteTranslationBundle
```
Expand All @@ -59,6 +70,10 @@ Create Provider and implement interface
DivanteTranslationBundle\Provider\ProviderInterface
```

If your provider has a option to set `formality` option implement interface:
```
DivanteTranslationBundle\Provider\FormalityProviderInterface
```

#### How it works?
![Screenshot](docs/translate.png)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('provider')
->defaultValue('google_translate')
->end()
->scalarNode('formality')
->defaultValue('default')
->end()
->end();

return $treeBuilder;
Expand Down
19 changes: 19 additions & 0 deletions src/DivanteTranslationBundle/Provider/DeeplFreeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author Piotr Rugała <piotr@isedo.pl>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace DivanteTranslationBundle\Provider;

class DeeplFreeProvider extends DeeplProvider
{
protected string $url = 'https://api-free.deepl.com/';

public function getName(): string
{
return 'deepl_free';
}
}
13 changes: 11 additions & 2 deletions src/DivanteTranslationBundle/Provider/DeeplProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@

use DivanteTranslationBundle\Exception\TranslationException;

class DeeplProvider extends AbstractProvider
class DeeplProvider extends AbstractProvider implements FormalityProviderInterface
{
protected string $url = 'https://api.deepl.com/';
protected string $formality = 'default';

public function setFormality(?string $formality): self
{
$this->formality = $formality ?? $this->formality;

return $this;
}

public function translate(string $data, string $targetLanguage): string
{
Expand All @@ -25,6 +33,7 @@ public function translate(string $data, string $targetLanguage): string
'auth_key' => $this->apiKey,
'text' => $data,
'target_lang' => locale_get_primary_language($targetLanguage),
'formality' => $this->formality
]
]
);
Expand All @@ -35,7 +44,7 @@ public function translate(string $data, string $targetLanguage): string
throw new TranslationException();
}

return $data['data']['translations'][0]['text'];
return $data['translations'][0]['text'];
}

public function getName(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @author Piotr Rugała <piotr@isedo.pl>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace DivanteTranslationBundle\Provider;

interface FormalityProviderInterface
{
public function setFormality(?string $formality): self;
}
9 changes: 8 additions & 1 deletion src/DivanteTranslationBundle/Provider/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class ProviderFactory
{
private string $apiKey;
private iterable $providers;
private ?string $formality;

public function __construct(string $apiKey, iterable $providers)
public function __construct(string $apiKey, iterable $providers, ?string $formality)
{
$this->apiKey = $apiKey;
$this->providers = $providers;
$this->formality = $formality;
}

public function get(string $name): ProviderInterface
Expand All @@ -27,6 +29,11 @@ public function get(string $name): ProviderInterface
foreach ($this->providers as $provider) {
if ($provider->getName() === $name) {
$provider->setApiKey($this->apiKey);

if ($provider instanceof FormalityProviderInterface) {
$provider->setFormality($this->formality);
}

return $provider;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/DivanteTranslationBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ services:
arguments:
$apiKey: '%divante_translation.api_key%'
$providers: !tagged translation_bundle.provider
$formality: '%divante_translation.formality%'
50 changes: 50 additions & 0 deletions tests/DivanteTranslationBundle/Provider/DeeplFreeProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @author Piotr Rugała <piotr@isedo.pl>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace Tests\DivanteTranslationBundle\Provider;

use DivanteTranslationBundle\Provider\DeeplFreeProvider;
use DivanteTranslationBundle\Provider\ProviderInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

final class DeeplFreeProviderTest extends TestCase
{
public function testTranslate(): void
{
$response = [
'translations' => [
[
'text' => 'test'
],
],
];

$this->assertSame('test', $this->createProvider($response)->translate('test', 'en'));
}

private function createProvider(array $response): ProviderInterface
{
$mock = new MockHandler([
new Response(200, [], json_encode($response)),
]);
$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);
$provider = $this->getMockBuilder(DeeplFreeProvider::class)
->onlyMethods(['getHttpClient'])
->getMock();
$provider->method('getHttpClient')->willReturn($client);
$provider->setApiKey('test');
$provider->setFormality('more');

return $provider;
}
}
10 changes: 4 additions & 6 deletions tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Tests\DivanteTranslationBundle\Provider;

use DivanteTranslationBundle\Provider\DeeplProvider;
use DivanteTranslationBundle\Provider\GoogleProvider;
use DivanteTranslationBundle\Provider\ProviderInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
Expand All @@ -22,11 +21,9 @@ final class DeeplProviderTest extends TestCase
public function testTranslate(): void
{
$response = [
'data' => [
'translations' => [
[
'text' => 'test'
],
'translations' => [
[
'text' => 'test'
],
],
];
Expand All @@ -46,6 +43,7 @@ private function createProvider(array $response): ProviderInterface
->getMock();
$provider->method('getHttpClient')->willReturn($client);
$provider->setApiKey('test');
$provider->setFormality('more');

return $provider;
}
Expand Down
10 changes: 8 additions & 2 deletions tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

use ArrayObject;
use DivanteTranslationBundle\Exception\TranslationProviderNotImplemented;
use DivanteTranslationBundle\Provider\DeeplFreeProvider;
use DivanteTranslationBundle\Provider\DeeplProvider;
use DivanteTranslationBundle\Provider\GoogleProvider;
use DivanteTranslationBundle\Provider\MicrosoftProvider;
use DivanteTranslationBundle\Provider\ProviderFactory;
use DivanteTranslationBundle\Provider\ProviderInterface;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,17 +22,19 @@ final class ProviderFactoryTest extends TestCase
{
public function testGet(): void
{
$factory = new ProviderFactory('test', $this->getProviders());
$factory = new ProviderFactory('test', $this->getProviders(), 'default');

$this->assertInstanceOf(ProviderInterface::class, $factory->get('google_translate'));
$this->assertInstanceOf(ProviderInterface::class, $factory->get('deepl'));
$this->assertInstanceOf(ProviderInterface::class, $factory->get('deepl_free'));
$this->assertInstanceOf(ProviderInterface::class, $factory->get('microsoft_translate'));
}

public function testGetException(): void
{
$this->expectException(TranslationProviderNotImplemented::class);

$factory = new ProviderFactory('test', $this->getProviders());
$factory = new ProviderFactory('test', $this->getProviders(), 'default');
$factory->get('test');
}

Expand All @@ -39,6 +43,8 @@ private function getProviders(): iterable
$providers = [
new GoogleProvider(),
new DeeplProvider(),
new DeeplFreeProvider(),
new MicrosoftProvider(),
];

$arrayObject = new ArrayObject($providers);
Expand Down

0 comments on commit 75d07ae

Please sign in to comment.