0
# V2 Legacy API
1
2
The V2 API provides simple REST-based translation functionality with basic features for text translation, language detection, and supported language listing. This is the legacy API and users are encouraged to migrate to V3 for access to advanced features.
3
4
## Core Import
5
6
```python
7
from google.cloud import translate_v2 as translate
8
```
9
10
## Client Class
11
12
```python { .api }
13
class Client:
14
"""
15
V2 Translation API client.
16
17
Args:
18
target_language (str, optional): Default target language for translations. Defaults to 'en'.
19
credentials: OAuth2 Credentials for authentication
20
_http: HTTP session object for requests
21
client_info: Client info for user-agent string
22
client_options: Client options including API endpoint
23
"""
24
def __init__(
25
self,
26
target_language="en",
27
credentials=None,
28
_http=None,
29
client_info=None,
30
client_options=None,
31
): ...
32
```
33
34
## Capabilities
35
36
### Text Translation
37
38
Translate text strings from one language to another with optional source language detection and custom models.
39
40
```python { .api }
41
def translate(
42
self,
43
values,
44
target_language=None,
45
format_=None,
46
source_language=None,
47
customization_ids=(),
48
model=None
49
):
50
"""
51
Translate a string or list of strings.
52
53
Args:
54
values (str or list): Text string(s) to translate
55
target_language (str, optional): Target language code (ISO 639-1)
56
format_ (str, optional): Format of input text ('text' or 'html')
57
source_language (str, optional): Source language code for translation
58
customization_ids (tuple, optional): Custom model IDs to use
59
model (str, optional): Translation model ('base' or 'nmt')
60
61
Returns:
62
dict or list: Translation results with translated text and metadata
63
"""
64
```
65
66
### Language Detection
67
68
Detect the language of input text strings with confidence scores.
69
70
```python { .api }
71
def detect_language(self, values):
72
"""
73
Detect the language of a string or list of strings.
74
75
Args:
76
values (str or list): Text string(s) to analyze
77
78
Returns:
79
dict or list: Detection results with language codes and confidence
80
"""
81
```
82
83
### Supported Languages
84
85
Get a list of all languages supported by the Translation API.
86
87
```python { .api }
88
def get_languages(self, target_language=None):
89
"""
90
Get list of supported languages for translation.
91
92
Args:
93
target_language (str, optional): Language code for language names
94
95
Returns:
96
list: List of supported language objects with codes and names
97
"""
98
```
99
100
## Constants
101
102
```python { .api }
103
ENGLISH_ISO_639 = "en" # ISO 639-1 language code for English
104
BASE = "base" # Base translation model
105
NMT = "nmt" # Neural Machine Translation model
106
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",) # OAuth2 scopes
107
```
108
109
## Usage Examples
110
111
### Basic Translation
112
113
```python
114
from google.cloud import translate_v2 as translate
115
116
client = translate.Client()
117
118
# Translate a single string
119
result = client.translate(
120
"Hello, world!",
121
target_language="es"
122
)
123
124
print(f"Original: {result['input']}")
125
print(f"Translation: {result['translatedText']}")
126
print(f"Detected language: {result['detectedSourceLanguage']}")
127
```
128
129
### Multiple Text Translation
130
131
```python
132
from google.cloud import translate_v2 as translate
133
134
client = translate.Client()
135
136
# Translate multiple strings
137
texts = ["Hello", "How are you?", "Goodbye"]
138
results = client.translate(
139
texts,
140
target_language="fr",
141
source_language="en"
142
)
143
144
for result in results:
145
print(f"{result['input']} -> {result['translatedText']}")
146
```
147
148
### Language Detection
149
150
```python
151
from google.cloud import translate_v2 as translate
152
153
client = translate.Client()
154
155
# Detect language of text
156
result = client.detect_language("Bonjour le monde")
157
158
print(f"Language: {result['language']}")
159
print(f"Confidence: {result['confidence']}")
160
```
161
162
### List Supported Languages
163
164
```python
165
from google.cloud import translate_v2 as translate
166
167
client = translate.Client()
168
169
# Get all supported languages
170
languages = client.get_languages()
171
172
for language in languages:
173
print(f"{language['language']}: {language['name']}")
174
175
# Get language names in Spanish
176
languages_es = client.get_languages(target_language="es")
177
178
for language in languages_es:
179
print(f"{language['language']}: {language['name']}")
180
```
181
182
## Response Format
183
184
### Translation Response
185
186
```python
187
{
188
'translatedText': 'Hola, mundo!',
189
'detectedSourceLanguage': 'en',
190
'input': 'Hello, world!'
191
}
192
```
193
194
### Detection Response
195
196
```python
197
{
198
'language': 'fr',
199
'confidence': 1.0,
200
'input': 'Bonjour le monde'
201
}
202
```
203
204
### Languages Response
205
206
```python
207
[
208
{'language': 'en', 'name': 'English'},
209
{'language': 'es', 'name': 'Spanish'},
210
{'language': 'fr', 'name': 'French'},
211
...
212
]
213
```