-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_translation.php
More file actions
217 lines (194 loc) · 8.47 KB
/
text_translation.php
File metadata and controls
217 lines (194 loc) · 8.47 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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
/**
* Complete text translation examples for the Lara PHP 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
*/
use Lara\LaraCredentials;
use Lara\Translator;
use Lara\LaraException;
use Lara\TranslateOptions;
use Lara\TextBlock;
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);
// Example 1: Basic single string translation
echo "=== Basic Single String Translation ===\n";
try {
$result = $lara->translate("Hello, world!", "en-US", "fr-FR");
echo "Original: Hello, world!\n";
echo "French: " . $result->getTranslation() . "\n\n";
} catch (LaraException $e) {
echo "Error translating text: " . $e->getMessage() . "\n\n";
return;
}
// Example 2: Multiple strings translation
echo "=== Multiple Strings Translation ===\n";
try {
$result = $lara->translate(["Hello", "How are you?", "Goodbye"], "en-US", "es-ES");
echo "Original: [Hello, How are you?, Goodbye]\n";
echo "Spanish: [" . implode(", ", $result->getTranslation()) . "]\n\n";
} catch (LaraException $e) {
echo "Error translating multiple texts: " . $e->getMessage() . "\n\n";
return;
}
// Example 3: TextBlocks translation (mixed translatable/non-translatable content)
echo "=== TextBlocks Translation ===\n";
try {
$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),
];
$result = $lara->translate($textBlocks, "en-US", "it-IT");
$translations = $result->getTranslation();
echo "Original TextBlocks: " . count($textBlocks) . " blocks\n";
echo "Translated blocks: " . count($translations) . "\n";
foreach ($translations as $i => $translation) {
echo "Block " . ($i + 1) . ": " . $translation . "\n";
}
echo "\n";
} catch (LaraException $e) {
echo "Error with TextBlocks translation: " . $e->getMessage() . "\n\n";
return;
}
// Example 4: Translation with instructions
echo "=== Translation with Instructions ===\n";
try {
$options = new TranslateOptions([
'instructions' => ["Be formal", "Use technical terminology"]
]);
$result = $lara->translate("Could you send me the report by tomorrow morning?", "en-US", "de-DE", $options);
echo "Original: Could you send me the report by tomorrow morning?\n";
echo "German (formal): " . $result->getTranslation() . "\n\n";
} catch (LaraException $e) {
echo "Error with instructed translation: " . $e->getMessage() . "\n\n";
return;
}
// Example 5: Auto-detecting source language
echo "=== Auto-detect Source Language ===\n";
try {
$result = $lara->translate("Bonjour le monde!", null, "en-US");
echo "Original: Bonjour le monde!\n";
echo "Detected source: " . $result->getSourceLanguage() . "\n";
echo "English: " . $result->getTranslation() . "\n\n";
} catch (LaraException $e) {
echo "Error with auto-detection: " . $e->getMessage() . "\n\n";
return;
}
// Example 6: Advanced options with comprehensive settings
echo "=== Translation with Advanced Options ===\n";
try {
$options = new TranslateOptions([
'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,
]);
$result = $lara->translate("This is a comprehensive translation example", "en-US", "it-IT", $options);
echo "Original: This is a comprehensive translation example\n";
echo "Italian (with all options): " . $result->getTranslation() . "\n\n";
} catch (LaraException $e) {
echo "Error with advanced translation: " . $e->getMessage() . "\n\n";
return;
}
// Example 7: Profanity filter options
echo "=== Translation with Profanity Filter Options ===\n";
try {
$profanityText = "Don't be such a tool.";
$detectResult = $lara->translate($profanityText, "en-US", "it-IT", new TranslateOptions([
'profanityFilter' => 'detect',
'verbose' => true
]));
$hideResult = $lara->translate($profanityText, "en-US", "it-IT", new TranslateOptions([
'profanityFilter' => 'hide',
'verbose' => true
]));
$avoidResult = $lara->translate($profanityText, "en-US", "it-IT", new TranslateOptions([
'profanityFilter' => 'avoid',
'verbose' => true
]));
echo "Original: " . $profanityText . "\n";
echo "Detect mode translation: " . $detectResult->getTranslation() . "\n";
echo "Hide mode translation: " . $hideResult->getTranslation() . "\n";
echo "Avoid mode translation: " . $avoidResult->getTranslation() . "\n\n";
} catch (LaraException $e) {
echo "Error with profanity filter translation: " . $e->getMessage() . "\n\n";
return;
}
// Example 8: Get available languages
echo "=== Available Languages ===\n";
try {
$languages = $lara->getLanguages();
echo "Supported languages: [" . implode(", ", $languages) . "]\n";
} catch (LaraException $e) {
echo "Error getting languages: " . $e->getMessage() . "\n";
return;
}
// Example 9: Detect language of a given text
echo "=== Detect language ===\n";
try {
$detect_result_1 = $lara->detect("Hello, world!");
echo "Detected language for 'Hello, world!': " . $detect_result_1->getLanguage() . "\n";
} catch (LaraException $e) {
echo "Error detecting language: " . $e->getMessage() . "\n";
return;
}
// Example 10: Detect language of a given text with hint and passlist
echo "=== Detect language with hint and passlist ===\n";
try {
$detect_result_2 = $lara->detect("Hello, world!", "en", ["en", "fr"]);
echo "Detected language for 'Hello, world!': " . $detect_result_2->getLanguage() . "\n";
} catch (LaraException $e) {
echo "Error detecting language: " . $e->getMessage() . "\n";
return;
}
// Example 11: Quality estimation for a single sentence pair
echo "\n=== Quality Estimation: single sentence ===\n";
try {
$qeSingle = $lara->qualityEstimation(
"en-US",
"it-IT",
"Hello, how are you today?",
"Ciao, come stai oggi?"
);
echo "Score: " . $qeSingle->getScore() . "\n";
} catch (LaraException $e) {
echo "Error in quality estimation: " . $e->getMessage() . "\n";
return;
}
// Example 12: Quality estimation for a batch of sentence pairs
echo "\n=== Quality Estimation: batch ===\n";
try {
$qeBatch = $lara->qualityEstimation(
"en-US",
"it-IT",
["Good morning.", "The weather is nice."],
["Buongiorno.", "Il tempo è bello."]
);
$scores = array_map(function ($r) { return $r->getScore(); }, $qeBatch);
echo "Scores: [" . implode(", ", $scores) . "]\n";
} catch (LaraException $e) {
echo "Error in quality estimation: " . $e->getMessage() . "\n";
return;
}
}
main();