o
    ¥gP                     @   sl   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ G dd	 d	eZd
S )    )settings) is_language_prefix_patterns_used)HttpResponseRedirect)get_script_prefixis_valid_path)translation)patch_vary_headers)MiddlewareMixinc                   @   s,   e Zd ZdZeZdd Zdd Zdd ZdS )	LocaleMiddlewarez
    Parse a request and decide what translation object to install in the
    current thread context. This allows pages to be dynamically translated to
    the language the user desires (if the language is available).
    c                 C   s(   zt tjW S  ty   tj Y S w )a  
        Return the fallback language for the current request based on the
        settings. If LANGUAGE_CODE is a variant not included in the supported
        languages, get_fallback_language() will try to fallback to a supported
        generic variant.

        Can be overridden to have a fallback language depending on the request,
        e.g. based on top level domain.
        )r   get_supported_language_variantr   LANGUAGE_CODELookupError)selfrequest r   U/var/www/rescue_company/venv/lib/python3.10/site-packages/django/middleware/locale.pyget_fallback_language   s
   

z&LocaleMiddleware.get_fallback_languagec                 C   sN   t |dtj}t|\}}tj||d}|s| |}t| t |_	d S )Nurlconf)
check_path)
getattrr   ROOT_URLCONFr   r   get_language_from_requestr   activateget_languager   )r   r   r   i18n_patterns_used_languager   r   r   process_request"   s   

z LocaleMiddleware.process_requestc                 C   s   t  }t |j}t |}t|dtj}t|\}}|j	dkrj|sj|rj|s)|rjd||jf }	t
|	|}
|
 oGtjoG|	d oGt
d|	 |}|
sL|rjt }|j|d|d||f d}| |}t|d	 |S |rn|sst|d
 |jd| |S )Nr   i  z/%s%s/z%s/)force_append_slashz%s%s/   )Accept-LanguageCookie)r!   zContent-Language)r   r   get_language_from_path	path_infor   r   r   r   r   status_coder   APPEND_SLASHendswithr   get_full_pathreplaceresponse_redirect_classr   headers
setdefault)r   r   responser   language_from_pathlanguage_from_requestr   r   prefixed_default_languagelanguage_path
path_validpath_needs_slashscript_prefixlanguage_urlredirectr   r   r   process_response.   sN   






z!LocaleMiddleware.process_responseN)	__name__
__module____qualname____doc__r   r*   r   r   r7   r   r   r   r   r
   
   s    r
   N)django.confr   django.conf.urls.i18nr   django.httpr   django.urlsr   r   django.utilsr   django.utils.cacher   django.utils.deprecationr	   r
   r   r   r   r   <module>   s    