@@ -50,7 +50,7 @@ def _zip_assert(values1, values2):
5050class Client (object ):
5151 """Client to bundle configuration needed for API requests.
5252
53- :type key: string
53+ :type key: str
5454 :param key: The key used to send with requests as a query
5555 parameter.
5656
@@ -91,19 +91,18 @@ def get_languages(self, target_language=None):
9191 method = 'GET' , path = '/languages' , query_params = query_params )
9292 return response .get ('data' , {}).get ('languages' , ())
9393
94- def detect_language (self , values ):
94+ def detect_language (self , * values ):
9595 """Detect the language of a string or list of strings.
9696
9797 See: https://cloud.google.com/translate/v2/\
9898 detecting-language-with-rest
9999
100- :type values: str or list
101- :param values: String or list of strings that will have
102- language detected.
100+ :type values: tuple
101+ :param values: Tuple of strings that will have language detected.
103102
104103 :rtype: list
105104 :returns: A list of dictionaries for each queried value. Each
106- dictionary typically contains four keys (though not
105+ dictionary typically contains three keys (though not
107106 all will be present in all cases)
108107
109108 * ``confidence``: The confidence in language detection, a
@@ -118,8 +117,6 @@ def detect_language(self, values):
118117 :raises: :class:`ValueError <exceptions.ValueError>` if the number of
119118 detections is not equal to the number of values.
120119 """
121- if isinstance (values , six .string_types ):
122- values = [values ]
123120 query_params = [('key' , self .key )]
124121 query_params .extend (('q' , _to_bytes (value , 'utf-8' ))
125122 for value in values )
@@ -145,49 +142,53 @@ def detect_language(self, values):
145142
146143 return detections
147144
148- def translate (self , values , target_language = ENGLISH_ISO_639 ,
149- format_ = None , source_language = None ,
150- customization_ids = ()):
145+ def translate (self , * values , ** kwargs ):
151146 """Translate a string or list of strings.
152147
153148 See: https://cloud.google.com/translate/v2/\
154149 translating-text-with-rest
155150
156- :type values: str or list
157- :param values: String or list of strings that will have
158- language detected.
151+ Accepted keyword arguments are:
159152
160- :type target_language: str
161- :param target_language: The language to translate results into. This
162- is required by the API and defaults to
163- :data:`ENGLISH_ISO_639`.
164-
165- :type format_: str
166- :param format_: (Optional) One of ``text`` or ``html``, to specify
167- if the input text is plain text or HTML.
168-
169- :type source_language: str
170- :param source_language: (Optional) The language of the text to
171- be translated.
172-
173- :type customization_ids: list
174- :param customization_ids: (Optional) List of customization IDs for
175- translation. Sets the ``cid`` parameter
176- in the query.
153+ * ``target_language`` (str): The language to translate results into.
154+ This is required by the API and defaults to :data:`ENGLISH_ISO_639`.
155+ * ``format`` (str): (Optional) One of ``text`` or ``html``, to specify
156+ if the input text is plain text or HTML.
157+ * ``source_language`` (str): (Optional) The language of the text to
158+ be translated.
159+ * ``customization_ids`` (list): (Optional) List of customization IDs
160+ for translation. Sets the ``cid`` parameter in the query.
161+
162+ :type values: tuple
163+ :param values: Tuple of strings to translate.
164+
165+ :type kwargs: dict
166+ :param kwargs: Keyword arguments to be passed in.
167+
168+ :rtype: list
169+ :returns: A list of dictionaries for each queried value. Each
170+ dictionary typically contains three keys (though not
171+ all will be present in all cases)
172+
173+ * ``detectedSourceLanguage``: The detected language (as an
174+ ISO 639-1 language code) of the text.
175+ * ``translatedText``: The translation of the text into the
176+ target language.
177+ * ``input``: The corresponding input value.
177178 """
178- if isinstance ( values , six . string_types ):
179- values = [ values ]
179+ target_language = kwargs . get ( 'target_language' , ENGLISH_ISO_639 )
180+ customization_ids = kwargs . get ( 'customization_ids' , ())
180181 if isinstance (customization_ids , six .string_types ):
181182 customization_ids = [customization_ids ]
182183
183184 query_params = [('key' , self .key ), ('target' , target_language )]
184185 query_params .extend (('q' , _to_bytes (value , 'utf-8' ))
185186 for value in values )
186187 query_params .extend (('cid' , cid ) for cid in customization_ids )
187- if format_ is not None :
188- query_params .append (('format' , format_ ))
189- if source_language is not None :
190- query_params .append (('source' , source_language ))
188+ if 'format' in kwargs :
189+ query_params .append (('format' , kwargs [ 'format' ] ))
190+ if ' source_language' in kwargs :
191+ query_params .append (('source' , kwargs [ ' source_language' ] ))
191192
192193 response = self .connection .api_request (
193194 method = 'GET' , path = '' , query_params = query_params )
0 commit comments