
 Xt                 @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z 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 d d l m 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! m" Z" d d l# m$ Z$ d d l% m& Z& m' Z' m( Z( i  a) e   Z* d a+ d Z, e j- d e j.  Z/ e j- d e j0  Z1 e j- d  Z2 e e  d d    Z3 d d d  Z4 d d   Z5 Gd d   d e j6  Z7 d d    Z8 d! d"   Z9 d# d$   Z: d% d&   Z; d' d(   Z< d) d*   Z= d+ d,   Z> d- d.   Z? d/ d0   Z e j@ rhe ZA n d1 d2   ZA d3 d4   ZB d5 d6   ZC d7 d8   ZD d9 d:   ZE e j@ reE ZF n d; d<   ZF d= d>   ZG d? d@   ZH e j dA dB  dC dD    ZI e j   dE dF    ZJ e j dA dB  d dG dH   ZK d dI dJ  ZL d dK dL  ZM e j- dM  ZN dN dO   ZO e j- dP  ZP e j- dQ  ZQ e j- dR  ZR e j- dS  ZS e j- dT  ZT e j- dU  ZU d dV dW  ZV dX dY   ZW d S)ZzTranslation helper functions.    )unicode_literalsN)OrderedDict)local)apps)settings)	LANG_INFO)AppRegistryNotReady)setting_changed)receiver)	lru_cachesix)upath)
force_text)SafeData	mark_safe)StringIO)LANGUAGE_SESSION_KEYTranslatorCommentWarningtrim_whitespacea  
        ([A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*)      # "en", "en-au", "x-y-z", "es-419", "*"
        (?:\s*;\s*q=(0(?:\.\d{,3})?|1(?:.0{,3})?))?   # Optional "q=1.00", "q=0.8"
        (?:\s*,\s*|$)                                 # Multiple accepts per header.
        z3^[a-z]{1,8}(?:-[a-z0-9]{1,8})*(?:@[a-z0-9]{1,20})?$z^/([\w@-]+)(/|$)c              K   s5   |  d d k r1 t  j   t j   t j   n  d S)zy
    Reset global state when LANGUAGES setting has been changed, as some
    languages should no longer be accepted.
    Zsetting	LANGUAGESLANGUAGE_CODEN)z	LANGUAGESzLANGUAGE_CODE)check_for_languagecache_clearget_languagesget_supported_language_variant)kwargs r   Q/home/ubuntu/projects/ifolica/build/django/django/utils/translation/trans_real.pyreset_cache6   s    

r   Fc             C   s   |  j  d  } | d k r | rQ |  d |  j   d |  | d d  j   St |  | d d   d k r |  d |  j   d |  | d j   |  | d d  j   S|  d |  j   d |  | d d  j   Sn
 |  j   Sd S)z
    Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
    True, the last component is lower-cased (en_us).
    -r   N_      )findlowerlenupper)languageZto_lowerpr   r   r   	to_localeB   s    0 B3r*   c             C   sY   |  j  d  } | d k rK |  d |  j   d |  | d d  j   S|  j   Sd S)z9Turns a locale name (en_US) into a language name (en-us).r!   r   Nr    r"   )r$   r%   )localer)   r   r   r   to_languageT   s    0r,   c               @   s   e  Z d  Z d Z d Z d d d d  Z d d   Z d d	 d
  Z d d   Z d d   Z	 d d   Z
 d d d  Z d d   Z d d   Z d d   Z d S)DjangoTranslationam  
    This class sets up the GNUTranslations context with regard to output
    charset.

    This translation object will be constructed out of multiple GNUTranslations
    objects by merging their catalogs. It will construct an object for the
    requested language and add a fallback to the default language, if it's
    different from the requested language.
    djangoNc             C   sb  t  j j |   | d k	 r( | |  _ n  |  j d  | |  _ t |  |  _ t |  |  _	 d |  _
 |  j d k r | d k	 r t j d t  d } n  |  j   n  | r x7 | D]" } |  j |  } |  j |  q Wn
 |  j   |  j   |  j t j k r6|  j d k r6|  j
 d k r6t d t j   n  |  j |  |  j
 d k r^i  |  _
 n  d S)z8Create a GNUTranslations() using many locale directoriesNzutf-8r.   z.localedirs is ignored when domain is 'django'.z3No translation files found for default language %s.)gettext_moduleGNUTranslations__init__domainset_output_charset_DjangoTranslation__languager,   _DjangoTranslation__to_languager*   _DjangoTranslation__locale_catalogwarningswarnRuntimeWarning_init_translation_catalog_new_gnu_transmerge _add_installed_apps_translations_add_local_translationsr   r   IOError_add_fallback)selfr(   r2   
localedirs	localedirtranslationr   r   r   r1   i   s0    			

0zDjangoTranslation.__init__c             C   s   d |  j  S)Nz<DjangoTranslation lang:%s>)r4   )rB   r   r   r   __repr__   s    zDjangoTranslation.__repr__Tc             C   s1   t  j d |  j d | d |  j g d d d |  S)z
        Returns a mergeable gettext.GNUTranslations instance.

        A convenience wrapper. By default gettext uses 'fallback=False'.
        Using param `use_null_fallback` to avoid confusion with any other
        references to 'fallback'.
        r2   rD   	languagescodesetzutf-8fallback)r/   rE   r2   r6   )rB   rD   Zuse_null_fallbackr   r   r   r<      s    		z DjangoTranslation._new_gnu_transc             C   sZ   t  t j t j j  } t j j t j j	 |  d  } |  j
 |  } |  j |  d S)z8Creates a base catalog using global django translations.r+   N)r   sysmodulesr   
