Skip to content

Commit

Permalink
Microsoft translator support (#13)
Browse files Browse the repository at this point in the history
* Microsoft Translator Support

* Github Actions changes
  • Loading branch information
prugala authored Apr 14, 2021
1 parent 9fcc449 commit 5810bbd
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 5 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, pull_request]
on: [push]

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, pull_request]
on: [push]

jobs:
build-test:
Expand Down
11 changes: 8 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
[![Total Downloads](https://poser.pugx.org/divante-ltd/pimcore-translation-bundle/downloads)](https://packagist.org/packages/divante-ltd/pimcore-translation-bundle)
[![License](https://poser.pugx.org/divante-ltd/pimcore-translation-bundle/license)](https://github.com/DivanteLtd/divante-ltd/pimcore-translation-bundle/blob/master/LICENSE)

Copy data from the source language and translate it by using Google Translate, Deepl or other integration.
Supports input and wysiwyg.
Copy data from the source language and translate it by using:
- Google Translate (https://cloud.google.com/translate)
- Deepl (https://www.deepl.com/en/docs-api/)
- Microsoft Translator (global region) (https://azure.microsoft.com/en-en/services/cognitive-services/translator/)

Supports input, textarea and wysiwyg fields.

**Table of Contents**
- [Pimcore Translation Bundle](#google-translate)
Expand Down Expand Up @@ -35,12 +39,13 @@ composer require divante-ltd/pimcore-translation-bundle
Available providers:
- `google_translate`
- `deepl`
- `microsoft_translate`

```
divante_translation:
api_key:
source_lang:
provider: # default google_translate
provider: # default provider: google_translate
```

Enable the Bundle:
Expand Down
49 changes: 49 additions & 0 deletions src/DivanteTranslationBundle/Provider/MicrosoftProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @author Piotr Rugała <[email protected]>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace DivanteTranslationBundle\Provider;

use DivanteTranslationBundle\Exception\TranslationException;

class MicrosoftProvider extends AbstractProvider
{
protected string $url = 'https://api.cognitive.microsofttranslator.com/';

public function translate(string $data, string $targetLanguage): string
{
try {
$response = $this->getHttpClient()->request(
'POST',
'translate',
[
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->apiKey,
'X-ClientTraceId' => md5(uniqid((string) rand(), true)),
'Content-Type' => 'application/json',
],
'json' => [['Text' => $data]],
'query' => [
'api-version' => '3.0',
'to' => locale_get_primary_language($targetLanguage),
],
]
);
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
} catch (\Throwable $exception) {
throw new TranslationException();
}

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

public function getName(): string
{
return 'microsoft_translate';
}
}
4 changes: 4 additions & 0 deletions tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
/**
* @author Piotr Rugała <[email protected]>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
/**
* @author Piotr Rugała <[email protected]>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

Expand Down
51 changes: 51 additions & 0 deletions tests/DivanteTranslationBundle/Provider/MicrosoftProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @author Piotr Rugała <[email protected]>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace Tests\DivanteTranslationBundle\Provider;

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

final class MicrosoftProviderTest 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(MicrosoftProvider::class)
->onlyMethods(['getHttpClient'])
->getMock();
$provider->method('getHttpClient')->willReturn($client);
$provider->setApiKey('test');

return $provider;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* @author Piotr Rugała <[email protected]>
* @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co)
*/

declare(strict_types=1);

namespace Tests\DivanteTranslationBundle\Provider;

Expand Down

0 comments on commit 5810bbd

Please sign in to comment.