Repository navigation
Expand file tree
/
Copy pathDocumentTranslation.java
More file actions
187 lines (157 loc) · 8.84 KB
/
Copy pathDocumentTranslation.java
File metadata and controls
187 lines (157 loc) · 8.84 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
import com.translated.lara.Credentials;
import com.translated.lara.errors.LaraException;
import com.translated.lara.errors.S3Exception;
import com.translated.lara.translator.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Complete document translation examples for the Lara Java SDK
*
* This example demonstrates:
* - Basic document translation
* - Advanced options with memories and glossaries
* - Step-by-step document translation with status monitoring
* - Document translation with a styleguide and its change report
*/
public class DocumentTranslation {
public static void main(String[] args) throws InterruptedException {
// All examples can use environment variables for credentials:
// export LARA_ACCESS_KEY_ID="your-access-key-id"
// export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
// Set your credentials here
String accessKeyId = System.getenv("LARA_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("LARA_ACCESS_KEY_SECRET");
Credentials credentials = new Credentials(accessKeyId, accessKeySecret);
Translator lara = new Translator(credentials);
// Replace with your actual document file path
String sampleFilePath = "sample_document.docx"; // Create this file with your content
File sampleFile = new File(sampleFilePath);
if (!sampleFile.exists()) {
System.out.println("Please create a sample document file at: " + sampleFilePath);
System.out.println("Add some sample text content to translate.\n");
return;
}
String sourceLang = "en-US";
String targetLang = "de-DE";
if (args.length > 0) {
if (args.length == 1 && "styleguide".equals(args[0])) {
runStyleguideExample(lara, sampleFile, sourceLang, targetLang);
} else {
System.err.println("Usage: DocumentTranslation [styleguide]");
}
return;
}
// Example 1: Basic document translation
System.out.println("=== Basic Document Translation ===");
System.out.println("Translating document: " + sampleFile.getName() + " from " + sourceLang + " to " + targetLang);
try {
InputStream fileStream = lara.documents.translate(sampleFile, sourceLang, targetLang);
// Save translated document - replace with your desired output path
String outputPath = "sample_document_translated.docx";
Files.copy(fileStream, Paths.get(outputPath));
fileStream.close();
System.out.println("✅ Document translation completed");
System.out.println("📄 Translated document saved to: " + outputPath + "\n");
} catch (LaraException | S3Exception | InterruptedException | IOException e) {
System.out.println("Error translating document: " + e.getMessage() + "\n");
return;
}
// Example 2: Document translation with advanced options
System.out.println("=== Document Translation with Advanced Options ===");
try {
DocumentTranslateOptions options = new DocumentTranslateOptions()
.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory IDs
.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary IDs
.setStyle(TranslationStyle.FLUID)
.setNoTrace(false);
InputStream fileStream = lara.documents.translate(sampleFile, sourceLang, "fr-FR", options);
String outputPath = "advanced_document_translated.docx";
Files.copy(fileStream, Paths.get(outputPath));
fileStream.close();
System.out.println("✅ Advanced document translation completed");
System.out.println("📄 Translated document saved to: " + outputPath + "\n");
} catch (LaraException | S3Exception | InterruptedException | IOException e) {
System.out.println("Error in advanced translation: " + e.getMessage() + "\n");
return;
}
// Example 3: Step-by-step document translation
System.out.println("=== Step-by-Step Document Translation ===");
try {
// Upload document
DocumentUploadOptions uploadOptions = new DocumentUploadOptions()
.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory ID
.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary ID
.setStyle(TranslationStyle.CREATIVE);
System.out.println("Step 1: Uploading document...");
Document document = lara.documents.upload(sampleFile, sourceLang, "es-ES", uploadOptions);
System.out.println("Document uploaded with ID: " + document.getId());
System.out.println("Initial status: " + document.getStatus());
// Check status
System.out.println("\nStep 2: Checking status...");
Document status = lara.documents.status(document.getId());
System.out.println("Current status: " + status.getStatus());
// Download translated document
System.out.println("\nStep 3: Downloading would happen after translation completes...");
while(status.getStatus() != Document.Status.TRANSLATED) {
Thread.sleep(1000); // Wait for 1 second before checking status again
status = lara.documents.status(document.getId());
System.out.println("Current status: " + status.getStatus());
}
InputStream fileStream = lara.documents.download(document.getId());
String outputPath = "monitored_translated.docx";
Files.copy(fileStream, Paths.get(outputPath));
fileStream.close();
System.out.println("✅ Step-by-step translation completed");
} catch (LaraException | S3Exception | IOException e) {
System.out.println("Error in step-by-step process: " + e.getMessage() + "\n");
}
// Example 4: Document translation with a styleguide and its report
runStyleguideExample(lara, sampleFile, sourceLang, targetLang);
}
private static void runStyleguideExample(Translator lara, File sampleFile, String sourceLang, String targetLang)
throws InterruptedException {
String styleguideId = System.getenv("LARA_STYLEGUIDE_ID");
if (styleguideId == null || styleguideId.isEmpty()) {
System.out.println("Set LARA_STYLEGUIDE_ID to run the styleguide example.");
return;
}
System.out.println("=== Document Translation with a Styleguide ===");
try {
DocumentUploadOptions options = new DocumentUploadOptions()
.setStyleguideId(styleguideId)
.setStyleguideExplanationLanguage("en");
Document document = lara.documents.upload(sampleFile, sourceLang, targetLang, options);
long start = System.nanoTime();
while (document.getStatus() != Document.Status.TRANSLATED) {
if (document.getStatus() == Document.Status.ERROR || document.getStatus() == Document.Status.PAUSED) {
System.out.println("Document " + document.getId() + " is " + document.getStatus());
return;
}
if (System.nanoTime() - start >= TimeUnit.MINUTES.toNanos(15)) {
System.out.println("Timed out waiting for document " + document.getId());
return;
}
Thread.sleep(2000);
document = lara.documents.status(document.getId());
}
Map<Integer, DocumentStyleguideSegment> report = lara.documents.getStyleguideReport(document.getId());
for (Map.Entry<Integer, DocumentStyleguideSegment> entry : report.entrySet()) {
DocumentStyleguideSegment segment = entry.getValue();
DocumentStyleguideSegment.Results results = segment.getStyleguideResults();
if (results == null) {
System.out.println("Styleguide was not applied to segment " + entry.getKey());
continue;
}
for (StyleguideChange change : results.getChanges()) {
System.out.printf("Segment %d: %s -> %s (%s)%n", entry.getKey(),
change.getOriginalTranslation(), change.getRefinedTranslation(), change.getExplanation());
}
}
} catch (LaraException | S3Exception e) {
System.out.println("Error in styleguide translation: " + e.getMessage());
}
}
}