__module____file__ospathjoindirnamer<   r=   )rB   ZsettingsfilerD   rE   r   r   r   r;      s    !z+DjangoTranslation._init_translation_catalogc             C   s   y t  t t j     } Wn t k
 r< t d   Yn XxB | D]: } t j j | j d  } |  j |  } |  j	 |  qD Wd S)z,Merges translations from each installed app.zThe translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.r+   N)
reversedlistr   Zget_app_configsr   rN   rO   rP   r<   r=   )rB   Zapp_configsZ
app_configrD   rE   r   r   r   r>      s    z2DjangoTranslation._add_installed_apps_translationsc             C   s:   x3 t  t j  D]" } |  j |  } |  j |  q Wd S)z,Merges translations defined in LOCALE_PATHS.N)rR   r   LOCALE_PATHSr<   r=   )rB   rD   rE   r   r   r   r?      s    z)DjangoTranslation._add_local_translationsc             C   sx   |  j  t j k s$ |  j  j d  r( d S|  j d k rI t t j  } n t t j d |  j d | } |  j |  d S)z>Sets the GNUTranslations() fallback with the default language.enNr.   r2   rC   )r4   r   r   
startswithr2   rE   r-   add_fallback)rB   rC   Zdefault_translationr   r   r   rA      s    $zDjangoTranslation._add_fallbackc             C   so   t  | d d  s d S|  j d k rX | j |  _ | j j   |  _ | j j   |  _ n |  j j | j  d S)z,Merge another translation into this catalog.r7   N)getattrr7   plural_infocopyupdate)rB   otherr   r   r   r=      s    zDjangoTranslation.mergec             C   s   |  j  S)z!Returns the translation language.)r4   )rB   r   r   r   r(      s    zDjangoTranslation.languagec             C   s   |  j  S)z&Returns the translation language name.)r5   )rB   r   r   r   r,      s    zDjangoTranslation.to_language)__name__rL   __qualname____doc__r2   r1   rF   r<   r;   r>   r?   rA   r=   r(   r,   r   r   r   r   r-   ]   s   	#r-   c             C   s'   |  t  k r t |   t  |  <n  t  |  S)zF
    Returns a translation object in the default 'django' domain.
    )_translationsr-   )r(   r   r   r   rE      s    rE   c             C   s   |  s
 d St  |   t _ d S)z
    Fetches the translation object for a given language and installs it as the
    current translation object for the current thread.
    N)rE   _activevalue)r(   r   r   r   activate   s    rd   c               C   s   t  t d  r t ` n  d S)z
    Deinstalls the currently active translation object so that further _ calls
    will resolve against the default translation object, again.
    rc   N)hasattrrb   rc   r   r   r   r   
deactivate   s    rf   c               C   s%   t  j   t _ d d   t j _ d S)z
    Makes the active translation object a NullTranslations() instance. This is
    useful when we want delayed translations to appear as the original string
    for some reason.
    c              W   s   d  S)Nr   )argsr   r   r   <lambda>  s    z deactivate_all.<locals>.<lambda>N)r/   NullTranslationsrb   rc   r,   r   r   r   r   deactivate_all   s    rj   c              C   sK   t  t d d  }  |  d k	 rD y |  j   SWqD t k
 r@ YqD Xn  t j S)z(Returns the currently selected language.rc   N)rX   rb   r,   AttributeErrorr   r   )tr   r   r   get_language  s    rm   c              C   s@   t    }  |  d k r d St    j d  d } | t j k Sd S)zx
    Returns selected language's BiDi layout.

    * False = left-to-right layout
    * True = right-to-left layout
    NFr    r   )rm   splitr   ZLANGUAGES_BIDI)langZ	base_langr   r   r   get_language_bidi  s
    	rp   c              C   sD   t  t d d  }  |  d k	 r" |  St d k r@ t t j  a n  t S)z
    Returns the current active catalog for further processing.
    This can be used if you need to modify the catalog or want to access the
    whole message catalog instead of just translating one string.
    rc   N)rX   rb   _defaultrE   r   r   )rl   r   r   r   catalog  s    rr   c             C   s   |  j  t d  t d   j  t d  t d   } t |  d k r] t |   d  } n< t po t t j  a t t	 d t  } t | |  |  } t
 |  t  r t |  S| S)a-  
    Translates 'message' using the given 'translation_function' name -- which
    will be either gettext or ugettext. It uses the current thread to find the
    translation object to use. If no current translation is activated, the
    message will be run through the default translation object.
    z

r    rc   )replacestrr&   typerq   rE   r   r   rX   rb   
isinstancer   r   )messagetranslation_functionZeol_messageresultZtranslation_objectr   r   r   do_translate/  s    
6
r}   c             C   s   t  |  d  S)z
    Returns a string of the translation of the message.

    Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
    gettext)r}   )rz   r   r   r   r~   K  s    r~   c             C   s   t  |  d  S)Nugettext)r}   )rz   r   r   r   r   V  s    r   c             C   s>   d |  t  | f } t |  } t  | k r: t |  } n  | S)Nz%s%s%s)CONTEXT_SEPARATORr   r   )contextrz   Zmsg_with_ctxtr|   r   r   r   pgettextZ  s
    r   c             C   s   |  S)z
    Marks strings for translation but doesn't translate them now. This can be
    used to store strings in global variables that should stay in the base
    language (because they might be used externally) and will be translated
    later.
    r   )rz   r   r   r   gettext_noopd  s    r   c             C   sn   t  t d d   } | d  k	 r7 t  | |  |  | |  St d  k rU t t j  a n  t  t |  |  | |  S)Nrc   )rX   rb   rq   rE   r   r   )singularrY   numberr{   rl   r   r   r   do_ntranslaten  s    r   c             C   s   t  |  | | d  S)z
    Returns a string of the translation of either the singular or plural,
    based on the number.

    Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
    ngettext)r   )r   rY   r   r   r   r   r   y  s    r   c             C   s   t  |  | | d  S)z}
        Returns a unicode strings of the translation of either the singular or
        plural, based on the number.
        	ungettext)r   )r   rY   r   r   r   r   r     s    r   c             C   sZ   d |  t  | f d |  t  | f | f } t |   } t  | k rV t | | |  } n  | S)Nz%s%s%s)r   r   )r   r   rY   r   Zmsgs_with_ctxtr|   r   r   r   	npgettext  s    	r   c              C   sH   t  j j t  j j t t j t j j	   d  }  |  g t
 t j  S)zC
    Returns a list of paths to user-provides languages files.
    r+   )rN   rO   rP   rQ   r   rJ   rK   r   rL   rM   rS   rT   )Z
globalpathr   r   r   all_locale_paths  s    	+r   maxsizei  c             C   s`   |  d k s t  j |   r  d Sx9 t   D]. } t j d | t |   g  d k	 r* d Sq* Wd S)a  
    Checks whether there is a global language file for the given language
    code. This is used to decide whether a user-provided language is
    available.

    lru_cache should have a maxsize to prevent from memory exhaustion attacks,
    as the provided language codes are taken from the HTTP request. See also
    <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
    NFr.   T)language_code_researchr   r/   r$   r*   )	lang_coderO   r   r   r   r     s    $r   c               C   s   t  t j  S)zP
    Cache of settings.LANGUAGES in an OrderedDict for easy lookups by key.
    )r   r   r   r   r   r   r   r     s    r   c             C   s   |  r |  g } y | j  t |  d  Wn t k
 r< Yn X|  j d  d } | j |  t   } x* | D]" } | | k rm t |  rm | Sqm W| s x( | D] } | j | d  r | Sq Wq n  t |    d S)a  
    Returns the language-code that's listed in supported languages, possibly
    selecting a more generic variant. Raises LookupError if nothing found.

    If `strict` is False (the default), the function will look for an alternative
    country-specific variant when the currently checked is not found.

    lru_cache should have a maxsize to prevent from memory exhaustion attacks,
    as the provided language codes are taken from the HTTP request. See also
    <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
    rI   r    r   N)	extendr   KeyErrorrn   appendr   r   rV   LookupError)r   strictZpossible_lang_codesZgeneric_lang_codesupported_lang_codescodeZsupported_coder   r   r   r     s"    		r   c             C   sY   t  j |   } | s d S| j d  } y t | d | SWn t k
 rT d SYn Xd S)z
    Returns the language-code if there is a valid language-code
    found in the `path`.

    If `strict` is False (the default), the function will look for an alternative
    country-specific variant when the currently checked is not found.
    Nr"   r   )language_code_prefix_rematchgroupr   r   )rO   r   Zregex_matchr   r   r   r   get_language_from_path  s    r   c             C   sc  | r( t  |  j  } | d k	 r( | Sn  t   } t |  d  r} |  j j t  } | | k r} | d k	 r} t |  r} | Sn  |  j j t	 j
  } y t |  SWn t k
 r Yn X|  j j d d  } xe t |  D]W \ } } | d k r Pn  t j |  sq n  y t |  SWq t k
 r-w Yq Xq Wy t t	 j  SWn t k
 r^t	 j SYn Xd S)a  
    Analyzes the request to find what language the user wants the system to
    show. Only languages listed in settings.LANGUAGES are taken into account.
    If the user requests a sublanguage where we have a main language, we send
    out the main language.

    If check_path is True, the URL path prefix will be checked for a language
    code, otherwise this is skipped for backwards compatibility.
    NsessionZHTTP_ACCEPT_LANGUAGEru   *)r   Z	path_infor   re   r   getr   r   ZCOOKIESr   ZLANGUAGE_COOKIE_NAMEr   r   ZMETAparse_accept_lang_headerr   r   r   )request
