-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglossaries_management.js
More file actions
223 lines (191 loc) Β· 9.79 KB
/
glossaries_management.js
File metadata and controls
223 lines (191 loc) Β· 9.79 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const { Credentials, Translator } = require("lara-sdk");
const fs = require("fs");
const path = require("path");
/**
* Complete glossary management examples for the Lara Node.js SDK
*
* This example demonstrates:
* - Create, list, update, delete glossaries
* - CSV import with status monitoring
* - Glossary export
* - Glossary terms count
* - Import status checking
* - Add or replace glossary entries
* - Delete glossary entries
*/
async 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"
// Set your credentials here
const accessKeyId = process.env.LARA_ACCESS_KEY_ID;
const accessKeySecret = process.env.LARA_ACCESS_KEY_SECRET;
const credentials = new Credentials(accessKeyId, accessKeySecret);
const lara = new Translator(credentials);
console.log("ποΈ Glossaries require a specific subscription plan.");
console.log(" If you encounter errors, please check your subscription level.\n");
let glossaryId = null;
try {
// Example 1: Basic glossary management
console.log("=== Basic Glossary Management ===");
const glossary = await lara.glossaries.create("MyDemoGlossary");
console.log(`β
Created glossary: ${glossary.name} (ID: ${glossary.id})`);
glossaryId = glossary.id;
// List all glossaries
const glossaries = await lara.glossaries.list();
console.log(`π Total glossaries: ${glossaries.length}`);
console.log();
// Example 2: Glossary operations
console.log("=== Glossary Operations ===");
// Get glossary details
const retrievedGlossary = await lara.glossaries.get(glossaryId);
if (retrievedGlossary) {
console.log(`π Glossary: ${retrievedGlossary.name} (Owner: ${retrievedGlossary.ownerId})`);
}
// Get glossary terms count
const counts = await lara.glossaries.counts(glossaryId);
if (counts.unidirectional) {
for (const [lang, count] of Object.entries(counts.unidirectional)) {
console.log(` ${lang}: ${count} entries`);
}
}
// Update glossary
const updatedGlossary = await lara.glossaries.update(glossaryId, "UpdatedDemoGlossary");
console.log(`π Updated name: '${glossary.name}' -> '${updatedGlossary.name}'`);
// Example 3: CSV import functionality
console.log("=== CSV Import Functionality ===");
// Replace with your actual CSV file path
const csvFilePath = path.join(__dirname, "sample_glossary.csv"); // Create this file with your glossary data
if (fs.existsSync(csvFilePath)) {
console.log(`Importing CSV file: ${path.basename(csvFilePath)}`);
const csvImport = await lara.glossaries.importCsv(glossaryId, csvFilePath);
console.log(`Import started with ID: ${csvImport.id}`);
console.log(`Initial progress: ${Math.round(csvImport.progress * 100)}%`);
// Check import status manually
console.log("Checking import status...");
const importStatus = await lara.glossaries.getImportStatus(csvImport.id);
console.log(`Current progress: ${Math.round(importStatus.progress * 100)}%`);
// Wait for import to complete
try {
const completedImport = await lara.glossaries.waitForImport(csvImport, 10);
console.log("β
Import completed!");
console.log(`Final progress: ${Math.round(completedImport.progress * 100)}%`);
} catch (error) {
console.log("Import timeout: The import process took too long to complete.");
}
console.log();
} else {
console.log(`CSV file not found: ${csvFilePath}`);
}
// Example 4: Export functionality
console.log("=== Export Functionality ===");
try {
// Export as CSV table unidirectional format
console.log("π€ Exporting as CSV table unidirectional...");
const csvUniData = await lara.glossaries.export(glossaryId, "csv/table-uni", "en-US");
console.log(`β
CSV unidirectional export successful (${csvUniData.length} bytes)`);
// Save sample exports to files - replace with your desired output paths
const exportFilePath = path.join(__dirname, "exported_glossary.csv"); // Replace with actual path
fs.writeFileSync(exportFilePath, csvUniData);
console.log(`πΎ Sample export saved to: ${path.basename(exportFilePath)}`);
console.log();
} catch (error) {
console.log(`Error with export: ${error.message}\n`);
}
// Example 5: Glossary Terms Count
console.log("=== Glossary Terms Count ===");
try {
// Get detailed counts
const detailedCounts = await lara.glossaries.counts(glossaryId);
console.log("π Detailed glossary terms count:");
if (detailedCounts.unidirectional) {
console.log(" Unidirectional entries by language pair:");
for (const [langPair, count] of Object.entries(detailedCounts.unidirectional)) {
console.log(` ${langPair}: ${count} terms`);
}
} else {
console.log(" No unidirectional entries found");
}
let totalEntries = 0;
if (detailedCounts.unidirectional) {
totalEntries += Object.values(detailedCounts.unidirectional).reduce((sum, count) => sum + count, 0);
}
console.log(` Total entries: ${totalEntries}`);
console.log();
} catch (error) {
console.log(`Error getting glossary terms count: ${error.message}\n`);
}
// Example 6: Add or replace glossary entries
console.log("=== Add or Replace Glossary Entries ===");
try {
// Add a new entry with multiple language terms
const terms = [
{ language: "en-US", value: "computer" },
{ language: "it-IT", value: "computer" }
];
const addResult = await lara.glossaries.addOrReplaceEntry(glossaryId, terms);
console.log(`β
Entry added/replaced (import ID: ${addResult.id})`);
// Wait for the import to complete
const completedAdd = await lara.glossaries.waitForImport(addResult);
console.log(` Import progress: ${Math.round(completedAdd.progress * 100)}%`);
// Add another entry with a custom GUID
const termsWithGuid = [
{ language: "en-US", value: "keyboard" },
{ language: "it-IT", value: "tastiera" }
];
const addWithGuidResult = await lara.glossaries.addOrReplaceEntry(glossaryId, termsWithGuid, "custom-guid-123");
console.log(`β
Entry added with GUID (import ID: ${addWithGuidResult.id})`);
await lara.glossaries.waitForImport(addWithGuidResult);
// Replace an existing entry by using the same GUID
const updatedTerms = [
{ language: "en-US", value: "keyboard" },
{ language: "it-IT", value: "tastiera" },
{ language: "fr-FR", value: "clavier" }
];
const replaceResult = await lara.glossaries.addOrReplaceEntry(glossaryId, updatedTerms, "custom-guid-123");
console.log(`β
Entry replaced with updated terms (import ID: ${replaceResult.id})`);
await lara.glossaries.waitForImport(replaceResult);
console.log();
} catch (error) {
console.log(`Error adding/replacing entry: ${error.message}\n`);
}
// Example 7: Delete glossary entries
console.log("=== Delete Glossary Entries ===");
try {
// Delete an entry by GUID
const deleteByGuidResult = await lara.glossaries.deleteEntry(glossaryId, undefined, "custom-guid-123");
console.log(`β
Entry deleted by GUID (import ID: ${deleteByGuidResult.id})`);
await lara.glossaries.waitForImport(deleteByGuidResult);
// Delete an entry by term
const term = { language: "en-US", value: "computer" };
const deleteByTermResult = await lara.glossaries.deleteEntry(glossaryId, term);
console.log(`β
Entry deleted by term: ${term.language} -> "${term.value}" (import ID: ${deleteByTermResult.id})`);
await lara.glossaries.waitForImport(deleteByTermResult);
console.log();
} catch (error) {
console.log(`Error deleting entry: ${error.message}\n`);
}
} catch (error) {
console.log(`Error creating glossary: ${error.message}\n`);
return;
} finally {
// Cleanup
console.log("=== Cleanup ===");
if (glossaryId) {
try {
const deletedGlossary = await lara.glossaries.delete(glossaryId);
console.log(`ποΈ Deleted glossary: ${deletedGlossary.name}`);
// Clean up export files - replace with actual cleanup if needed
const exportFilePath = path.join(__dirname, "exported_glossary.csv");
if (fs.existsSync(exportFilePath)) {
fs.unlinkSync(exportFilePath);
console.log("ποΈ Cleaned up export file");
}
} catch (error) {
console.log(`Error deleting glossary: ${error.message}`);
}
}
}
console.log("\nπ Glossary management examples completed!");
}
main().catch(console.error);