Ńň
ÁŁvLc        	   @   sŐ   d  Z  d d k Z d d k Z d d k l Z d d k l Z d d k l Z d d k	 l
 Z
 d d k l Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d S(   sZ   overrides and support functions for enabling Live Translation and
arbitrary locale supporti˙˙˙˙N(   t   translation(   t
   trans_real(   t   lazy(   t   gettext(   t   datac         C   s   t  i |   } g  } xy | D]q } t i i |  oX t  i i t  i i |  |   o6 | i t	 i
 |  t i i | | f  d f  q q W| S(   s4   generates supported languages list from mo directoryi    (   t   ost   listdirR   t   langcode_ret   matcht   patht   isdirt   joint   appendR   t   to_languaget	   languagest   get(   t   locale_patht   dirst   langst   lang(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   find_languages$   s     5:c       
   C   s   d d k  l }  |  i oq yW d d k l } g  } | i i d d  D]" } | t i | i	  | i
 f qG ~ SWq t j
 o q Xn |  i S(   sH   returns list of locales supported adapting to live translation
    statei˙˙˙˙(   t   settings(   t   Languaget   codet   template(   t   django.confR   t   LIVE_TRANSLATIONt   pootle_language.modelsR   t   objectst   excludeR   R   R   t   fullnamet	   Exceptiont	   LANGUAGES(   R   R   t   _[1]t   language(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   supported_langs.   s    
G	c          C   s°   g  }  x t    D]| \ } } t i t i d   |  } t i t i |   |  } | | j o d | | f } n |  i | | f  q W|  i d t i d d    |  S(   s5   generated locale choices for drop down lists in formst   enu
   %s | â­%st   cmpt   keyc         S   s   t  |  d   S(   i   (   t   unicode(   t   choice(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   <lambda>L   s    (	   R#   R   t   tr_langR    t	   to_localeR   t   sortt   localet   strcoll(   t   choicesR   t   namet   tr_name(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   lang_choices?   s    
 c         C   sF   t  |  d  o2 |  i i d d   } | o | | j o | Sn d  S(   Nt   sessiont   django_language(   t   hasattrR3   R   t   None(   t   requestt	   supportedt	   lang_code(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_lang_from_sessionP   s
    	c         C   sF   d d k  l } |  i i | i  } | o | | j o | Sd Sd S(   sJ   See if the user's browser sent a cookie with a her preferred
    language.i˙˙˙˙(   R   N(   R   R   t   COOKIESR   t   LANGUAGE_COOKIE_NAMER6   (   R7   R8   R   R9   (    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_lang_from_cookieW   s
    c         C   sI   |  i  i   o5 |  i  i   } | i o | i | j o | i Sn d S(   s   If the current user is logged in, get her profile model object
    and check whether she has set her preferred interface language.N(   t   usert   is_authenticatedt   get_profilet   ui_langR6   (   R7   R8   t   profile(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_lang_from_prefsa   s
    c         C   sź   |  i  i d d  } x  t i |  D] \ } } | d j o d St i t i | |   } | d j o d S| | j o | Sx/ | i   D]! } | t i |  j o | Sq Wq% Wd S(   s+  If the user's browser sends a list of preferred languages in the
    HTTP_ACCEPT_LANGUAGE header, parse it into a list. Then walk through
    the list, and for each entry, we check whether we have a matching
    pootle translation project. If so, we return it.

    If nothing is found, return None.t   HTTP_ACCEPT_LANGUAGEt    t   *s   en-usR$   N(   s   en-uss   en(	   t   METAR   R   t   parse_accept_lang_headerR6   R   t   normalize_codet   simplify_to_commont   keys(   R7   R8   t   acceptt   accept_langt   unusedt
   normalizedR   (    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_lang_from_http_headerm   s      c         C   sd   t  t    } x; t t t t f D]' } | |  |  } | d j	 o | Sq" Wd d k l } | i	 S(   s÷   Try to get the user's preferred language by first checking the
    cookie, then the user's preferences (stored in the PootleProfile
    model) and finally by checking the HTTP language headers.

    If all fails, try fall back to default language.i˙˙˙˙(   R   N(
   t   dictR#   R:   R=   RC   RP   R6   R   R   t   LANGUAGE_CODE(   R7   R8   t   lang_getterR   R   (    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_language_from_request   s     	c         C   sf   t  i i |  d  } | d j	 o | St  i   } | i |   h  | _ d   | _ | t  i |  <| S(   s\   return dumy translation object to please django's l10n while
    Live Translation is enabledc         S   s   |  S(    (    (   t   x(    (    s'   /var/www/Pootle/pootle/i18n/override.pyR)   ¤   s    N(   R   t   _translationsR   R6   t   DjangoTranslationt   set_languaget   _catalogt   plural(   R"   t   tt
   dummytrans(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   translation_dummy   s    	c         C   s   |  i  t _  |  i t _ |  i t _ |  i t _ t |  i  t  t _ t |  i t  t _	 t |  i t  t _
 t |  i t  t _ d S(   s9   replace django's translation functions with safe versionsN(   R   R    t   ugettextt   ngettextt	   ungettextR   t   strt   gettext_lazyR'   t   ugettext_lazyt   ngettext_lazyt   ungettext_lazy(   t   real_translation(    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   override_gettext¨   s    c           C   s   t  i t i    d j S(   sN   override for django's get_language_bidi that's aware of more
    RTL languagest   rtl(   R   t   language_dirR    t   get_language(    (    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   get_language_bidił   s    (   t   __doc__R-   R   t   django.utilsR    t   django.utils.translationR   t   django.utils.functionalR   t   pootle.i18nR   t   translate.langR   R   R#   R2   R:   R=   RC   RP   RT   R]   Rg   Rk   (    (    (    s'   /var/www/Pootle/pootle/i18n/override.pyt   <module>   s$   	
				
					