-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextTranslation.java
More file actions
198 lines (171 loc) · 9.23 KB
/
TextTranslation.java
File metadata and controls
198 lines (171 loc) · 9.23 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
import com.translated.lara.Credentials;
import com.translated.lara.errors.LaraException;
import com.translated.lara.translator.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
/**
* Complete text translation examples for the Lara Java SDK
*
* This example demonstrates:
* - Single string translation
* - Multiple strings translation
* - Translation with instructions
* - TextBlocks translation (mixed translatable/non-translatable content)
* - Auto-detect source language
* - Advanced options
* - Get available languages
*/
public class TextTranslation {
public static void main(String[] args) {
// Set your credentials here
String accessKeyId = "your-access-key-id";
String accessKeySecret = "your-access-key-secret";
Credentials credentials = new Credentials(accessKeyId, accessKeySecret);
Translator lara = new Translator(credentials);
// Example 1: Basic single string translation
System.out.println("=== Basic Single String Translation ===");
try {
TextResult result = lara.translate("Hello, world!", "en-US", "fr-FR");
System.out.println("Original: Hello, world!");
System.out.println("French: " + result.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error translating text: " + e.getMessage() + "\n");
return;
}
// Example 2: Multiple strings translation
System.out.println("=== Multiple Strings Translation ===");
try {
String[] texts = {"Hello", "How are you?", "Goodbye"};
TextResult result = lara.translate(texts, "en-US", "es-ES");
System.out.println("Original: " + Arrays.toString(texts));
System.out.println("Spanish: " + result.getTranslations() + "\n");
} catch (LaraException e) {
System.out.println("Error translating multiple texts: " + e.getMessage() + "\n");
return;
}
// Example 3: TextBlocks translation (mixed translatable/non-translatable content)
System.out.println("=== TextBlocks Translation ===");
try {
TextBlock[] textBlocks = {
new TextBlock("Adventure novels, mysteries, cookbooks—wait, who packed those?", true),
new TextBlock("<br>", false), // Non-translatable HTML
new TextBlock("Suddenly, it doesn't feel so deserted after all.", true),
new TextBlock("<div class=\"separator\"></div>", false), // Non-translatable HTML
new TextBlock("Every page you turn is a new journey, and the best part?", true)
};
TextResult result = lara.translateBlocks(textBlocks, "en-US", "it-IT");
List<TextBlock> translations = result.getTranslationBlocks();
System.out.println("Original TextBlocks: " + textBlocks.length + " blocks");
System.out.println("Translated blocks: " + translations.size());
for (int i = 0; i < translations.size(); i++) {
System.out.println("Block " + (i + 1) + ": " + translations.get(i).getText());
}
System.out.println();
} catch (LaraException e) {
System.out.println("Error with TextBlocks translation: " + e.getMessage() + "\n");
return;
}
// Example 4: Translation with instructions
System.out.println("=== Translation with Instructions ===");
try {
TranslateOptions options = new TranslateOptions()
.setInstructions("Be formal", "Use technical terminology");
TextResult result = lara.translate("Could you send me the report by tomorrow morning?", "en-US", "de-DE", options);
System.out.println("Original: Could you send me the report by tomorrow morning?");
System.out.println("German (formal): " + result.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error with instructed translation: " + e.getMessage() + "\n");
return;
}
// Example 5: Auto-detecting source language
System.out.println("=== Auto-detect Source Language ===");
try {
TextResult result = lara.translate("Bonjour le monde!", null, "en-US");
System.out.println("Original: Bonjour le monde!");
System.out.println("Detected source: " + result.getSourceLanguage());
System.out.println("English: " + result.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error with auto-detection: " + e.getMessage() + "\n");
return;
}
// Example 6: Advanced options with comprehensive settings
System.out.println("=== Translation with Advanced Options ===");
try {
TranslateOptions options = new TranslateOptions()
.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl", "mem_2XyZ9AbC8dEf7GhI6jKlMn") // Replace with actual memory IDs
.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl", "gls_2XyZ9AbC8dEf7GhI6jKlMn") // Replace with actual glossary IDs
.setInstructions("Be professional")
.setStyle(TranslationStyle.FLUID)
.setContentType("text/plain")
.setTimeoutMs(10000L)
TextResult result = lara.translate("This is a comprehensive translation example", "en-US", "it-IT", options);
System.out.println("Original: This is a comprehensive translation example");
System.out.println("Italian (with all options): " + result.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error with advanced translation: " + e.getMessage() + "\n");
return;
}
// Example 7: Translation with reasoning
System.out.println("=== Translation with Reasoning ===");
try {
TranslateOptions reasoningOptions = new TranslateOptions()
.setReasoning(true);
System.out.println("Original: Wonderful cavernous interior in a central but quiet and private area!");
System.out.println("Streaming partial results:");
Consumer<TextResult> streamingCallback = partialResult -> {
System.out.println("Partial result: " + partialResult.getTranslation());
};
TextResult finalResult = lara.translate("Wonderful cavernous interior in a central but quiet and private area!", "en-US", "it-IT", reasoningOptions, streamingCallback);
System.out.println("Final result: " + finalResult.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error with reasoning translation: " + e.getMessage() + "\n");
return;
}
// Example 8: Get available languages
System.out.println("=== Available Languages ===");
try {
List<String> languages = lara.getLanguages();
System.out.print("Supported languages: [" + languages + "]");
} catch (LaraException e) {
System.out.println("Error getting languages: " + e.getMessage());
return;
}
// Example 9: Detect language of a given text
System.out.println("=== Language Detection ===");
try {
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?");
System.out.println("Text: Hola, ¿cómo estás?");
System.out.println("Detected Language: " + detectResult.getLanguage());
} catch (LaraException e) {
System.out.println("Error detecting language: " + e.getMessage());
return;
}
// Example 10: Detect languages with hint and passlist
System.out.println("=== Language Detection with Hint and Passlist ===");
try {
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?", "es", new String[]{"es", "pt", "it"});
System.out.println("Text: Hola, ¿cómo estás?");
System.out.println("Detected Language: " + detectResult.getLanguage());
} catch (LaraException e) {
System.out.println("Error detecting language with hint/passlist: " + e.getMessage());
return;
}
// Example 10: Translation with reasoning
System.out.println("=== Translation with Reasoning ===");
try {
TranslateOptions reasoningOptions = new TranslateOptions()
.setReasoning(true);
System.out.println("Original: Wonderful cavernous interior in a central but quiet and private area!");
System.out.println("Streaming partial results:");
Consumer<TextResult> streamingCallback = partialResult -> {
System.out.println("Partial result: " + partialResult.getTranslation());
};
TextResult finalResult = lara.translate("Wonderful cavernous interior in a central but quiet and private area!", "en-US", "it-IT", reasoningOptions, streamingCallback);
System.out.println("Final result: " + finalResult.getTranslation() + "\n");
} catch (LaraException e) {
System.out.println("Error with reasoning translation: " + e.getMessage() + "\n");
return;
}
}
}