-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglossaries_management.php
More file actions
176 lines (148 loc) Β· 6.44 KB
/
glossaries_management.php
File metadata and controls
176 lines (148 loc) Β· 6.44 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
require_once __DIR__ . '/../vendor/autoload.php';
/**
* Complete glossary management examples for the Lara PHP SDK
*
* This example demonstrates:
* - Create, list, update, delete glossaries
* - CSV import with status monitoring
* - Glossary export
* - Glossary terms count
* - Import status checking
*/
use Lara\LaraCredentials;
use Lara\Translator;
use Lara\LaraException;
use Lara\LaraTimeoutException;
function main() {
// All examples use environment variables for credentials, so set them first:
// export LARA_ACCESS_KEY_ID="your-access-key-id"
// export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
// Get credentials from environment variables
$accessKeyId = getenv('LARA_ACCESS_KEY_ID');
$accessKeySecret = getenv('LARA_ACCESS_KEY_SECRET');
$credentials = new LaraCredentials($accessKeyId, $accessKeySecret);
$lara = new Translator($credentials);
echo "ποΈ Glossaries require a specific subscription plan.\n";
echo " If you encounter errors, please check your subscription level.\n\n";
// Example 1: Basic glossary management
echo "=== Basic Glossary Management ===\n";
try {
$glossary = $lara->glossaries->create("MyDemoGlossary");
echo "β
Created glossary: " . $glossary->getName() . " (ID: " . $glossary->getId() . ")\n";
// List all glossaries
$glossaries = $lara->glossaries->getAll();
echo "π Total glossaries: " . count($glossaries) . "\n";
echo "\n";
// Store the glossary ID for later examples
$glossaryId = $glossary->getId();
} catch (LaraException $e) {
echo "Error creating glossary: " . $e->getMessage() . "\n\n";
return;
}
// Example 2: Glossary operations
echo "=== Glossary Operations ===\n";
try {
// Get glossary details
$retrievedGlossary = $lara->glossaries->get($glossaryId);
if ($retrievedGlossary) {
echo "π Glossary: " . $retrievedGlossary->getName() . " (Owner: " . $retrievedGlossary->getOwnerId() . ")\n";
}
// Get glossary terms count
$counts = $lara->glossaries->counts($glossaryId);
if ($counts->getUnidirectional()) {
foreach ($counts->getUnidirectional() as $lang => $count) {
echo " $lang: $count entries\n";
}
}
// Update glossary
$updatedGlossary = $lara->glossaries->update($glossaryId, "UpdatedDemoGlossary");
echo "π Updated name: '" . $glossary->getName() . "' -> '" . $updatedGlossary->getName() . "'\n";
echo "\n";
} catch (LaraException $e) {
echo "Error with glossary operations: " . $e->getMessage() . "\n\n";
}
// Example 3: CSV import functionality
echo "=== CSV Import Functionality ===\n";
// Replace with your actual CSV file path
$csvFilePath = __DIR__ . '/sample_glossary.csv'; // Create this file with your glossary data
if (file_exists($csvFilePath)) {
try {
echo "Importing CSV file: " . basename($csvFilePath) . "\n";
$import = $lara->glossaries->importCsv($glossaryId, $csvFilePath);
echo "Import started with ID: " . $import->getId() . "\n";
echo "Initial progress: " . ($import->getProgress() * 100) . "%\n";
// Check import status manually
echo "Checking import status...\n";
$importStatus = $lara->glossaries->getImportStatus($import->getId());
echo "Current progress: " . ($importStatus->getProgress() * 100) . "%\n";
// Wait for import to complete
try {
$completedImport = $lara->glossaries->waitForImport($import, 10);
echo "β
Import completed!\n";
echo "Final progress: " . ($completedImport->getProgress() * 100) . "%\n";
} catch (LaraTimeoutException $e) {
echo "Import timeout: The import process took too long to complete.\n";
}
echo "\n";
} catch (LaraException $e) {
echo "Error with CSV import: " . $e->getMessage() . "\n\n";
}
} else {
echo "CSV file not found: $csvFilePath\n";
}
// Example 4: Export functionality
echo "=== Export Functionality ===\n";
try {
// Export as CSV table unidirectional format
echo "π€ Exporting as CSV table unidirectional...\n";
$csvUniData = $lara->glossaries->export($glossaryId, "csv/table-uni", "en-US");
echo "β
CSV unidirectional export successful (" . strlen($csvUniData) . " bytes)\n";
// Save sample exports to files - replace with your desired output paths
$exportFilePath = __DIR__ . '/exported_glossary.csv'; // Replace with actual path
file_put_contents($exportFilePath, $csvUniData);
echo "πΎ Sample export saved to: " . basename($exportFilePath) . "\n";
echo "\n";
} catch (LaraException $e) {
echo "Error with export: " . $e->getMessage() . "\n\n";
}
// Example 5: Glossary Terms Count
echo "=== Glossary Terms Count ===\n";
try {
// Get detailed counts
$counts = $lara->glossaries->counts($glossaryId);
echo "π Detailed glossary terms count:\n";
if ($counts->getUnidirectional()) {
echo " Unidirectional entries by language pair:\n";
foreach ($counts->getUnidirectional() as $langPair => $count) {
echo " $langPair: $count terms\n";
}
} else {
echo " No unidirectional entries found\n";
}
$totalEntries = 0;
if ($counts->getUnidirectional()) {
$totalEntries += array_sum($counts->getUnidirectional());
}
echo " Total entries: $totalEntries\n";
echo "\n";
} catch (LaraException $e) {
echo "Error getting glossary terms count: " . $e->getMessage() . "\n\n";
}
// Cleanup
echo "=== Cleanup ===\n";
try {
$deletedGlossary = $lara->glossaries->delete($glossaryId);
echo "ποΈ Deleted glossary: " . $deletedGlossary->getName() . "\n";
// Clean up export files - replace with actual cleanup if needed
$exportFilePath = __DIR__ . '/exported_glossary.csv';
if (file_exists($exportFilePath)) {
unlink($exportFilePath);
echo "ποΈ Cleaned up export file\n";
}
} catch (LaraException $e) {
echo "Error deleting glossary: " . $e->getMessage() . "\n";
}
echo "\nπ Glossary management examples completed!\n";
}
main();