src/pyams_i18n/negotiator.py
changeset 0 9c21b8e2ba46
child 4 a80dffc48eb6
equal deleted inserted replaced
-1:000000000000 0:9c21b8e2ba46
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 # import standard library
       
    16 
       
    17 # import interfaces
       
    18 from pyams_i18n.interfaces import INegotiator, LANGUAGE_CACHE_KEY
       
    19 from zope.i18n.interfaces import INegotiator as IZopeNegotiator
       
    20 
       
    21 # import packages
       
    22 from pyams_utils.i18n import get_browser_language
       
    23 from pyams_utils.registry import query_utility, utility_config
       
    24 from persistent import Persistent
       
    25 from zope.container.contained import Contained
       
    26 from zope.i18n.locales import locales
       
    27 from zope.interface import implementer
       
    28 from zope.schema.fieldproperty import FieldProperty
       
    29 
       
    30 
       
    31 @implementer(INegotiator)
       
    32 class Negotiator(Persistent, Contained):
       
    33     """Language negotiator utility"""
       
    34 
       
    35     policy = FieldProperty(INegotiator['policy'])
       
    36     server_language = FieldProperty(INegotiator['server_language'])
       
    37     offered_languages = FieldProperty(INegotiator['offered_languages'])
       
    38     cache_enabled = FieldProperty(INegotiator['cache_enabled'])
       
    39 
       
    40     def get_language(self, request):
       
    41         """See :intf:`INegotiator`"""
       
    42         policies = self.policy.split(' --> ')
       
    43         for policy in policies:
       
    44 
       
    45             # check server policy
       
    46             if policy == 'server':
       
    47                 if self.server_language:
       
    48                     return self.server_language
       
    49 
       
    50             # check session policy
       
    51             elif policy == 'session':
       
    52                 if self.cache_enabled:
       
    53                     try:
       
    54                         cached = request.annotations[LANGUAGE_CACHE_KEY]
       
    55                         return cached
       
    56                     except KeyError:
       
    57                         session = request.session
       
    58                         lang = session.get('language')
       
    59                         if lang is not None:
       
    60                             request.annotations[LANGUAGE_CACHE_KEY] = lang
       
    61                             return lang
       
    62                 else:
       
    63                     session = request.session
       
    64                     lang = session.get('language')
       
    65                     if lang is not None:
       
    66                         return lang
       
    67 
       
    68             # check browser policy
       
    69             elif policy == 'browser':
       
    70                 lang = get_browser_language(request)
       
    71                 if lang is not None:
       
    72                     return lang
       
    73 
       
    74         return None
       
    75 
       
    76     def clear_cache(self, request):
       
    77         """Clear cached language value"""
       
    78         try:
       
    79             del request.annotations[LANGUAGE_CACHE_KEY]
       
    80         except KeyError:
       
    81             pass
       
    82 
       
    83 
       
    84 def locale_negotiator(request):
       
    85     """Negotiate language based on server, browser and user settings"""
       
    86     negotiator = query_utility(INegotiator)
       
    87     if negotiator is not None:
       
    88         return negotiator.get_language(request)
       
    89     return get_browser_language(request)
       
    90 
       
    91 
       
    92 def get_locale(request):
       
    93     """Get zope.i18n locale attribute"""
       
    94     return locales.getLocale(request.locale_name)
       
    95 
       
    96 
       
    97 @utility_config(provides=IZopeNegotiator)
       
    98 class ZopeNegotiator(object):
       
    99     """Zope language negotiator"""
       
   100 
       
   101     def getLanguage(self, langs, env):
       
   102         return locale_negotiator(env)