3
(h                 @   sp   d Z 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 )zHThis is the locale selecting middleware that will look at accept headers    )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S )LocaleMiddlewarea  
    This is a very simple middleware that parses a request
    and decides 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, of course).
    c             C   sb   t |dtj}t|\}}tj||d}tj|j}| rJ|rJ| rJtj}tj	| tj
 |_d S )Nurlconf)
check_path)getattrr   ROOT_URLCONFr   r   Zget_language_from_requestget_language_from_path	path_infoZLANGUAGE_CODEactivateget_language)selfrequestr   i18n_patterns_usedprefixed_default_languagelanguagelanguage_from_path r   </tmp/pip-install-q3hcpn_q/Django/django/middleware/locale.pyprocess_request   s    
z LocaleMiddleware.process_requestc             C   s   t j }t j|j}t|dtj}t|\}}|jdkr| r|rd||jf }t	||}	|	 o~tj
o~|jd o~t	d| |}
|	s|
rt }|j|
dj|d||f d}| j|S |o|st|d d
|kr||d
< |S )Nr   i  z/%s%s/z%s/)Zforce_append_slashz%s%s/   Accept-LanguagezContent-Language)r   )r   r   r   r   r   r   r   r   status_coder   ZAPPEND_SLASHendswithr   Zget_full_pathreplaceresponse_redirect_classr   )r   r   responser   r   r   r   r   Zlanguage_pathZ
path_validZpath_needs_slashZscript_prefixZlanguage_urlr   r   r   process_response    s,    



z!LocaleMiddleware.process_responseN)__name__
__module____qualname____doc__r   r"   r   r$   r   r   r   r   r
      s   
r
   N)r(   Zdjango.confr   Zdjango.conf.urls.i18nr   Zdjango.httpr   Zdjango.urlsr   r   Zdjango.utilsr   Zdjango.utils.cacher   Zdjango.utils.deprecationr	   r
   r   r   r   r   <module>   s   