B
    `;                 @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZ d dlmZ eZeZdd	 ZG d
d dZdd Zdd Zdd Zdd Zdd ZG dd de jZG dd dZdS )    N)
Pluralizer
Translator)TranslationString)TranslationStringFactory)reify)ILocaleNegotiator
ILocalizerITranslationDirectories)get_current_registryc             C   s   t | dkS )N   )int)n r   T/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/i18n.py<lambda>       r   c               @   s,   e Zd ZdZdd Zd	ddZd
ddZdS )	Localizerz
    An object providing translation and pluralizations related to
    the current request's locale name.  A
    :class:`pyramid.i18n.Localizer` object is created using the
    :func:`pyramid.i18n.get_localizer` function.
    c             C   s   || _ || _d | _d | _d S )N)locale_nametranslations
pluralizer
translator)selfr   r   r   r   r   __init__   s    zLocalizer.__init__Nc             C   s&   | j dkrt| j| _ | j |||dS )a@  
        Translate a :term:`translation string` to the current language
        and interpolate any *replacement markers* in the result.  The
        ``translate`` method accepts three arguments: ``tstring``
        (required), ``domain`` (optional) and ``mapping`` (optional).
        When called, it will translate the ``tstring`` translation
        string using the current locale.  If the current locale could not be
        determined, the result of interpolation of the default value is
        returned.  The optional ``domain`` argument can be used to specify
        or override the domain of the ``tstring`` (useful when ``tstring``
        is a normal string rather than a translation string).  The optional
        ``mapping`` argument can specify or override the ``tstring``
        interpolation mapping, useful when the ``tstring`` argument is
        a simple string instead of a translation string.

        Example::

           from pyramid.i18n import TranslationString
           ts = TranslationString('Add ${item}', domain='mypackage',
                                  mapping={'item':'Item'})
           translated = localizer.translate(ts)

        Example::

           translated = localizer.translate('Add ${item}', domain='mypackage',
                                            mapping={'item':'Item'})

        N)domainmapping)r   r   r   )r   Ztstringr   r   r   r   r   	translate#   s    
zLocalizer.translatec             C   s*   | j dkrt| j| _ | j |||||dS )a  
        Return a string translation by using two
        :term:`message identifier` objects as a singular/plural pair
        and an ``n`` value representing the number that appears in the
        message using gettext plural forms support.  The ``singular``
        and ``plural`` objects should be strings. There is no
        reason to use translation string objects as arguments as all
        metadata is ignored.

        ``n`` represents the number of elements. ``domain`` is the
        translation domain to use to do the pluralization, and ``mapping``
        is the interpolation mapping that should be used on the result. If
        the ``domain`` is not supplied, a default domain is used (usually
        ``messages``).

        Example::

           num = 1
           translated = localizer.pluralize('Add ${num} item',
                                            'Add ${num} items',
                                            num,
                                            mapping={'num':num})

        If using the gettext plural support, which is required for
        languages that have pluralisation rules other than n != 1, the
        ``singular`` argument must be the message_id defined in the
        translation file. The plural argument is not used in this case.

        Example::

           num = 1
           translated = localizer.pluralize('item_plural',
                                            '',
                                            num,
                                            mapping={'num':num})


        N)r   r   )r   r   r   )r   singularpluralr   r   r   r   r   r   	pluralizeD   s    '
zLocalizer.pluralize)NN)NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   
!r   c             C   s<   d}t | |d}|dkr8| j|}|dkr8| j|}|S )a  The default :term:`locale negotiator`.  Returns a locale name
    or ``None``.

    - First, the negotiator looks for the ``_LOCALE_`` attribute of
      the request object (possibly set by a view or a listener for an
      :term:`event`). If the attribute exists and it is not ``None``,
      its value will be used.

    - Then it looks for the ``request.params['_LOCALE_']`` value.

    - Then it looks for the ``request.cookies['_LOCALE_']`` value.

    - Finally, the negotiator returns ``None`` if the locale could not
      be determined via any of the previous checks (when a locale
      negotiator returns ``None``, it signifies that the
      :term:`default locale name` should be used.)
    Z_LOCALE_N)getattrparamsgetcookies)requestnamer   r   r   r   default_locale_negotiatorr   s    r)   c             C   s^   y
| j }W n tk
r$   t }Y nX |jttd}|| }|dkrZ|jpLi }|dd}|S )zUNegotiate and return the :term:`locale name` associated with
    the current request.)defaultNZdefault_locale_nameen)registryAttributeErrorr
   queryUtilityr   r)   settingsr%   )r'   r,   Z
negotiatorr   r/   r   r   r   negotiate_locale_name   s    


r0   c             C   s   | j S )z
    .. deprecated:: 1.5
        Use :attr:`pyramid.request.Request.locale_name` directly instead.
        Return the :term:`locale name` associated with the current request.
    )r   )r'   r   r   r   get_locale_name   s    r1   c             C   s2  t  }i |_g }d| kr(| dd g}||  x|D ]}g }x8|D ]0}tjtj||}tj|rF|| qFW x|D ]}tj|d}	tjtj|	sqxtt	|	D ]f}
tjtj|	|
}|

drtj|rt|d&}|
dd }t ||}|| W dQ R X qW qW q8W t| |dS )	zCreate a :class:`pyramid.i18n.Localizer` object
    corresponding to the provided locale name from the
    translations found in the list of translation directories._r   LC_MESSAGESz.morbN)r   r   )Translations_catalogsplitappendospathrealpathjoinisdirlistdirendswithisfileopenaddr   )current_locale_nameZtranslation_directoriesr   Zlocales_to_tryZtdirZlocale_dirslnameZldirZ
locale_dirZmessages_dirmofileZmopathZmofpr   Zdtransr   r   r   make_localizer   s2    





 rG   c             C   s   | j S )z
    .. deprecated:: 1.5
        Use the :attr:`pyramid.request.Request.localizer` attribute directly
        instead.  Retrieve a :class:`pyramid.i18n.Localizer` object
        corresponding to the current request's locale name.
    )	localizer)r'   r   r   r   get_localizer   s    rI   c               @   s   e Zd ZdZdZdefddZeddefddZdd	 ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )r6   z>An extended translation catalog class (ripped off from Babel) messagesNc             C   s@   t | _tjj| |d ttdt|ddg| _|| _	i | _
dS )zInitialize the translations catalog.

        :param fileobj: the file-like object the translation should be read
                        from
        )fpNr(   )DEFAULT_PLURALr   gettextGNUTranslationsr   listfilterr#   filesr   _domains)r   fileobjr   r   r   r   r      s
    zTranslations.__init__c          	   C   sp   |dk	r*t |ttfs|g}dd |D }|s4| j}t|||}|sNt S t|d}| ||dS Q R X dS )a  Load translations from the given directory.

        :param dirname: the directory containing the ``MO`` files
        :param locales: the list of locales in order of preference (items in
                        this list can be either `Locale` objects or locale
                        strings)
        :param domain: the message domain
        :return: the loaded catalog, or a ``NullTranslations`` instance if no
                 matching translations were found
        :rtype: `Translations`
        Nc             S   s   g | ]}t |qS r   )str).0localer   r   r   
<listcomp>
  s    z%Translations.load.<locals>.<listcomp>r4   )rS   r   )
isinstancerO   tupleDEFAULT_DOMAINrM   findNullTranslationsrB   )clsdirnameZlocalesr   filenamerK   r   r   r   load   s    zTranslations.loadc             C   s   dt | j| jdf S )Nz
<%s: "%s">zproject-id-version)typer   _infor%   )r   r   r   r   __repr__  s    zTranslations.__repr__Tc             C   s~   t |d| j}|| jkr*| jtkr*|j| _|rB|| jkrB| |S | j|}|rf|dk	rf|| n||  || j|< | S )a  Add the given translations to the catalog.

        If the domain of the translations is different than that of the
        current catalog, they are added as a catalog that is only accessible
        by the various ``d*gettext`` functions.

        :param translations: the `Translations` instance with the messages to
                             add
        :param merge: whether translations for message domains that have
                      already been added should be merged with the existing
                      translations
        :return: the `Translations` instance (``self``) so that `merge` calls
                 can be easily chained
        :rtype: `Translations`
        r   N)	r#   rZ   r   rL   r   mergerR   r%   add_fallback)r   r   rd   r   existingr   r   r   rC     s    


zTranslations.addc             C   s6   t |tjr2| j|j t |tr2| j|j | S )a  Merge the given translations into the catalog.

        Message translations in the specified catalog override any messages
        with the same identifier in the existing catalog.

        :param translations: the `Translations` instance with the messages to
                             merge
        :return: the `Translations` instance (``self``) so that `merge` calls
                 can be easily chained
        :rtype: `Translations`
        )rX   rM   rN   r7   updater6   rQ   extend)r   r   r   r   r   rd   9  s
    
zTranslations.mergec             C   s   | j || |S )zULike ``gettext()``, but look the message up in the specified
        domain.
        )rR   r%   rM   )r   r   messager   r   r   dgettextL  s    zTranslations.dgettextc             C   s   | j || |S )zVLike ``lgettext()``, but look the message up in the specified
        domain.
        )rR   r%   lgettext)r   r   ri   r   r   r   	ldgettextR  s    zTranslations.ldgettextc             C   s   | j || |S )zVLike ``ugettext()``, but look the message up in the specified
        domain.
        )rR   r%   rM   )r   r   ri   r   r   r   	dugettextX  s    zTranslations.dugettextc             C   s   | j || |||S )zVLike ``ngettext()``, but look the message up in the specified
        domain.
        )rR   r%   ngettext)r   r   r   r   numr   r   r   	dngettext^  s    zTranslations.dngettextc             C   s   | j || |||S )zWLike ``lngettext()``, but look the message up in the specified
        domain.
        )rR   r%   	lngettext)r   r   r   r   ro   r   r   r   
ldngettextd  s    zTranslations.ldngettextc             C   s   | j || |||S )zVLike ``ungettext()`` but look the message up in the specified
        domain.
        )rR   r%   rn   )r   r   r   r   ro   r   r   r   
dungettextj  s    zTranslations.dungettext)T)r   r    r!   r"   rZ   r   classmethodr`   rc   rC   rd   rj   rl   rm   rp   rr   rs   r   r   r   r   r6      s   
 r6   c               @   s$   e Zd Zedd Zedd ZdS )LocalizerRequestMixinc             C   sN   | j }| j}|jt|d}|dkrJ|jtg d}t||}|j|t|d |S )z, Convenience property to return a localizer )r(   N)r*   )r,   r   r.   r   r	   rG   ZregisterUtility)r   r,   rD   rH   Ztdirsr   r   r   rH   r  s    
zLocalizerRequestMixin.localizerc             C   s   t | }|S )N)r0   )r   r   r   r   r   r     s    z!LocalizerRequestMixin.locale_nameN)r   r    r!   r   rH   r   r   r   r   r   ru   q  s   ru   )rM   r:   Ztranslationstringr   r   r   r   Zpyramid.decoratorr   Zpyramid.interfacesr   r   r	   Zpyramid.threadlocalr
   rL   r   r)   r0   r1   rG   rI   rN   r6   ru   r   r   r   r   <module>   s&   ]	,
 