diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 3f39311..b8b1992 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -1,5 +1,5 @@ name: Analysis -on: [push, pull_request] +on: [push] jobs: php-cs-fixer: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f468b83..630af98 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,5 +1,5 @@ name: Tests -on: [push, pull_request] +on: [push] jobs: build-test: diff --git a/readme.md b/readme.md index b4628f7..4752035 100644 --- a/readme.md +++ b/readme.md @@ -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) @@ -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: diff --git a/src/DivanteTranslationBundle/Provider/MicrosoftProvider.php b/src/DivanteTranslationBundle/Provider/MicrosoftProvider.php new file mode 100644 index 0000000..c3f06ce --- /dev/null +++ b/src/DivanteTranslationBundle/Provider/MicrosoftProvider.php @@ -0,0 +1,49 @@ + + * @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'; + } +} diff --git a/tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php b/tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php index a6b8356..50c7582 100644 --- a/tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php +++ b/tests/DivanteTranslationBundle/Provider/DeeplProviderTest.php @@ -1,4 +1,8 @@ + * @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) + */ declare(strict_types=1); diff --git a/tests/DivanteTranslationBundle/Provider/GoogleProviderTest.php b/tests/DivanteTranslationBundle/Provider/GoogleProviderTest.php index 3872778..4616988 100644 --- a/tests/DivanteTranslationBundle/Provider/GoogleProviderTest.php +++ b/tests/DivanteTranslationBundle/Provider/GoogleProviderTest.php @@ -1,4 +1,8 @@ + * @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) + */ declare(strict_types=1); diff --git a/tests/DivanteTranslationBundle/Provider/MicrosoftProviderTest.php b/tests/DivanteTranslationBundle/Provider/MicrosoftProviderTest.php new file mode 100644 index 0000000..b77d33e --- /dev/null +++ b/tests/DivanteTranslationBundle/Provider/MicrosoftProviderTest.php @@ -0,0 +1,51 @@ + + * @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; + } +} diff --git a/tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php b/tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php index 2c6617b..7f3c152 100644 --- a/tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php +++ b/tests/DivanteTranslationBundle/Provider/ProviderFactoryTest.php @@ -1,4 +1,10 @@ + * @copyright Copyright (c) 2021 Divante Ltd. (https://divante.co) + */ + +declare(strict_types=1); namespace Tests\DivanteTranslationBundle\Provider;