ó
ZËTc        	   @   s‘  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z m Z d  d l Z d  d l m	 Z	 d  d l
 m Z m Z e j e ƒ Z d e f d „  ƒ  YZ d d „ Z d	 „  Z d
 „  Z e	 e ƒ Z d „  Z e	 e ƒ Z i  Z d e d „ Z d d d „ Z e d „ Z d „  Z i  Z  d „  Z! d d „ Z" d d „ Z# d „  Z$ d Z% d a& d a' d d „ Z( d „  Z) d d d d d d d d d g	 Z* d S(    iÿÿÿÿN(   t   NullTranslationst   GNUTranslations(   t   lazify(   t   PY3t   string_typet   LanguageErrorc           B   s   e  Z d  Z RS(   s>   Exception raised when a problem occurs with changing languages(   t   __name__t
   __module__t   __doc__(    (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyR      s   t   _c      
   C   sö  d |  k r% |  j  d d ƒ d }  n  d |  k rJ |  j  d d ƒ d }  n  |  j  | ƒ } | j d ƒ j ƒ  } | j ƒ  s t d | ƒ ‚ n  d } } } | râ t | d ƒ d k râ | d j ƒ  râ | j d ƒ j ƒ  } qâ n  | rat | d ƒ d k r&| d j ƒ  r&| j d ƒ j ƒ  } qat | d ƒ d k ra| d j	 ƒ  ra| j d ƒ } qan  | rÍt | d ƒ d k r‘| d d j	 ƒ  s»t | d ƒ d	 k rÍ| d d j ƒ  rÍ| j ƒ  } qÍn  | ræt d
 |  ƒ ‚ n  | | | | f S(   s¼  
    Took from Babel,
    Parse a locale identifier into a tuple of the form::

      ``(language, territory, script, variant)``

    >>> parse_locale('zh_CN')
    ('zh', 'CN', None, None)
    >>> parse_locale('zh_Hans_CN')
    ('zh', 'CN', 'Hans', None)

    The default component separator is "_", but a different separator can be
    specified using the `sep` parameter:

    :see: `IETF RFC 4646 <http://www.ietf.org/rfc/rfc4646.txt>`_
    t   .i   i    t   @s   expected only letters, got %ri   i   i   i   s#   %r is not a valid locale identifierN(
   t   splitt   popt   lowert   isalphat
   ValueErrort   Nonet   lent   titlet   uppert   isdigit(   t
   identifiert   sept   partst   langt   scriptt	   territoryt   variant(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   _parse_locale   s0    &&&**c         C   s   |  S(   sM   Mark a string for translation without translating it. Returns
    value.
    (    (   t   value(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   gettext_noopE   s    c         C   s*   t  r t j j |  ƒ St j j |  ƒ Sd S(   s¹   Mark a string for translation. Returns the localized unicode
    string of value.

    Mark a string to be localized as follows::

        _('This should be in lots of languages')

    N(   R   t   tgt
   translatort   gettextt   ugettext(   R   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyR#   L   s    	c         C   s6   t  r t j j |  | | ƒ St j j |  | | ƒ Sd S(   sþ  Mark a string for translation. Returns the localized unicode
    string of the pluralized value.

    This does a plural-forms lookup of a message id. ``singular`` is
    used as the message id for purposes of lookup in the catalog, while
    ``n`` is used to determine which plural form to use. The returned
    message is a Unicode string.

    Mark a string to be localized as follows::

        ungettext('There is %(num)d file here', 'There are %(num)d files here',
                  n) % {'num': n}

    N(   R   R    R!   t   ngettextt	   ungettext(   t   singulart   pluralt   n(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyR%   \   s    c   	   	   C   sí   | d k r t } n  | s; | r( t ƒ  St d |  ƒ ‚ n  d } x¥ | D] } | t j j | ƒ f } t j | ƒ } | d k r´ t	 | d ƒ  } t j
 | | | ƒ ƒ } Wd QXn  t j | ƒ } | d k rØ | } qH | j | ƒ qH W| S(   sq   
    Adapted from python translation function in gettext module
    to work with a provided list of mo files
    s'   No translation file found for domain %st   rbN(   R   R   R    R   t   ost   patht   abspatht   _TRANSLATORS_CACHEt   gett   opent
   setdefaultt   copyt   add_fallback(	   t   domaint   mofilest   class_t   fallbackt   resultt   mofilet   keyt   tt   fp(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   _translator_from_mofiless   s$    	!	c      	   K   s`  | r | } n! | r! | j  } n t j  j ƒ  } |  s= t ƒ  Sy | d } Wn. t k
 r{ t j j | d d d ƒ } n X| d j } t	 |  t
 ƒ s¤ |  g }  n  g  } g  } x[ |  D]S }	 t j | d | d |	 g d t ƒ}
 |
 d	 k	 r· | j |
 ƒ | j |	 ƒ q· q· Wy t | | |  } Wn# t k
 rI} t d | ƒ ‚ n X|  | _ | | _ | S(
   sD   Utility method to get a valid translator object from a language namet	   localedirt   pathst   roott   i18nt   packaget	   languagest   alls   IOError: %sN(   t   configR    t   current_confR    t   KeyErrorR*   R+   t   joinR   t
   isinstancet   listt   _gettextt   findt   FalseR   t   appendR<   t   IOErrorR   t   tg_langt   tg_supported_lang(   R   t   tglt	   tg_configt   kwargst   confR=   t
   app_domainR4   t   supported_languagest   lt   moR!   t   ioe(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   _get_translator”   s8    	!$		c         C   s2   |  t  k r t t j d g  ƒ St t j d g  ƒ S(   sq  
    Return the current i18n languages used

    returns ``None`` if no supported language is available (no translations
    are in place) or a list of languages.

    In case ``all`` parameter is ``False`` only the languages for which
    the application is providing a translation are returned. Otherwise
    all the languages preferred by the user are returned.
    RP   RO   (   RL   t   getattrR    R!   (   RC   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   get_lang¿   s    c         K   s1   t  j j j ƒ  } t  j j t |  d | | ƒ S(   sY  Add a fallback language from which words not matched in other
    languages will be translated to.

    This fallback will be associated with the currently selected
    language -- that is, resetting the language via set_lang() resets
    the current fallbacks.

    This function can be called multiple times to add multiple
    fallbacks.
    RQ   (   R    t   request_localt   contextt   _current_objR!   R2   RZ   (   R   RS   RQ   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyR2   Ï   s    c         C   sµ   y t  |  }  Wn  |  } y& d j t d t |  ƒ d  ƒ ƒ }  Wn` t k
 r¢ d |  k r£ y, d j t d t |  d d ƒd  ƒ ƒ }  WqŸ t k
 r› qŸ Xq£ n X|  t  | <n X|  S(   sŒ   Sanitize the language code if the spelling is slightly wrong.

    For instance, 'pt-br' and 'pt_br' should be interpreted as 'pt_BR'.

    R	   i   t   -R   N(   t   sanitized_language_cacheRG   t   filterR   R   R   (   R   t	   orig_lang(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   sanitize_language_codeß   s    &,c         C   sà   |  s t  j j j ƒ  }  n  |  j } | rª | j ƒ  } | j |  j j d d ƒ ƒ } | r} |  j j d ƒ r} d | j	 d <n  | r¡ t
 | t ƒ r§ | g } q§ q° g  } n g  } | j t t |  j j ƒ ƒ t | d |  ƒd S(   s  Set languages from the request header and the session.

    The session language(s) take priority over the request languages.

    Automatically called by tg controllers to setup i18n.
    Should only be manually called if you override controllers function.

    t   lang_session_keyRO   s   beaker.session.tg_avoid_toucht   _sessRQ   N(   R    R]   R^   R_   t   sessiont   accessedR.   RD   R   t   __dict__RH   R   t   extendt   mapRd   t   requestt   plain_languagest   set_temporary_lang(   RQ   t   session_t   session_existedRB   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt
   setup_i18nø   s    			c         C   s€   | s t  j j j ƒ  } n  t |  d | d t ƒ| _ t d t ƒ } | rT | }  n  y t	 |  d | ƒWn t
 k
 r{ n Xd S(   sÙ   Set the current language(s) used for translations without touching
    the session language.

    languages should be a string or a list of strings.
    First lang will be used as main lang, others as fallbacks.

    RQ   R6   RC   N(   R    R]   R^   R_   RZ   t   TrueR!   R\   RL   t   set_formencode_translationR   (   RB   RQ   RV   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyRn     s    
	c         K   sX   t  j j j ƒ  } t |  | ƒ | j rT |  | j | j j d d ƒ <| j j ƒ  n  d S(   sÏ   Set the current language(s) used for translations
    in current call and session.

    languages should be a string or a list of strings.
    First lang will be used as main lang, others as fallbacks.

    Re   RO   N(	   R    R]   R^   R_   Rn   Rg   RD   R.   t   save(   RB   RS   RQ   (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   set_lang7  s
    	t   _MISSING_FORMENCODEc         C   sÉ   t  t k r d St  d k rY y d d l  a  t  j j ƒ  a WqY t k
 rU t a  d SXn  | st t j j	 j
 ƒ  } n  y t j d d |  d t ƒ} Wn# t k
 r¸ } t d | ƒ ‚ n X| | j _ d S(   s/   Set request specific translation of FormEncode.Niÿÿÿÿt
   FormEncodeRB   R=   s   IOError: %s(   t
   formencodet   FormEncodeMissingR   t   apit   get_localedirt	   _localdirt   ImportErrorR    R]   R^   R_   RJ   t   translationRN   R   R!   t   _formencode_translation(   RB   RQ   t   formencode_translationt   error(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyRs   K  s$    c         C   s‚   t  |  ƒ } | |  k r~ y t j j } Wn t t f k
 rG d  } n X| sZ t ƒ  } n  t | d | j	 ƒ } | |  ƒ } n  | S(   NR#   (
   R#   R    R!   R   t   AttributeErrort	   TypeErrorR   R    R[   R"   (   R   t   transt   fetranst   translator_gettext(    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   _formencode_gettextf  s    
Rq   Ru   R\   R2   Rn   R#   t   lazy_ugettextR%   t   lazy_ungettext(+   R1   t   loggingR*   R"   RJ   R    R   R    t   tg.utilR   t
   tg._compatR   R   t	   getLoggerR   t   logt	   ExceptionR   R   R   R#   Rˆ   R%   R‰   R-   R   RL   R<   RZ   Rr   R\   R2   Ra   Rd   Rq   Rn   Ru   Ry   Rx   R|   Rs   R‡   t   __all__(    (    (    se   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/i18n.pyt   <module>   s>   4			!+		 		