@@ -79,16 +79,17 @@ def get_languages(self, target_language=None):
7979 method = 'GET' , path = '/languages' , query_params = query_params )
8080 return response .get ('data' , {}).get ('languages' , ())
8181
82- def detect_language (self , * values ):
82+ def detect_language (self , values ):
8383 """Detect the language of a string or list of strings.
8484
8585 See: https://cloud.google.com/translate/v2/\
8686 detecting-language-with-rest
8787
88- :type values: tuple
89- :param values: Tuple of strings that will have language detected.
88+ :type values: str or list
89+ :param values: String or list of strings that will have
90+ language detected.
9091
91- :rtype: list
92+ :rtype: str or list
9293 :returns: A list of dictionaries for each queried value. Each
9394 dictionary typically contains three keys (though not
9495 all will be present in all cases)
@@ -99,15 +100,19 @@ def detect_language(self, *values):
99100 * ``language``: The detected language (as an ISO 639-1
100101 language code).
101102
102- If multiple languages are detected for a given input, then
103- the there will be a list of dictionaries (instead of a single
104- dictionary) for that queried value.
103+ If only a single value is passed, then only a single
104+ dictionary will be returned.
105105 :raises: :class:`ValueError <exceptions.ValueError>` if the number of
106106 detections is not equal to the number of values.
107107 :class:`ValueError <exceptions.ValueError>` if a value
108108 produces a list of detections with 0 or multiple results
109109 in it.
110110 """
111+ single_value = False
112+ if isinstance (values , six .string_types ):
113+ single_value = True
114+ values = [values ]
115+
111116 query_params = [('key' , self .key )]
112117 query_params .extend (('q' , _to_bytes (value , 'utf-8' ))
113118 for value in values )
@@ -134,32 +139,40 @@ def detect_language(self, *values):
134139 # The ``isReliable`` field is deprecated.
135140 detections [index ].pop ('isReliable' , None )
136141
137- return detections
142+ if single_value :
143+ return detections [0 ]
144+ else :
145+ return detections
138146
139- def translate (self , * values , ** kwargs ):
147+ def translate (self , values , target_language = None , format_ = None ,
148+ source_language = None , customization_ids = ()):
140149 """Translate a string or list of strings.
141150
142151 See: https://cloud.google.com/translate/v2/\
143152 translating-text-with-rest
144153
145- Accepted keyword arguments are:
154+ :type values: str or list
155+ :param values: String or list of strings to translate.
146156
147- * ``target_language`` (str): The language to translate results into.
148- This is required by the API and defaults to :data:`ENGLISH_ISO_639`.
149- * ``format`` (str): (Optional) One of ``text`` or ``html``, to specify
150- if the input text is plain text or HTML.
151- * ``source_language`` (str): (Optional) The language of the text to
152- be translated.
153- * ``customization_ids`` (list): (Optional) List of customization IDs
154- for translation. Sets the ``cid`` parameter in the query.
157+ :type target_language: str
158+ :param target_language: The language to translate results into. This
159+ is required by the API and defaults to
160+ the target language of the current instance.
155161
156- :type values: tuple
157- :param values: Tuple of strings to translate.
162+ :type format_: str
163+ :param format_: (Optional) One of ``text`` or ``html``, to specify
164+ if the input text is plain text or HTML.
158165
159- :type kwargs: dict
160- :param kwargs: Keyword arguments to be passed in.
166+ :type source_language: str
167+ :param source_language: (Optional) The language of the text to
168+ be translated.
161169
162- :rtype: list
170+ :type customization_ids: str or list
171+ :param customization_ids: (Optional) ID or list of customization IDs
172+ for translation. Sets the ``cid`` parameter
173+ in the query.
174+
175+ :rtype: str or list list
163176 :returns: A list of dictionaries for each queried value. Each
164177 dictionary typically contains three keys (though not
165178 all will be present in all cases)
@@ -169,22 +182,30 @@ def translate(self, *values, **kwargs):
169182 * ``translatedText``: The translation of the text into the
170183 target language.
171184 * ``input``: The corresponding input value.
185+
186+ If only a single value is passed, then only a single
187+ dictionary will be returned.
172188 :raises: :class:`ValueError <exceptions.ValueError>` if the number of
173189 values and translations differ.
174190 """
175- target_language = kwargs .get ('target_language' , self .target_language )
176- customization_ids = kwargs .get ('customization_ids' , ())
191+ single_value = False
192+ if isinstance (values , six .string_types ):
193+ single_value = True
194+ values = [values ]
195+
196+ if target_language is None :
197+ target_language = self .target_language
177198 if isinstance (customization_ids , six .string_types ):
178199 customization_ids = [customization_ids ]
179200
180201 query_params = [('key' , self .key ), ('target' , target_language )]
181202 query_params .extend (('q' , _to_bytes (value , 'utf-8' ))
182203 for value in values )
183204 query_params .extend (('cid' , cid ) for cid in customization_ids )
184- if 'format' in kwargs :
185- query_params .append (('format' , kwargs [ 'format' ] ))
186- if ' source_language' in kwargs :
187- query_params .append (('source' , kwargs [ ' source_language' ] ))
205+ if format_ is not None :
206+ query_params .append (('format' , format_ ))
207+ if source_language is not None :
208+ query_params .append (('source' , source_language ))
188209
189210 response = self .connection .api_request (
190211 method = 'GET' , path = '' , query_params = query_params )
@@ -195,4 +216,8 @@ def translate(self, *values, **kwargs):
195216 values , translations )
196217 for value , translation in six .moves .zip (values , translations ):
197218 translation ['input' ] = value
198- return translations
219+
220+ if single_value :
221+ return translations [0 ]
222+ else :
223+ return translations
0 commit comments