
    ;3h                    T    d dl mZ d dlZd dlZd dlmZ  G d d      Z G d d      Zy)    )annotationsN)Anyc                  J    e Zd ZdZddZddZddZddZd Zd Z	d Z
dd	Zy
)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.
    c                     || _         d| _        y)z
        Initialize a I18nData object.

        Args:
            key: The translation key to be translated in the frontend.
        translation_metadataN)key_typeselfr	   s     K/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/i18n.py__init__zI18nData.__init__   s     +
    c                4    | 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   s    r   to_dictzI18nData.to_dict   s    
 !JJtxx88r   c                N    ddl }d|j                  | j                                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   s     r   __str__zI18nData.__str__!   s$     	$**T\\^4566r   c                "    | j                         S )zF
        Representation of the I18nData object for debugging.
        )r   r   s    r   __repr__zI18nData.__repr__,   s     ||~r   c                0    t        |       t        |      z   S )z=
        Handle string concatenation (self + other).
        strr   others     r   __add__zI18nData.__add__2   s     4y3u:%%r   c                0    t        |      t        |       z   S )z=
        Handle string concatenation (other + self).
        r   r   s     r   __radd__zI18nData.__radd__8   s     5zCI%%r   c                     |j                  d      r5|j                  d      r$t         j                  j                   d|        f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 N )_args_kwargsr   s     r   methodz$I18nData.__getattr__.<locals>.methodG   s    Kr   )
startswithendswithAttributeError	__class____name__)r   namer*   s   `  r   __getattr__zI18nData.__getattr__>   sJ     ??4 T]]4%8 DNN$;$;#<<Ntf!UVV	 r   c                "    | j                         S )z
        Convert the I18nData object to a JSON-serializable dictionary.
        This is used by the default Python JSON serializer.
        )r   r   s    r   tojsonzI18nData.tojsonL   s    
 ||~r   N)r	   r   )returnzdict[str, Any])r4   r   )r/   
__module____qualname____doc__r   r   r   r   r    r"   r1   r3   r'   r   r   r   r      s/    ,9	7&&r   r   c                  ^    e Zd ZdZ ej
                  d      ZddZd	dZd
dZ	e
dd       Zy)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})*$c                    i | _         |j                         D ]C  \  }}| j                  |      st        j                  d| dt
               || j                   |<   E y)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)translationsitems_is_valid_localewarningswarnUserWarning)r   r;   localetranslation_dicts       r   r   zI18n.__init__`   sl     (4(:(:(< 	9$F$((0,VH 5V W
 )9Df%	9r   c                J    t        | j                  j                  |            S r&   )bool_LOCALE_PATTERNmatch)r   rA   s     r   r=   zI18n._is_valid_localev   s    D((..v677r   c                    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   s     r   __call__zI18n.__call__y   s     }r   c                    | j                   S )z
        Returns the dictionary of translations provided during initialization.
        These can be passed to the frontend for use in its translation system.
        )r;   r   s    r   translations_dictzI18n.translations_dict   s        r   N)r;   zdict[str, str])rA   r   r4   rD   )r	   r   r4   r   )r4   zdict[str, dict[str, str]])r/   r5   r6   r7   recompilerE   r   r=   rH   propertyrJ   r'   r   r   r9   r9   T   s=     !bjj!DEO9,8 ! !r   r9   )
__future__r   rK   r>   typingr   r   r9   r'   r   r   <module>rP      s(    " 	  I IX:! :!r   