-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #47
- Loading branch information
Showing
54 changed files
with
2,192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Scn\DeeplApiConnector\DeeplClientFactory; | ||
use Scn\DeeplApiConnector\Enum\GlossarySubmissionEntryFormatEnum; | ||
use Scn\DeeplApiConnector\Model\GlossariesInterface; | ||
use Scn\DeeplApiConnector\Model\GlossaryEntries; | ||
use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; | ||
use Scn\DeeplApiConnector\Model\GlossaryInterface; | ||
use Scn\DeeplApiConnector\Model\GlossarySubmission; | ||
use Scn\DeeplApiConnector\Model\GlossariesSupportedLanguagesPairsInterface; | ||
use Scn\DeeplApiConnector\Model\TranslationConfig; | ||
|
||
$apiKey = 'your-api-key'; | ||
|
||
$deepl = DeeplClientFactory::create($apiKey); | ||
|
||
/** @var GlossariesSupportedLanguagesPairsInterface $result */ | ||
$result = $deepl->getGlossariesSupportedLanguagesPairs(); | ||
/** | ||
* List all available pairs | ||
*/ | ||
var_dump($result->getList()); | ||
|
||
/** @var GlossariesInterface $result */ | ||
$result = $deepl->getGlossariesList(); | ||
/** | ||
* List all glossaries | ||
*/ | ||
var_dump($result->getList()); | ||
|
||
$input = (new GlossarySubmission()) | ||
->setName('en => nl') | ||
->setSourceLang('en') | ||
->setTargetLang('nl') | ||
->setEntries("Hello\tDag\nDog\tHond"); | ||
|
||
// NB.: Note that entries can be a tsv or csv. Related to the documentation : https://www.deepl.com/fr/docs-api/glossaries/formats | ||
// To use CSV, this is the example : | ||
|
||
$input->setEntriesFormat(GlossarySubmissionEntryFormatEnum::FORMAT_CSV) | ||
->setEntries("Hello,Dag\nDog,Hond"); | ||
/** @var GlossaryInterface $glossary */ | ||
$glossary = $deepl->createGlossary($input); | ||
/** | ||
* Get created glossary | ||
*/ | ||
var_dump($glossary->getDetails()); | ||
|
||
$input = (new GlossaryIdSubmission()) | ||
->setId($glossary->getDetails()['glossary_id']); | ||
/** @var GlossaryInterface $result */ | ||
$result = $deepl->retrieveGlossary($input); | ||
/** | ||
* Get glossary details | ||
*/ | ||
var_dump($result->getDetails()); | ||
|
||
/** @var GlossaryEntries $result */ | ||
$result = $deepl->retrieveGlossaryEntries($input); | ||
/** | ||
* Get glossary entries array | ||
*/ | ||
var_dump($result->getList()); | ||
/** | ||
* Get glossary entries real result | ||
*/ | ||
var_dump($result->getResult()); | ||
|
||
$result = $deepl->deleteGlossary($input); | ||
/** | ||
* True if glossary successfully deleted | ||
*/ | ||
var_dump($result); | ||
|
||
/** | ||
* Now, whe can get the glossary from the source and target lang and use it in the TranslationConfig | ||
*/ | ||
$source = 'en'; | ||
$target = 'nl'; | ||
/** @var GlossariesInterface $glossaries */ | ||
$glossaries = $deepl->getGlossariesList(); | ||
$glossary = current(array_filter( | ||
$glossaries->getList(), | ||
fn (array $e) => $e['source_lang'] === $source && $e['target_lang'] === $target | ||
)); | ||
|
||
$translationConfig = new TranslationConfig( | ||
'Hello World', | ||
$target, | ||
$source, | ||
glossaryId: $glossary !== false ? $glossary['glossary_id'] : '' | ||
); | ||
$translationObj = $deepl->getTranslation($translationConfig); | ||
var_dump($translationObj); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scn\DeeplApiConnector\Enum; | ||
|
||
final class GlossarySubmissionEntryFormatEnum | ||
{ | ||
public const FORMAT_TSV = 'tsv'; | ||
public const FORMAT_CSV = 'csv'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scn\DeeplApiConnector\Handler; | ||
|
||
abstract class AbstractDeeplHandler implements DeeplRequestHandlerInterface | ||
{ | ||
public function getAuthHeader(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getAcceptHeader(): ?string | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Handler/DeeplGlossariesListRetrievalRequestHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scn\DeeplApiConnector\Handler; | ||
|
||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
final class DeeplGlossariesListRetrievalRequestHandler extends AbstractDeeplHandler | ||
{ | ||
public const API_ENDPOINT = '/v2/glossaries'; | ||
|
||
private string $authKey; | ||
|
||
private StreamFactoryInterface $streamFactory; | ||
|
||
public function __construct( | ||
string $authKey, | ||
StreamFactoryInterface $streamFactory | ||
) { | ||
$this->authKey = $authKey; | ||
$this->streamFactory = $streamFactory; | ||
} | ||
|
||
public function getMethod(): string | ||
{ | ||
return DeeplRequestHandlerInterface::METHOD_GET; | ||
} | ||
|
||
public function getPath(): string | ||
{ | ||
return static::API_ENDPOINT; | ||
} | ||
|
||
public function getBody(): StreamInterface | ||
{ | ||
return $this->streamFactory->createStream(); | ||
} | ||
|
||
public function getAuthHeader(): ?string | ||
{ | ||
return sprintf('DeepL-Auth-Key %s', $this->authKey); | ||
} | ||
|
||
public function getContentType(): string | ||
{ | ||
return 'application/x-www-form-urlencoded'; | ||
} | ||
} |
Oops, something went wrong.