check_pathr   r   acceptZaccept_langunusedr   r   r   get_language_from_request  s8    
	$r   z\Sc             C   s   t  j | |   S)zh
    Changes every non-whitespace character to the given char.
    Used in the templatize function.
    )dot_resub)srccharr   r   r   blankout$  s    r   z.^\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?'))\s*z^\s*trans\s+((?:"[^"]*?")|(?:'[^']*?'))(?:\s*\|\s*[^\s:]+(?::(?:[^\s'":]+|(?:"[^"]*?")|(?:'[^']*?')))?)*(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?\s*zD^\s*blocktrans(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)z^\s*endblocktrans$z^\s*plural$z_\(((?:".*?")|(?:'.*?'))\)c       +      C   s  d d l  m } m } m } m } m } m } t |  t j	  }  t
 d  } d }	 d }
 d } d } g  } g  } d } g  } i  } d } t j r d n d } d d d  } x<| |   j   D](} | r| j | k r| j d	 k rd j |  } d } xA t | j d
   D]* \ } } | j   j |  r| } qqWx\ t | j d
   D]E \ } } | d k	 r| | k r| j d |  qX| j d  qXWd } g  } q| j | j  q |
 r?| j | k rt j | j  } t j | j  } | re| r|	 rE| j d j |	 | | |  | | |  d |  n1 | j d j | | |  | | |  d |  x$ | D] } | j t | d   q}Wx | D] } | j t | d   qWn} |	 r| j d j |	 | | |  d |  n% | j d j | | |  d |  x$ | D] } | j t | d   q$Wd }	 d }
 d } g  } g  } q<| rtd
 } q<d } | rd | } n  t d | j | | j f   q| j | k r| r| j d | j  q<| j d | j  q| j | k r| j j d d  } | r,| j |  q<| j |  qq | d k	 r| j | j j d  } | | k r| j | k rxN | | D]B }  d } | rd | } n  d |  | | f }! t  j! |! t"  qWg  | | <qn | j d d j | |   d } n  | j | k rt# j | j  }" t$ j | j  }# t% j& | j  }$ |" rx|" j' d  }% |% d d k r|% j( d  }% n" |% d d k r|% j( d  }% n  |% j d d  }% |" j' d   rYt) j |" j' d    }& |& j' d  }	 |	 d d k r|	 j( d  }	 n" |	 d d k r1|	 j( d  }	 n  | j d j |	 |% d |  d }	 q| j d j |% d |  q|# rVx+ t% j& | j  D] }' | j d! |'  qW|# j' d  r)t) j |# j' d   }& |& j' d  }	 |	 d d k r|	 j( d  }	 q)|	 d d k r)|	 j( d  }	 q)n  d
 }
 d } d" | j*   k } g  } g  } q|$ rxS |$ D] }( | j d! |(  qcWq| j d# k rd
 } q| j t | j d$   q | j | k ry| j j+ d%  }) t% j |) d  }( |( r| j d! |( j' d   n  x |) d d  D]U }* |* j, d&  d k r\| j d' |* j+ d( d  d  q| j t |* d)   qWq | j | k r| j j   j |  r| j- | j g   j | j  | j } qq | j t | j d*   q W| j.   S)+z
    Turns a Django template into something that is understood by xgettext. It
    does so by translating the Django translation tags into standard gettext
    function invocations.
    r   )Lexer
