Translate Client#
Client for interacting with the Google Cloud Translate API.
-
class
google.cloud.translate.client.
Client
(api_key, http=None, target_language='en')[source]# Bases:
object
Client to bundle configuration needed for API requests.
Parameters: - api_key (str) – The key used to send with requests as a query parameter.
- http (
httplib2.Http
or class that definesrequest()
.) – (Optional) HTTP object to make requests. If not passed, anhttplib.Http
object is created. - target_language (str) – (Optional) The target language used for
translations and language names. (Defaults to
ENGLISH_ISO_639
.)
-
detect_language
(values)[source]# Detect the language of a string or list of strings.
See: https://cloud.google.com/translate/v2/ detecting-language-with-rest
Parameters: values (str or list) – String or list of strings that will have language detected. Return type: str or list Returns: A list of dictionaries for each queried value. Each dictionary typically contains three keys confidence
: The confidence in language detection, a float between 0 and 1.input
: The corresponding input value.language
: The detected language (as an ISO 639-1 language code).
though the key
confidence
may not always be present.If only a single value is passed, then only a single dictionary will be returned.
Raises: ValueError
if the number of detections is not equal to the number of values.ValueError
if a value produces a list of detections with 0 or multiple results in it.
-
get_languages
(target_language=None)[source]# Get list of supported languages for translation.
Response
See: https://cloud.google.com/translate/v2/ discovering-supported-languages-with-rest
Parameters: target_language (str) – (Optional) The language used to localize returned language names. Defaults to the target language on the current client. Return type: list Returns: List of dictionaries. Each dictionary contains a supported ISO 639-1 language code (using the dictionary key language
). Iftarget_language
is passed, each dictionary will also contain the name of each supported language (localized to the target language).
-
translate
(values, target_language=None, format_=None, source_language=None, customization_ids=())[source]# Translate a string or list of strings.
See: https://cloud.google.com/translate/v2/ translating-text-with-rest
Parameters: - values (str or list) – String or list of strings to translate.
- target_language (str) – The language to translate results into. This is required by the API and defaults to the target language of the current instance.
- format (str) – (Optional) One of
text
orhtml
, to specify if the input text is plain text or HTML. - source_language (str) – (Optional) The language of the text to be translated.
- customization_ids (str or list) – (Optional) ID or list of customization IDs
for translation. Sets the
cid
parameter in the query.
Return type: str or list list
Returns: A list of dictionaries for each queried value. Each dictionary typically contains three keys (though not all will be present in all cases)
detectedSourceLanguage
: The detected language (as an ISO 639-1 language code) of the text.translatedText
: The translation of the text into the target language.input
: The corresponding input value.
If only a single value is passed, then only a single dictionary will be returned.
Raises: ValueError
if the number of values and translations differ.
-
google.cloud.translate.client.
ENGLISH_ISO_639
= 'en'# ISO 639-1 language code for English.
Connection#
Create / interact with Google Cloud Translate connections.
-
class
google.cloud.translate.connection.
Connection
(credentials=None, http=None)[source]# Bases:
google.cloud.connection.JSONConnection
A connection to Google Cloud Translate via the JSON REST API.
-
API_BASE_URL
= 'https://www.googleapis.com'# The base of the API call URL.
-
API_URL_TEMPLATE
= '{api_base_url}/language/translate/{api_version}{path}'# A template for the URL of a particular API call.
-
API_VERSION
= 'v2'# The version of the API, used in building the API call’s URL.
-