o
    ٷi                     @  sH   d dl mZ d dlZd dlZd dlmZ G dd dZG dd dZdS )    )annotationsN)Anyc                   @  sZ   e Zd ZdZdddZddd	Zdd
dZdddZdd Zdd Z	dd Z
dddZdS )I18nDataz
    A class that wraps a translation key with metadata.

    This object will be serialized and sent to the frontend, where the actual
    translation will happen using the frontend's i18n system.
    keystrc                 C  s   || _ d| _dS )z
        Initialize a I18nData object.

        Args:
            key: The translation key to be translated in the frontend.
        translation_metadataN)r   _typeselfr    r   ?/home/ubuntu/.local/lib/python3.10/site-packages/gradio/i18n.py__init__   s   
zI18nData.__init__returndict[str, Any]c                 C  s   | j | jdS )z
        Convert the I18nData object to a dictionary for serialization.
        This allows the frontend to recognize it as a translatable object.
        )__type__r   )r   r   r
   r   r   r   to_dict   s   zI18nData.to_dictc                 C  s   ddl }d||   S )z
        String representation of the I18nData object.
        Used when the object is converted to a string.
        This returns a special format that can be recognized by the frontend
        as needing translation.
        r   N__i18n__)jsondumpsr   )r
   r   r   r   r   __str__!   s   zI18nData.__str__c                 C     |   S )zF
        Representation of the I18nData object for debugging.
        )r   r   r   r   r   __repr__,   s   zI18nData.__repr__c                 C  s   t | t | S )z=
        Handle string concatenation (self + other).
        r   r
   otherr   r   r   __add__2      zI18nData.__add__c                 C  s   t |t |  S )z=
        Handle string concatenation (other + self).
        r   r   r   r   r   __radd__8   r   zI18nData.__radd__c                   s:   | dr|drt jj d|  fdd}|S )z
        Handle attribute access for I18nData.
        This makes it possible to use I18nData objects in contexts
        that expect strings with methods.
        __z has no attribute c                    s    S Nr   )_args_kwargsr   r   r   methodG   s   z$I18nData.__getattr__.<locals>.method)
startswithendswithAttributeError	__class____name__)r
   namer#   r   r   r   __getattr__>   s   zI18nData.__getattr__c                 C  r   )z
        Convert the I18nData object to a JSON-serializable dictionary.
        This is used by the default Python JSON serializer.
        )r   r   r   r   r   tojsonL   s   zI18nData.tojsonN)r   r   )r   r   )r   r   )r(   
__module____qualname____doc__r   r   r   r   r   r   r*   r+   r   r   r   r   r      s    




r   c                   @  sF   e Zd ZdZedZdddZdddZdddZ	e
dddZdS )I18na  
    Handles internationalization (i18n) for Gradio applications.

    Stores translation dictionaries and provides a method to retrieve translation keys.
    The translation lookup happens on the frontend based on the browser's locale
    and the provided translation dictionaries.
    z ^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$translationsdict[str, str]c                 K  sD   i | _ | D ]\}}| |std| dt || j |< qdS )a  
        Initializes the I18n class.

        Args:
            **translations: Each keyword argument should be a locale code (e.g., "en", "fr") with a
                          dictionary value, which maps translation keys to translated strings.
                          Example: gr.I18n(en={"greeting": "Hello"}, es={"greeting": "Hola"})

                          These translations can be passed to the frontend for use there.
        zInvalid locale code: 'z'. Locale codes should follow BCP 47 format (e.g., 'en', 'en-US'). This locale will still be included, but may not work correctly.N)r0   items_is_valid_localewarningswarnUserWarning)r
   r0   localetranslation_dictr   r   r   r   `   s   

zI18n.__init__r7   r   r   boolc                 C  s   t | j|S r    )r9   _LOCALE_PATTERNmatch)r
   r7   r   r   r   r3   v   s   zI18n._is_valid_localer   r   c                 C  s   t |S )a  
        Returns a I18nData object containing the translation key.

        This metadata object will be serialized and sent to the frontend,
        where it will be translated by the frontend's i18n system.

        Args:
            key: The key to identify the translation string (e.g., "submit_button").

        Returns:
            A I18nData object containing the translation key.
        )r   r	   r   r   r   __call__y   s   zI18n.__call__dict[str, dict[str, str]]c                 C  s   | j S )z
        Returns the dictionary of translations provided during initialization.
        These can be passed to the frontend for use in its translation system.
        )r0   r   r   r   r   translations_dict   s   zI18n.translations_dictN)r0   r1   )r7   r   r   r9   )r   r   r   r   )r   r=   )r(   r,   r-   r.   recompiler:   r   r3   r<   propertyr>   r   r   r   r   r/   T   s    
	


r/   )
__future__r   r?   r4   typingr   r   r/   r   r   r   r   <module>   s    L