a
    !f#                     @   sD   d Z ddlZddlZddlmZ ddlmZ dZG dd deZ	dS )z;Client for interacting with the Google Cloud Translate API.    N	_to_bytes)
Connectionenc                   @   s:   e Zd ZdZdefddZdddZdd Zdd
dZdS )Cliental  Client to bundle configuration needed for API requests.

    :type api_key: str
    :param api_key: The key used to send with requests as a query
                    parameter.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: (Optional) HTTP object to make requests. If not
                 passed, an :class:`httplib.Http` object is created.

    :type target_language: str
    :param target_language: (Optional) The target language used for
                            translations and language names. (Defaults to
                            :data:`ENGLISH_ISO_639`.)
    Nc                 C   s,   || _ |d u rt }t|d| _|| _d S )N)http)api_keyhttplib2ZHttpr   
connectiontarget_language)selfr   r   r    r   X/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/translate/client.py__init__.   s
    zClient.__init__c                 C   sN   d| j i}|du r| j}|dur(||d< | jjdd|d}|di dd	S )
a&  Get list of supported languages for translation.

        Response

        See: https://cloud.google.com/translate/v2/        discovering-supported-languages-with-rest

        :type target_language: str
        :param target_language: (Optional) The language used to localize
                                returned language names. Defaults to the
                                target language on the current client.

        :rtype: list
        :returns: List of dictionaries. Each dictionary contains a supported
                  ISO 639-1 language code (using the dictionary key
                  ``language``). If ``target_language`` is passed, each
                  dictionary will also contain the name of each supported
                  language (localized to the target language).
        keyNtargetGETz
/languagesmethodpathquery_paramsdata	languagesr   )r   r   r
   api_requestget)r   r   r   responser   r   r   get_languages5   s    
zClient.get_languagesc           	      C   s  d}t |tjrd}|g}d| jfg}|dd |D  | jjdd|d}|d	i d
d}t|t|kr|t	d||t
|D ]h\}}t|| dkr|| d ||< n"dt|| f }t	|||| ||| d< || dd q|r|d S |S dS )a  Detect the language of a string or list of strings.

        See: https://cloud.google.com/translate/v2/        detecting-language-with-rest

        :type values: str or list
        :param values: String or list of strings that will have
                       language detected.

        :rtype: 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: :class:`ValueError <exceptions.ValueError>` if the number of
                 detections is not equal to the number of values.
                 :class:`ValueError <exceptions.ValueError>` if a value
                 produces a list of detections with 0 or multiple results
                 in it.
        FTr   c                 s   s   | ]}d t |dfV  qdS qzutf-8Nr   .0valuer   r   r   	<genexpr>v   s   z)Client.detect_language.<locals>.<genexpr>r   z/detectr   r   
detectionsr   z-Expected same number of values and detections   r   z6Expected a single detection per value, API returned %dinputZ
isReliableN)
isinstancesixstring_typesr   extendr
   r   r   len
ValueError	enumeratepop)	r   valuessingle_valuer   r   r#   indexr!   messager   r   r   detect_languageR   s8    
zClient.detect_languager   c                 C   s  d}t |tjrd}|g}|du r(| j}t |tjr:|g}d| jfd|fg}|dd |D  |dd |D  |dur|d	|f |dur|d
|f | jjdd|d}|	di 	dd}	t
|t
|	krtd||	tj||	D ]\}
}|
|d< q|r|	d S |	S dS )a9  Translate a string or list of strings.

        See: https://cloud.google.com/translate/v2/        translating-text-with-rest

        :type values: str or list
        :param values: String or list of strings to translate.

        :type target_language: str
        :param target_language: The language to translate results into. This
                                is required by the API and defaults to
                                the target language of the current instance.

        :type format_: str
        :param format_: (Optional) One of ``text`` or ``html``, to specify
                        if the input text is plain text or HTML.

        :type source_language: str
        :param source_language: (Optional) The language of the text to
                                be translated.

        :type customization_ids: str or list
        :param customization_ids: (Optional) ID or list of customization IDs
                                  for translation. Sets the ``cid`` parameter
                                  in the query.

        :rtype: 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: :class:`ValueError <exceptions.ValueError>` if the number of
                 values and translations differ.
        FTNr   r   c                 s   s   | ]}d t |dfV  qdS r   r   r   r   r   r   r"      s   z#Client.translate.<locals>.<genexpr>c                 s   s   | ]}d |fV  qdS )cidNr   )r    r3   r   r   r   r"          formatsourcer    r   r   translationsr   z'Expected iterations to have same lengthr%   r   )r&   r'   r(   r   r   r)   appendr
   r   r   r*   r+   moveszip)r   r.   r   Zformat_Zsource_languageZcustomization_idsr/   r   r   r8   r!   translationr   r   r   	translate   s<    ,

zClient.translate)N)NNNr   )	__name__
__module____qualname____doc__ENGLISH_ISO_639r   r   r2   r=   r   r   r   r   r      s   
B  r   )
rA   r	   r'   Zgcloud._helpersr   Zgcloud.translate.connectionr   rB   objectr   r   r   r   r   <module>   s   