0
# Text Translation
1
2
Core text translation functionality supporting 20+ languages with automatic language detection, batch processing, and customizable translation parameters. The text translation service provides high-quality machine translation with enterprise-grade reliability.
3
4
## Capabilities
5
6
### Single Text Translation
7
8
Translates a single text string from source language to target language with optional untranslated text preservation.
9
10
```python { .api }
11
def TextTranslate(self, request: models.TextTranslateRequest) -> models.TextTranslateResponse:
12
"""
13
Translate text from source language to target language.
14
15
Args:
16
request: TextTranslateRequest with translation parameters
17
18
Returns:
19
TextTranslateResponse with translated text and metadata
20
21
Raises:
22
TencentCloudSDKException: For various error conditions
23
"""
24
```
25
26
**Usage Example:**
27
28
```python
29
from tencentcloud.common import credential
30
from tencentcloud.tmt.v20180321.tmt_client import TmtClient
31
from tencentcloud.tmt.v20180321 import models
32
33
# Initialize client
34
cred = credential.Credential("SecretId", "SecretKey")
35
client = TmtClient(cred, "ap-beijing")
36
37
# Create translation request
38
req = models.TextTranslateRequest()
39
req.SourceText = "Hello, world!"
40
req.Source = "en"
41
req.Target = "zh"
42
req.ProjectId = 0
43
44
# Perform translation
45
resp = client.TextTranslate(req)
46
print(resp.TargetText) # 你好,世界!
47
print(resp.Source) # en (detected source)
48
print(resp.Target) # zh (target language)
49
```
50
51
### Batch Text Translation
52
53
Translates multiple text strings in a single request for improved efficiency and throughput.
54
55
```python { .api }
56
def TextTranslateBatch(self, request: models.TextTranslateBatchRequest) -> models.TextTranslateBatchResponse:
57
"""
58
Translate multiple text strings in batch.
59
60
Args:
61
request: TextTranslateBatchRequest with batch parameters
62
63
Returns:
64
TextTranslateBatchResponse with list of translated texts
65
66
Raises:
67
TencentCloudSDKException: For various error conditions
68
"""
69
```
70
71
**Usage Example:**
72
73
```python
74
# Create batch translation request
75
req = models.TextTranslateBatchRequest()
76
req.Source = "en"
77
req.Target = "zh"
78
req.ProjectId = 0
79
req.SourceTextList = [
80
"Hello, world!",
81
"How are you?",
82
"Thank you very much."
83
]
84
# Optional: Use terminology and sentence databases
85
# req.TermRepoIDList = ["term_repo_id_1", "term_repo_id_2"]
86
# req.SentRepoIDList = ["sent_repo_id_1"]
87
88
# Perform batch translation
89
resp = client.TextTranslateBatch(req)
90
for translated_text in resp.TargetTextList:
91
print(translated_text)
92
print(f"Characters used: {resp.UsedAmount}")
93
```
94
95
### Language Detection
96
97
Automatically detects the language of input text without requiring explicit source language specification.
98
99
```python { .api }
100
def LanguageDetect(self, request: models.LanguageDetectRequest) -> models.LanguageDetectResponse:
101
"""
102
Detect the language of input text.
103
104
Args:
105
request: LanguageDetectRequest with text to analyze
106
107
Returns:
108
LanguageDetectResponse with detected language code
109
110
Raises:
111
TencentCloudSDKException: For various error conditions
112
"""
113
```
114
115
**Usage Example:**
116
117
```python
118
# Create language detection request
119
req = models.LanguageDetectRequest()
120
req.Text = "Bonjour le monde"
121
req.ProjectId = 0
122
123
# Detect language
124
resp = client.LanguageDetect(req)
125
print(resp.Lang) # fr (French detected)
126
```
127
128
## Request/Response Models
129
130
### TextTranslateRequest
131
132
```python { .api }
133
class TextTranslateRequest:
134
"""
135
Request parameters for text translation.
136
137
Attributes:
138
SourceText (str): Text to translate (max 6000 characters)
139
Source (str): Source language code
140
Target (str): Target language code
141
ProjectId (int): Project ID (default: 0)
142
UntranslatedText (str): Text segments to keep untranslated
143
"""
144
```
145
146
### TextTranslateResponse
147
148
```python { .api }
149
class TextTranslateResponse:
150
"""
151
Response from text translation.
152
153
Attributes:
154
TargetText (str): Translated text result
155
Source (str): Detected/specified source language
156
Target (str): Target language
157
UsedAmount (int): Characters consumed for billing
158
RequestId (str): Unique request identifier
159
"""
160
```
161
162
### TextTranslateBatchRequest
163
164
```python { .api }
165
class TextTranslateBatchRequest:
166
"""
167
Request parameters for batch text translation.
168
169
Attributes:
170
Source (str): Source language code
171
Target (str): Target language code
172
ProjectId (int): Project ID (default: 0)
173
SourceTextList (list[str]): List of texts to translate
174
TermRepoIDList (list[str]): List of terminology databases to use
175
SentRepoIDList (list[str]): List of sentence databases to use
176
"""
177
```
178
179
### TextTranslateBatchResponse
180
181
```python { .api }
182
class TextTranslateBatchResponse:
183
"""
184
Response from batch text translation.
185
186
Attributes:
187
Source (str): Source language
188
Target (str): Target language
189
TargetTextList (list[str]): List of translated texts
190
UsedAmount (int): Characters consumed for billing
191
RequestId (str): Unique request identifier
192
"""
193
```
194
195
### LanguageDetectRequest
196
197
```python { .api }
198
class LanguageDetectRequest:
199
"""
200
Request parameters for language detection.
201
202
Attributes:
203
Text (str): Text to analyze for language detection
204
ProjectId (int): Project ID (default: 0)
205
"""
206
```
207
208
### LanguageDetectResponse
209
210
```python { .api }
211
class LanguageDetectResponse:
212
"""
213
Response from language detection.
214
215
Attributes:
216
Lang (str): Detected language code
217
RequestId (str): Unique request identifier
218
"""
219
```
220
221
## Supported Languages
222
223
Text translation supports extensive language combinations:
224
225
- **Chinese variants**: zh (Simplified), zh-TW (Traditional), zh-HK (Hong Kong), zh-TR (Traditional)
226
- **Major languages**: en (English), ja (Japanese), ko (Korean)
227
- **European languages**: fr (French), de (German), es (Spanish), it (Italian), pt (Portuguese), ru (Russian)
228
- **Other languages**: ar (Arabic), tr (Turkish), vi (Vietnamese), th (Thai), ms (Malay), id (Indonesian), hi (Hindi)
229
230
## Error Handling
231
232
Common error scenarios for text translation:
233
234
- **UNSUPPORTEDOPERATION_TEXTTOOLONG**: Text exceeds 6000 character limit
235
- **UNSUPPORTEDOPERATION_UNSUPPORTEDLANGUAGE**: Language pair not supported
236
- **FAILEDOPERATION_NOFREEAMOUNT**: Translation quota exhausted
237
- **INVALIDPARAMETER**: Invalid request parameters
238
239
Always wrap translation calls in try-catch blocks to handle TencentCloudSDKException.