TOKEN_TEXT	TOKEN_VARTOKEN_BLOCKTOKEN_COMMENTTRANSLATOR_COMMENT_MARKru   NFuc             S   s(   d j  |   } | r$ t |  } n  | S)Nru   )rP   r   )tokensZtrimrz   r   r   r   join_tokensU  s    ztemplatize.<locals>.join_tokensZ
endcommentTz # %sz #
z, npgettext({p}{!r}, {p}{!r}, {p}{!r},count) r)   z# ngettext({p}{!r}, {p}{!r}, count) SPz pgettext({p}{!r}, {p}{!r}) z gettext({p}{!r}) z	file %s, zDTranslation blocks must not include other block tags: %s (%sline %d)z%%(%s)s%z%%rs   zjThe translator-targeted comment '%s' (%sline %d) was ignored, because it wasn't the last item on the line.z# %sz | r"   "'r#   z _(%s) trimmedcommentB|z:_(z %s :FX)/Zdjango.template.baser   r   r   r   r   r   r   r   ZFILE_CHARSETr   r   PY3tokenize
token_typecontentsrP   	enumerate
splitlineslstriprV   writer   endblock_rer   	plural_reformatr   SyntaxErrorlinenorv   countr8   r9   r   	inline_reblock_reconstant_refindallr   strip
context_reZsplit_contentsrn   r$   
setdefaultgetvalue)+r   originr   r   r   r   r   r   outZmessage_contextZintransZinpluralr   r   rY   Z	incommentr   Zlineno_comment_mapZcomment_lineno_cacheZ
raw_prefixr   rl   contentZtranslators_comment_startr   lineZ	endbmatchZpluralmatchpartZfilemsgr   Z
cur_linenocZwarn_msgZimatchZbmatchZcmatchesgZcontext_matchZfmatchZcmatchpartsr)   r   r   r   
templatize;  s6   .""	

						$r   c             C   s   g  } t  j |  j    } | d
 r) g  Sx t d t |  d d  D] } | | | d  \ } } } | rs g  S| r y t |  } Wq t k
 r g  SYq Xn  | s d } n  | j | | f  qF W| j d d d   d d	  | S)z
    Parses the lang_string, which is the body of an HTTP Accept-Language
    header, and returns a list of (lang, q-value), ordered by 'q' values.

    Any format errors in lang_string results in an empty list being returned.
    r"   r      g      ?keyc             S   s   |  d S)Nr"   r   )kr   r   r   rh     s    z*parse_accept_lang_header.<locals>.<lambda>reverseT)	accept_language_rern   r%   ranger&   float
ValueErrorr   sort)Zlang_stringr|   piecesifirstro   priorityr   r   r   r     s$    
#	r   )Xr`   
__future__r   r~   r/   rN   rerJ   r8   collectionsr   	threadingr   Zdjango.appsr   Zdjango.confr   Zdjango.conf.localer   Zdjango.core.exceptionsr   Zdjango.core.signalsr	   Zdjango.dispatchr
   Zdjango.utilsr   r   Zdjango.utils._osr   Zdjango.utils.encodingr   Zdjango.utils.safestringr   r   Zdjango.utils.sixr   Zdjango.utils.translationr   r   r   ra   rb   rq   r   compileVERBOSEr   
IGNORECASEr   r   r   r*   r,   r0   r-   rE   rd   rf   rj   rm   rp   rr   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   		

	
		

				"/	