-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_translation.js
More file actions
148 lines (127 loc) · 6.89 KB
/
text_translation.js
File metadata and controls
148 lines (127 loc) · 6.89 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
const { Credentials, Translator } = require('@translated/lara');
/**
* Complete text translation examples for the Lara Node.js 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 translation options
* - Get available languages
*/
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);
try {
// Example 1: Basic single string translation
console.log("=== Basic Single String Translation ===");
const result1 = await lara.translate("Hello, world!", "en-US", "fr-FR");
console.log("Original: Hello, world!");
console.log("French: " + result1.translation + "\n");
// Example 2: Multiple strings translation
console.log("=== Multiple Strings Translation ===");
const texts = ["Hello", "How are you?", "Goodbye"];
const result2 = await lara.translate(texts, "en-US", "es-ES");
console.log("Original: [" + texts.join(", ") + "]");
console.log("Spanish: [" + result2.translation.join(", ") + "]\n");
// Example 3: TextBlocks translation (mixed translatable/non-translatable content)
console.log("=== TextBlocks Translation ===");
const textBlocks = [
{ text: "Adventure novels, mysteries, cookbooks—wait, who packed those?", translatable: true },
{ text: "<br>", translatable: false }, // Non-translatable HTML
{ text: "Suddenly, it doesn't feel so deserted after all.", translatable: true },
{ text: "<div class=\"separator\"></div>", translatable: false }, // Non-translatable HTML
{ text: "Every page you turn is a new journey, and the best part?", translatable: true }
];
const result3 = await lara.translate(textBlocks, "en-US", "it-IT");
console.log("Original TextBlocks: " + textBlocks.length + " blocks");
console.log("Translated blocks: " + result3.translation.length);
result3.translation.forEach((translation, i) => {
console.log("Block " + (i + 1) + ": " + translation.text);
});
// Example 4: Translation with instructions
console.log("=== Translation with Instructions ===");
const options1 = {
instructions: ["Be formal", "Use technical terminology"]
};
const result4 = await lara.translate("Could you send me the report by tomorrow morning?", "en-US", "de-DE", options1);
console.log("Original: Could you send me the report by tomorrow morning?");
console.log("German (formal): " + result4.translation + "\n");
// Example 5: Auto-detecting source language
console.log("=== Auto-detect Source Language ===");
const result5 = await lara.translate("Bonjour le monde!", null, "en-US");
console.log("Original: Bonjour le monde!");
console.log("Detected source: " + result5.sourceLanguage);
console.log("English: " + result5.translation + "\n");
// Example 6: Advanced options with comprehensive settings
console.log("=== Translation with Advanced Options ===");
const options2 = {
adaptTo: ["mem_1A2b3C4d5E6f7G8h9I0jKl", "mem_2XyZ9AbC8dEf7GhI6jKlMn"], // Replace with actual memory IDs
glossaries: ["gls_1A2b3C4d5E6f7G8h9I0jKl", "gls_2XyZ9AbC8dEf7GhI6jKlMn"], // Replace with actual glossary IDs
instructions: ["Be professional"],
style: "fluid",
contentType: "text/plain",
timeoutInMillis: 10000,
};
const result6 = await lara.translate("This is a comprehensive translation example", "en-US", "it-IT", options2);
console.log("Original: This is a comprehensive translation example");
console.log("Italian (with all options): " + result6.translation + "\n");
// Example 7: Profanities detection and handling options
console.log("=== Translation with Profanities Detection and Handling Options ===");
const profanityText = "Don't be such a tool.";
const detectResult = await lara.translate(profanityText, "en-US", "it-IT", {
profanitiesDetect: "source_target",
profanitiesHandling: "detect",
verbose: true
});
const hideResult = await lara.translate(profanityText, "en-US", "it-IT", {
profanitiesDetect: "target",
profanitiesHandling: "hide",
verbose: true
});
console.log("Original: " + profanityText);
console.log("Detect mode translation: " + detectResult.translation);
console.log("Hide mode translation: " + hideResult.translation);
// Example 8: Get available languages
console.log("=== Available Languages ===");
const languages = await lara.getLanguages();
console.log("Supported languages: [" + languages + "]");
// Example 9: Detect language of a given text
console.log("=== Language Detection ===");
const detectionResult1 = await lara.detect("¿Cómo estás?");
console.log("Text: ¿Cómo estás?");
console.log("Detected Language: " + detectionResult1.language + " with content type " + detectionResult1.contentType + "\n");
// Example 10: Detect language with hint and passlist
console.log("=== Language Detection with Hint and Passlist ===");
const detectionResult2 = await lara.detect("Ciao", "it", ["it", "fr"]);
console.log("Text: Ciao");
console.log("Detected Language: " + detectionResult2.language + " with content type " + detectionResult2.contentType + "\n");
// Example 11: Quality estimation for a single sentence pair
console.log("=== Quality Estimation: single sentence ===");
const qeSingle = await lara.qualityEstimation(
"en-US", "it-IT",
"Hello, how are you today?",
"Ciao, come stai oggi?"
);
console.log("Score: " + qeSingle.score + "\n");
// Example 12: Quality estimation for a batch of sentence pairs
console.log("=== Quality Estimation: batch ===");
const qeBatch = await lara.qualityEstimation(
"en-US", "it-IT",
["Good morning.", "The weather is nice."],
["Buongiorno.", "Il tempo è bello."]
);
console.log("Scores: " + qeBatch.map(r => r.score).join(", ") + "\n");
} catch (error) {
console.error("Error:", error.message);
}
}
main();