-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathuse_glossaries.php
98 lines (83 loc) · 2.72 KB
/
use_glossaries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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);