
[c           @` sW  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l m	 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	 a i  a e t g  a e a d
 Z d   Z d   Z d d  Z d   Z  d   Z! d e" f d     YZ# d e# f d     YZ$ d e# f d     YZ% d S(   s  Translation methods for generating localized strings.

To load a locale and generate a translated string::

    user_locale = tornado.locale.get("es_LA")
    print(user_locale.translate("Sign out"))

`tornado.locale.get()` returns the closest matching locale, not necessarily the
specific locale you requested. You can support pluralization with
additional arguments to `~Locale.translate()`, e.g.::

    people = [...]
    message = user_locale.translate(
        "%(list)s is online", "%(list)s are online", len(people))
    print(message % {"list": user_locale.list(people)})

The first string is chosen if ``len(people) == 1``, otherwise the second
string is chosen.

Applications should call one of `load_translations` (which uses a simple
CSV format) or `load_gettext_translations` (which uses the ``.mo`` format
supported by `gettext` and related tools).  If neither method is called,
the `Locale.translate` method will simply return the original string.
i    (   t   absolute_importt   divisiont   print_functionN(   t   BytesIO(   t   escape(   t   gen_log(   t   PY3(   t   LOCALE_NAMESt   en_USs   c          G` s   t  j |    S(   s  Returns the closest match for the given locale codes.

    We iterate over all given locale codes in order. If we have a tight
    or a loose match for the code (e.g., "en" for "en_US"), we return
    the locale. Otherwise we move to the next code in the list.

    By default we return ``en_US`` if no translations are found for any of
    the specified locales. You can change the default locale with
    `set_default_locale()`.
    (   t   Localet   get_closest(   t   locale_codes(    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   getA   s    c         C` s)   |  a  t t t j    t  g  a d S(   s7  Sets the default locale.

    The default locale is assumed to be the language used for all strings
    in the system. The translations loaded from disk are mappings from
    the default locale to the destination locale. Consequently, you don't
    need to create a translation file for the default locale.
    N(   t   _default_localet	   frozensett   listt   _translationst   keyst   _supported_locales(   t   code(    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   set_default_localeO   s    
c      
   C` s  i  a  xit j |   D]X} | j d  s1 q n  | j d  \ } } t j d |  s t j d | t j	 j
 |  |   q n  t j	 j
 |  |  } | d k r t | d   } | j t t j   } Wd QX| t j t j f k r d } q d } n  t rt | d	 d
 | } nS t   } t j | d	 d
 | # } | j t j | j     Wd QX| j d  i  t  | <x t t j |   D] \ }	 }
 |
 st |
  d k  rqn  g  |
 D] } t j |  j   ^ q}
 |
 d  \ } } t |
  d k r|
 d pd } n d } | d k rFt j d | | |	 d  qn  | t  | j | i   | <qW| j   q Wt t  t  j!    t" g  a# t j$ d t% t#   d S(   sw  Loads translations from CSV files in a directory.

    Translations are strings with optional Python-style named placeholders
    (e.g., ``My name is %(name)s``) and their associated translations.

    The directory should have translation files of the form ``LOCALE.csv``,
    e.g. ``es_GT.csv``. The CSV files should have two or three columns: string,
    translation, and an optional plural indicator. Plural indicators should
    be one of "plural" or "singular". A given string can have both singular
    and plural forms. For example ``%(name)s liked this`` may have a
    different verb conjugation depending on whether %(name)s is one
    name or a list of names. There should be two rows in the CSV file for
    that string, one with plural indicator "singular", and one "plural".
    For strings with no verbs that would change on translation, simply
    use "unknown" or the empty string (or don't include the column at all).

    The file is read using the `csv` module in the default "excel" dialect.
    In this format there should not be spaces after the commas.

    If no ``encoding`` parameter is given, the encoding will be
    detected automatically (among UTF-8 and UTF-16) if the file
    contains a byte-order marker (BOM), defaulting to UTF-8 if no BOM
    is present.

    Example translation ``es_LA.csv``::

        "I love you","Te amo"
        "%(name)s liked this","A %(name)s les gustó esto","plural"
        "%(name)s liked this","A %(name)s le gustó esto","singular"

    .. versionchanged:: 4.3
       Added ``encoding`` parameter. Added support for BOM-based encoding
       detection, UTF-16, and UTF-8-with-BOM.
    s   .csvt   .s   [a-z]+(_[A-Z]+)?$s!   Unrecognized locale %r (path: %s)t   rbNs   utf-16s	   utf-8-sigt   rt   encodingi    i   t   unknownt   pluralt   singulars.   Unrecognized plural indicator %r in %s line %di   s   Supported locales: %s(   R   R   R   (&   R   t   ost   listdirt   endswitht   splitt   ret   matchR   t   errort   patht   joint   Nonet   opent   readt   lent   codecst   BOM_UTF16_LEt   BOM_UTF16_BER   R   t   writeR   t   utf8t   seekt	   enumeratet   csvt   readert
   to_unicodet   stript
   setdefaultt   closeR   R   R   R   R   t   debugt   sorted(   t	   directoryR   R#   t   localet	   extensiont	   full_patht   ft   datat   infilet   it   rowt   ct   englisht   translationR   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   load_translations]   sN    %			"
"(	c         C` s"  d d l  } i  a x t j |   D] } | j d  r= q" n  t j j t j j |  |   rd q" n  yI t j t j j |  | d | d   | j	 | |  d | g t | <Wq" t
 k
 r } t j d | t |   q" q" Xq" Wt t t j    t g  a t a t j d t t   d S(	   s  Loads translations from `gettext`'s locale tree

    Locale tree is similar to system's ``/usr/share/locale``, like::

        {directory}/{lang}/LC_MESSAGES/{domain}.mo

    Three steps are required to have your app translated:

    1. Generate POT translation file::

        xgettext --language=Python --keyword=_:1,2 -d mydomain file1.py file2.html etc

    2. Merge against existing POT file::

        msgmerge old.po mydomain.po > new.po

    3. Compile::

        msgfmt mydomain.po -o {directory}/pt_BR/LC_MESSAGES/mydomain.mo
    i    NR   t   LC_MESSAGESs   .mot	   languagess$   Cannot load translation for '%s': %ss   Supported locales: %s(   t   gettextR   R   R   t
   startswithR#   t   isfileR$   t   statRC   t	   ExceptionR   R"   t   strR   R   R   R   R   t   Truet   _use_gettextR6   R7   (   R8   t   domainRG   t   langt   e(    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   load_gettext_translations   s"    !&c           C` s   t  S(   s1   Returns a list of all the supported locale codes.(   R   (    (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   get_supported_locales   s    R	   c           B` s   e  Z d  Z e d    Z e d    Z d   Z d d d  Z d d d  Z	 d e
 e e d  Z d e
 d  Z d	   Z d
   Z RS(   s   Object representing a locale.

    After calling one of `load_translations` or `load_gettext_translations`,
    call `get` or `get_closest` to get a Locale object.
    c         G` s   x | D] } | s q n  | j  d d  } | j d  } t |  d k rR q n7 t |  d k r | d j   d | d j   } n  | t k r |  j |  S| d j   t k r |  j | d j    Sq W|  j t  S(   s4   Returns the closest match for the given locale code.t   -t   _i   i    i   (   t   replaceR   R(   t   lowert   upperR   R   R   (   t   clsR   R   t   parts(    (    s-   lib/python2.7/site-packages/tornado/locale.pyR
      s    %c         C` s   t  |  d  s i  |  _ n  | |  j k r | t k s< t  t j | d  } | d k rl t | i   } n' t r t	 | |  } n t | |  } | |  j | <n  |  j | S(   sn   Returns the Locale for the given locale code.

        If it is not supported, we raise an exception.
        t   _cacheN(
   t   hasattrR[   R   t   AssertionErrorR   R   R%   t	   CSVLocaleRN   t   GettextLocale(   RY   R   t   translationsR9   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR      s    c         C` s?  | |  _  t j | i   j d d  |  _ t |  _ x6 d d d g D]% } |  j  j |  rC t |  _ PqC qC W| |  _ |  j	 } | d  | d  | d  | d	  | d
  | d  | d  | d  | d  | d  | d  | d  g |  _
 | d  | d  | d  | d  | d  | d  | d  g |  _ d  S(   Nt   nameu   Unknownt   fat   art   het   Januaryt   Februaryt   Marcht   Aprilt   Mayt   Junet   Julyt   Augustt	   Septembert   Octobert   Novembert   Decembert   Mondayt   Tuesdayt	   Wednesdayt   Thursdayt   Fridayt   Saturdayt   Sunday(   R   R   R   Ra   t   Falset   rtlRH   RM   R`   t	   translatet   _monthst	   _weekdays(   t   selfR   R`   t   prefixRU   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   __init__  s    	!				$$-$c         C` s   t     d S(   s&  Returns the translation for the given message for this locale.

        If ``plural_message`` is given, you must also provide
        ``count``. We return ``plural_message`` when ``count != 1``,
        and we return the singular form for the given message when
        ``count == 1``.
        N(   t   NotImplementedError(   R}   t   messaget   plural_messaget   count(    (    s-   lib/python2.7/site-packages/tornado/locale.pyRz   '  s    c         C` s   t     d  S(   N(   R   (   R}   t   contextR   R   R   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   pgettext1  s    i    c         C` s]  t  | t j  r' t j j |  } n  t j j   } | | k rm | rd | | j d k  rd | } qm t } n  | t j d |  } | t j d |  } | t j d d  }	 | | }
 |
 j } |
 j	 } |  j
 } d+ } | s8| r{| d k r{| d k  r| d d |  i | d	 6S| d, k  rPt | d
  } | d d |  i | d 6St | d-  } | d d |  i | d 6S| d k r| d  } q8| d k r| j |	 j k r| r| r| d  n	 | d  } q8| d k  r| r| d  n	 | d  } q8| d k  r8| r&| d  n	 | d  } q8n  | d+ k re| rV| d  n	 | d  } n  |  j d. k } | rd | j | j f } nr |  j d k rd d/ | j d" k | j d" pd" | j f } n0 d# | j d" pd" | j d0 | j d" k f } | i |  j | j d d& 6|  j | j   d' 6t | j  d( 6t | j  d) 6| d* 6S(1   s  Formats the given date (which should be GMT).

        By default, we return a relative time (e.g., "2 minutes ago"). You
        can return an absolute date string with ``relative=False``.

        You can force a full format date ("July 10, 1980") with
        ``full_format=True``.

        This method is primarily intended for dates in the past.
        For dates in the future, we fall back to full format.
        i<   t   minutest   hoursi   i    i2   s   1 second agos   %(seconds)d seconds agot   secondsg      N@s   1 minute agos   %(minutes)d minutes agos
   1 hour agos   %(hours)d hours agos   %(time)si   t	   yesterdays   yesterday at %(time)si   s   %(weekday)ss   %(weekday)s at %(time)siN  s   %(month_name)s %(day)ss"   %(month_name)s %(day)s at %(time)ss    %(month_name)s %(day)s, %(year)ss,   %(month_name)s %(day)s, %(year)s at %(time)st   enR   t   zh_CNs   %d:%02ds	   %s%d:%02du   上午u   下午i   s
   %d:%02d %st   amt   pmt
   month_namet   weekdayt   dayt   yeart   timeNi  g      @(   R   R   R   (   u   上午u   下午(   R   R   (   t
   isinstancet   numberst   Realt   datetimet   utcfromtimestampt   utcnowR   RM   t	   timedeltat   daysRz   R%   t   roundR   R   t   hourt   minuteR{   t   monthR|   R   RL   R   (   R}   t   datet
   gmt_offsett   relativet   shortert   full_formatt   nowt
   local_datet	   local_nowt   local_yesterdayt
   differenceR   R   RU   t   formatR   R   t   tfhour_clockt   str_time(    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   format_date4  st    		
						 c         C` s   | t  j d |  } |  j } | rl | d  i |  j | j d d 6|  j | j   d 6t | j  d 6S| d  i |  j | j d d 6t | j  d 6Sd S(	   s   Formats the given date as a day of week.

        Example: "Monday, January 22". You can remove the day of week with
        ``dow=False``.
        R   s#   %(weekday)s, %(month_name)s %(day)si   R   R   R   s   %(month_name)s %(day)sN(	   R   R   Rz   R{   R   R|   R   RL   R   (   R}   R   R   t   dowR   RU   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt
   format_day  s    	c         C` s   |  j  } t |  d k r d St |  d k r9 | d S|  j j d  rQ d n d } | d  i | j | d   d	 6| t |  d d
 6S(   s   Returns a comma-separated list for the given list of parts.

        The format is, e.g., "A, B and C", "A and B" or just "A" for lists
        of size 1.
        i    t    i   Rb   u    و u   , s   %(commas)s and %(last)sit   commast   last(   Rz   R(   R   RH   R$   (   R}   RZ   RU   t   comma(    (    s-   lib/python2.7/site-packages/tornado/locale.pyR     s    	c         C` sf   |  j  d k r t |  St |  } g  } x% | rR | j | d  | d  } q. Wd j t |   S(   s7   Returns a comma-separated number for the given integer.R   R   it   ,(   R   R   (   R   RL   t   appendR$   t   reversed(   R}   t   valueRZ   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   friendly_number  s    
	N(   t   __name__t
   __module__t   __doc__t   classmethodR
   R   R   R%   Rz   R   RM   Rx   R   R   R   R   (    (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR	      s   	
	U	R^   c           B` s,   e  Z d  Z d d d  Z d d d  Z RS(   s=   Locale implementation using tornado's CSV translation format.c         C` s   | d  k	 r` | d  k	 s t  | d k rH | } |  j j d i   } qu |  j j d i   } n |  j j d i   } | j | |  S(   Ni   R   R   R   (   R%   R]   R`   R   (   R}   R   R   R   t   message_dict(    (    s-   lib/python2.7/site-packages/tornado/locale.pyRz     s    c         C` s,   |  j  r t j d  n  |  j | | |  S(   Ns&   pgettext is not supported by CSVLocale(   R`   R   t   warningRz   (   R}   R   R   R   R   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR     s    	N(   R   R   R   R%   Rz   R   (    (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR^     s   R_   c           B` s5   e  Z d  Z d   Z d d d  Z d d d  Z RS(   s1   Locale implementation using the `gettext` module.c         C` se   y | j  |  _ | j |  _ Wn) t k
 rG | j |  _ | j |  _ n Xt t |   j | |  d  S(   N(   t	   ungettextt   ngettextt   ugettextRG   t   AttributeErrort   superR_   R   (   R}   R   R`   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR     s    c         C` sB   | d  k	 r1 | d  k	 s t  |  j | | |  S|  j |  Sd  S(   N(   R%   R]   R   RG   (   R}   R   R   R   (    (    s-   lib/python2.7/site-packages/tornado/locale.pyRz     s    c         C` s   | d k	 r~ | d k	 s t  d | t | f d | t | f | f } |  j |   } t | k rz |  j | | |  } n  | Sd | t | f } |  j |  } t | k r | } n  | Sd S(   s?  Allows to set context for translation, accepts plural forms.

        Usage example::

            pgettext("law", "right")
            pgettext("good", "right")

        Plural message example::

            pgettext("organization", "club", "clubs", len(clubs))
            pgettext("stick", "club", "clubs", len(clubs))

        To generate POT file with context, add following options to step 1
        of `load_gettext_translations` sequence::

            xgettext [basic options] --keyword=pgettext:1c,2 --keyword=pgettext:1c,2,3

        .. versionadded:: 4.2
        s   %s%s%sN(   R%   R]   t   CONTEXT_SEPARATORR   RG   (   R}   R   R   R   R   t   msgs_with_ctxtt   resultt   msg_with_ctxt(    (    s-   lib/python2.7/site-packages/tornado/locale.pyR     s    		N(   R   R   R   R   R%   Rz   R   (    (    (    s-   lib/python2.7/site-packages/tornado/locale.pyR_     s   	(&   R   t
   __future__R    R   R   R)   R0   R   t   ioR   R   R   R    t   tornadoR   t   tornado.logR   t   tornado.utilR   t   tornado._locale_dataR   R   R   R   R   Rx   RN   R   R   R   R%   RD   RR   RS   t   objectR	   R^   R_   (    (    (    s-   lib/python2.7/site-packages/tornado/locale.pyt   <module>(   s2   		Z	+	