ó
ů˘TQc           @  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 m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z i  a e
   Z d a i  a d Z e j d e j   Z! e j d  Z" e# d  Z$ d   Z% d e j& f d     YZ' d   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z e j0 r¤e Z1 n	 d   Z1 d   Z2 d   Z3 d   Z4 d   Z5 e j0 răe5 Z6 n	 d    Z6 d!   Z7 d"   Z8 d#   Z9 d d$  Z: e# d%  Z; e j d&  Z< d'   Z= e j d(  Z> e j d)  Z? e j d*  Z@ e j d+  ZA e j d,  ZB e j d-  ZC e j d.  ZD d d/  ZE d0   ZF d S(1   u   Translation helper functions.i˙˙˙˙(   t   unicode_literalsN(   t   local(   t   import_module(   t	   force_strt
   force_text(   t   upath(   t	   mark_safet   SafeData(   t   six(   t   StringIOu   u  
        ([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.
        u   ^/([\w-]+)(/|$)c         C  sĘ   |  j  d  } | d k rź | rE |  |  j   d |  | d j   St |  | d  d k r |  |  j   d |  | d j   |  | d j   S|  |  j   d |  | d j   Sn
 |  j   Sd S(   u   
    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).
    u   -i    u   _i   i   N(   t   findt   lowert   lent   upper(   t   languaget   to_lowert   p(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt	   to_locale,   s    $6'c         C  sM   |  j  d  } | d k r? |  |  j   d |  | d j   S|  j   Sd S(   u9   Turns a locale name (en_US) into a language name (en-us).u   _i    u   -i   N(   R
   R   (   t   localeR   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   to_language=   s    $t   DjangoTranslationc           B  sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   u[   
    This class sets up the GNUTranslations context with regard to output
    charset.
    c         O  s9   t  j j |  | |  |  j d  d |  _ d |  _ d  S(   Nu   utf-8u   ??(   t   gettext_modulet   GNUTranslationst   __init__t   set_output_charsett   django_output_charsett   _DjangoTranslation__language(   t   selft   argst   kw(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR   J   s    	c         C  s   |  j  j | j   d  S(   N(   t   _catalogt   update(   R   t   other(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   mergeP   s    c         C  s   | |  _  t |  |  _ d  S(   N(   R   R   t   _DjangoTranslation__to_language(   R   R   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   set_languageS   s    	c         C  s   |  j  S(   N(   R   (   R   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR   W   s    c         C  s   |  j  S(   N(   R"   (   R   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR   Z   s    c         C  s   d |  j  S(   Nu   <DjangoTranslation lang:%s>(   R   (   R   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   __repr__]   s    (	   t   __name__t
   __module__t   __doc__R   R!   R#   R   R   R$   (    (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR   E   s   					c           s    t  j |  d  } | d k	 r" | Sd d l m   t j j t j j t	 t
 j   j j   d   d    f d  } |   j  } | |  d | } | S(   u8  
    Returns a translation object.

    This translation object will be constructed out of multiple GNUTranslations
    objects by merging their catalogs. It will construct a object for the
    requested language and add a fallback to the default language, if it's
    different from the requested language.
    i˙˙˙˙(   t   settingsu   localec   	        sĄ  t  j  d     d  k	 r"  St        f d        d   } |   g  t  D] } | |  ^ qe k r§  j j    _  j j    _ n    f d   } xn t  j  D]] } t	 |  } t
 j j t
 j j t | j   d  } t
 j j |  rÉ | |   qÉ qÉ Wx8 t  j  D]' } t
 j j |  r:| |   q:q:W d  k r| d  k	 r|  qt j   Sn   t   < S(   Nc           sI   y0 t  j d |   g t  } | j    | SWn t k
 rD d  SXd  S(   Nu   django(   R   t   translationR   R#   t   IOErrort   None(   t   patht   t(   t   langt   loc(    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   _translation}   s    c         S  s   |  j  d d  d S(   Nu   -i   i    (   t   split(   t   x(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   <lambda>   s    c           s<     |   } | d  k	 r8  d  k r( | S j |  n   S(   N(   R+   R!   (   R,   R-   (   R0   t   res(    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   _merge   s    u   locale(   t   _translationst   getR+   R   t   _infot   copyR   t   reversedt   INSTALLED_APPSR   t   osR,   t   joint   dirnameR   t   __file__t   isdirt   LOCALE_PATHSR   t   NullTranslations(	   R.   t   fallbackt	   base_langt   transR5   t   appnamet   appt   apppatht
   localepath(   t
   globalpathR(   (   R/   R4   R0   R.   s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   _fetchs   s2    	+	*	
RC   N(   R6   R7   R+   t   django.confR(   R<   R,   R=   R>   R   t   syst   modulesR&   R?   t   LANGUAGE_CODE(   R   R-   RK   t   default_translationt   current_translation(    (   R(   RJ   s<   ../Django//lib/python/django/utils/translation/trans_real.pyR)   `   s    49c         C  s   t  |   t _ d S(   u­   
    Fetches the translation object for a given tuple of application name and
    language and installs it as the current translation object for the current
    thread.
    N(   R)   t   _activet   value(   R   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   activateą   s    c           C  s   t  t d  r t ` n  d S(   u   
    Deinstalls the currently active translation object so that further _ calls
    will resolve against the default translation object, again.
    u   valueN(   t   hasattrRR   RS   (    (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt
   deactivateš   s    c           C  s   t  j   t _ d S(   uˇ   
    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.
    N(   R   RB   RR   RS   (    (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   deactivate_allÁ   s    c          C  sZ   t  t d d  }  |  d k	 rC y |  j   SWqC t k
 r? qC Xn  d d l m } | j S(   u(   Returns the currently selected language.u   valuei˙˙˙˙(   R(   N(   t   getattrRR   R+   R   t   AttributeErrorRL   R(   RO   (   R-   R(   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   get_languageÉ   s    c          C  s3   d d l  m }  t   j d  d } | |  j k S(   ux   
    Returns selected language's BiDi layout.

    * False = left-to-right layout
    * True = right-to-left layout
    i˙˙˙˙(   R(   u   -i    (   RL   R(   RZ   R1   t   LANGUAGES_BIDI(   R(   RD   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   get_language_bidiŐ   s    c          C  sT   t  t d d  }  |  d k	 r" |  St d k rP d d l m } t | j  a n  t S(   uÓ   
    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.
    u   valuei˙˙˙˙(   R(   N(   RX   RR   R+   t   _defaultRL   R(   R)   RO   (   R-   R(   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   catalogá   s    c         C  sĚ   |  j  t d  t d   j  t d  t d   } t t d d  } | d k	 rl t | |  |  } nC t d k r d d l m } t | j	  a n  t t |  |  } t
 |  t  rČ t |  S| S(   u-  
    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.
    u   
u   
u   u   valuei˙˙˙˙(   R(   N(   t   replacet   strRX   RR   R+   R]   RL   R(   R)   RO   t
   isinstanceR   R   (   t   messaget   translation_functiont   eol_messageR-   t   resultR(   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   do_translateń   s    
6
c         C  s   t  |  d  S(   u   
    Returns a string of the translation of the message.

    Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
    u   gettext(   Rf   (   Rb   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   gettext  s    c         C  s   t  |  d  S(   Nu   ugettext(   Rf   (   Rb   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   ugettext  s    c         C  s8   d |  t  | f } t |  } t  | k r4 | } n  | S(   Nu   %s%s%s(   t   CONTEXT_SEPARATORRh   (   t   contextRb   t   msg_with_ctxtRe   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   pgettext  s
    	c         C  s   |  S(   uő   
    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.
    (    (   Rb   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   gettext_noop  s    c         C  s~   t  t d d   } | d  k	 r7 t  | |  |  | |  St d  k re d d l m } t | j  a n  t  t |  |  | |  S(   Nu   valuei˙˙˙˙(   R(   (   RX   RR   R+   R]   RL   R(   R)   RO   (   t   singulart   pluralt   numberRc   R-   R(   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   do_ntranslate'  s    c         C  s   t  |  | | d  S(   uˇ   
    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.
    u   ngettext(   Rq   (   Rn   Ro   Rp   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   ngettext2  s    c         C  s   t  |  | | d  S(   u}   
        Returns a unicode strings of the translation of either the singular or
        plural, based on the number.
        u	   ungettext(   Rq   (   Rn   Ro   Rp   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt	   ungettext>  s    c         C  sZ   d |  t  | f d |  t  | f | f } t |   } t  | k rV t | | |  } n  | S(   Nu   %s%s%s(   Ri   Rs   (   Rj   Rn   Ro   Rp   t   msgs_with_ctxtRe   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt	   npgettextE  s    	c          C  sX   d d l  m }  t j j t j j t t j |  j	 j
   d  } | g t |  j  S(   uC   
    Returns a list of paths to user-provides languages files.
    i˙˙˙˙(   R(   u   locale(   RL   R(   R<   R,   R=   R>   R   RM   RN   R&   R?   t   listRA   (   R(   RJ   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   all_locale_pathsO  s    	+c         C  s@   x9 t    D]. } t j d | t |   g  d k	 r
 t Sq
 Wt S(   u  
    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. This is only used for language codes from either the cookies
    or session and during format localization.
    u   djangoN(   Rw   R   R
   R   R+   t   Truet   False(   t	   lang_codeR,   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   check_for_languageX  s    $c         C  su   | d k r. d d l m } t | j  } n  t j |   } | rq | j d  } | | k rq t |  rq | Sn  d S(   u^   
    Returns the language-code if there is a valid language-code
    found in the `path`.
    i˙˙˙˙(   R(   i   N(	   R+   RL   R(   t   dictt	   LANGUAGESt   language_code_prefix_ret   matcht   groupR{   (   R,   t	   supportedR(   t   regex_matchRz   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   get_language_from_pathd  s    c         C  s3  d d l  m } t | j  } | rJ t |  j |  } | d k	 rJ | Sn  t |  d  r |  j j	 d d  } | | k r | d k	 r t
 |  r | Sn  |  j j	 | j  } | rÖ | | k rÖ | j d  d } n  | rř | | k rř t
 |  rř | S|  j j	 d d  } xt |  D]\ } } | d	 k r6Pn  t j j	 t | t   } | s]qn  | j d
  d } | t k rt | SxĄ | | f | j d  d | j d  d f f D]m \ }	 }
 |	 j   | k rŰqˇn  xF t   D]; } t j j t j j | |
 d d   rĺ|	 t | <|	 SqĺWqˇWqW| j S(   u  
    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.
    i˙˙˙˙(   R(   u   sessionu   django_languageu   -i    u   HTTP_ACCEPT_LANGUAGEu    u   *u   .u   _u   LC_MESSAGESu	   django.moN(   RL   R(   R|   R}   R   t	   path_infoR+   RU   t   sessionR7   R{   t   COOKIESt   LANGUAGE_COOKIE_NAMER1   t   METAt   parse_accept_lang_headerR   t   locale_aliasR   Rx   t	   _acceptedR   Rw   R<   R,   t   existsR=   RO   (   t   requestt
   check_pathR(   R   Rz   t   acceptt   accept_langt   unusedt
   normalizedR.   R>   R,   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   get_language_from_requestr  sD    $	3'
u   \Sc         C  s   t  j | |   S(   uh   
    Changes every non-whitespace character to the given char.
    Used in the templatize function.
    (   t   dot_ret   sub(   t   srct   char(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   blankoutľ  s    u.   ^\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?'))\s*uW   ^\s*trans\s+((?:"[^"]*?")|(?:'[^']*?'))(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?\s*uD   ^\s*blocktrans(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)u   ^\s*endblocktrans$u   ^\s*plural$u   _\(((?:".*?")|(?:'.*?'))\)u   (?<!%)%(?!%)c   $      C  s  d d l  m } d d l m } m } m } m } m } m } t	 |  | j
  }  t   }	 d  }
 t } t } g  } g  } t } g  } xú| |  |  j   D]ă} | r| j | k r| j d k rd j |  } d  } xA t | j t   D]* \ } } | j   j |  rî | } qî qî Wx\ t | j t   D]E \ } } | d  k	 rj| | k rj|	 j d |  q2|	 j d  q2Wt } g  } q|| j | j  q | rö| j | k rft j | j  } t j | j  } | r| r|
 r|	 j d |
 d j |  d j |  f  n) |	 j d	 d j |  d j |  f  x$ | D] } |	 j t | d
   qGWx | D] } |	 j t | d   qnWnj |
 rş|	 j d |
 d j |  f  n |	 j d d j |   x$ | D] } |	 j t | d
   qŰWd  }
 t } t } g  } g  } qó| r+t } qód } | rDd | } n  t d | j | | j f   q|| j | k rŠ| r| j d | j  qó| j d | j  q|| j | k r|t j  d | j  } | ră| j |  qó| j |  q|q | j | k rt! j | j  } t" j | j  } t# j$ | j  } | rT| j% d  } | d d k rr| j& d  } n" | d d k r| j& d  } n  t j  d |  } | j% d  r@t' j | j% d   } | j% d  }
 |
 d d k rţ|
 j& d  }
 n" |
 d d k r |
 j& d  }
 n  |	 j d |
 | f  d  }
 q||	 j d |  q|| r x+ t# j$ | j  D] }  |	 j d |   qm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  t } t } g  } g  } q|| rKxS | D] }! |	 j d |!  q-Wq|| j d k rct } q||	 j t | j d   q | j | k r=| j j( d  }" t# j |" d  }! |! rÖ|	 j d |! j% d   n  xŁ |" d D]U }# |# j) d  d k r |	 j d |# j( d d  d  qá|	 j t |# d   qáWq | j | k rc|	 j d | j  q |	 j t | j d   q Wt* |	 j+    S(!   uş   
    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.
    i˙˙˙˙(   R(   (   t   Lexert
   TOKEN_TEXTt	   TOKEN_VARt   TOKEN_BLOCKt   TOKEN_COMMENTt   TRANSLATOR_COMMENT_MARKu
   endcommentu    u    # %su    #
u    npgettext(%r, %r, %r,count) u    ngettext(%r, %r, count) u   Su   Pu    pgettext(%r, %r) u    gettext(%r) u	   file %s, uD   Translation blocks must not include other block tags: %s (%sline %d)u   %%(%s)su   %%i   i    u   "u   'i   u    _(%s) u   commentu   Bu   |u   :_(u    %s u   :u   Fu   XN(,   RL   R(   t   django.templateR   R   R   R   R   R   R   t   FILE_CHARSETR	   R+   Ry   t   tokenizet
   token_typet   contentsR=   t	   enumeratet
   splitlinesRx   t   lstript
   startswitht   writet   appendt   endblock_reR   t	   plural_reR   t   SyntaxErrort   linenot   one_percent_reR   t	   inline_ret   block_ret   constant_ret   findallR   t   stript
   context_reR1   R
   R   t   getvalue($   R   t   originR(   R   R   R   R   R   R   t   outt   message_contextt   intranst   inpluralRn   Ro   t	   incommentt   commentR-   t   contentt   translators_comment_startR­   t   linet	   endbmatcht   pluralmatcht   partt   filemsgRŁ   t   imatcht   bmatcht   cmatchest   gt   context_matcht   fmatcht   cmatcht   partsR   (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt
   templatizeĹ  sě    .	""	/)#		"			$c         C  sś   g  } t  j |   } | d r# g  Sxs t d t |  d d  D]U } | | | d !\ } } } | rj g  S| r| t |  p d } | j | | f  q@ W| j d d   d t  | S(	   uç   
    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.
    i˙˙˙˙i    i   i   g      đ?t   keyc         S  s   |  d S(   Ni   (    (   t   k(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR3   c  s    t   reverse(   t   accept_language_reR1   t   rangeR   t   floatRŠ   t   sortRx   (   t   lang_stringRe   t   piecest   it   firstR.   t   priority(    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyR   R  s    
#(G   R'   t
   __future__R    R   R<   t   reRM   Rg   R   t	   threadingR   t   django.utils.importlibR   t   django.utils.encodingR   R   t   django.utils._osR   t   django.utils.safestringR   R   t   django.utilsR   t   django.utils.sixR	   R6   RR   R+   R]   R   Ri   t   compilet   VERBOSERĐ   R~   Ry   R   R   R   R   R)   RT   RV   RW   RZ   R\   R^   Rf   t   PY3Rh   Rl   Rm   Rq   Rr   Rs   Ru   Rw   R{   R   R   R   R   R´   RŻ   R°   RŞ   RŤ   Rą   RŽ   RĚ   R   (    (    (    s<   ../Django//lib/python/django/utils/translation/trans_real.pyt   <module>   sv   			Q																					
			B	