o
    Fh                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ G dd	 d	ed	 ZdS )
    N)contextmanager)
ContextVar)Path)Dict	GeneratorOptionalTupleUnion)	LazyProxy)ContextInstanceMixinc                   @   s   e Zd Zddddeeef dededdfd	d
ZedefddZej	deddfddZe
deded fddZe
ded fddZdeeejf fddZd%ddZedeedf fddZ	d&dedee d edee def
d!d"Z	d&dedee d edee def
d#d$ZdS )'I18nenmessages)default_localedomainpathr   r   returnNc                C   s.   || _ || _|| _td|d| _|  | _d S )Naiogram_ctx_locale)default)r   r   r   r   
ctx_localefind_localeslocales)selfr   r   r    r   J/var/www/html/venv/lib/python3.10/site-packages/aiogram/utils/i18n/core.py__init__   s
   zI18n.__init__c                 C   s
   | j  S N)r   getr   r   r   r   current_locale   s   
zI18n.current_localevaluec                 C   s   | j | d S r   )r   set)r   r    r   r   r   r      s   locale)NNNc              	   c   s6    | j |}zdV  W | j | dS | j | w )z6
        Create context with specified locale
        N)r   r!   reset)r   r"   	ctx_tokenr   r   r   
use_locale"   s
   zI18n.use_locale)r   NNc              	   c   s0    |  | }z| V  W | | dS | | w )z"
        Use I18n context
        N)set_currentreset_current)r   tokenr   r   r   context-   s
   
zI18n.contextc              	   C   s   i }t | jD ]S}t jt j| j|sqt j| j|d| jd }t j|rGt|d}t	|||< W d   n1 sAw   Y  qt j|dd d r[t
d| dq|S )	zY
        Load all compiled locales from path

        :return: dict with locales
        LC_MESSAGESz.morbNpozFound locale 'z$' but this language is not compiled!)oslistdirr   isdirjoinr   existsopengettextGNUTranslationsRuntimeError)r   translationsnamemo_pathfpr   r   r   r   8   s   zI18n.find_localesc                 C   s   |   | _dS )z$
        Hot reload locales
        N)r   r   r   r   r   r   reloadM   s   zI18n.reload.c                 C   s   t | j S )z:
        list of loaded locales

        :return:
        )tupler   keysr   r   r   r   available_localesS   s   zI18n.available_locales   singularpluralnc                 C   sZ   |du r| j }|| jvr|dkr|S |r|S |S | j| }|du r&||S ||||S )z
        Get text

        :param singular:
        :param plural:
        :param n:
        :param locale:
        :return:
        Nr?   )r   r   r4   ngettext)r   r@   rA   rB   r"   
translatorr   r   r   r4   \   s   


zI18n.gettextc                 C   s   t | j||||ddS )NF)r@   rA   rB   r"   enable_cache)r
   r4   )r   r@   rA   rB   r"   r   r   r   lazy_gettextv   s   zI18n.lazy_gettext)r   N)Nr?   N)__name__
__module____qualname__r	   strr   r   propertyr   setterr   r   r%   r)   r   r4   r5   r   r;   r   r>   r   intr
   rF   r   r   r   r   r      s`    




	
r   )r4   r.   
contextlibr   contextvarsr   pathlibr   typingr   r   r   r   r	   aiogram.utils.i18n.lazy_proxyr
   aiogram.utils.mixinsr   r   r   r   r   r   <module>   s    