Back to index

plone3  3.1.7
Classes | Functions | Variables
PlacelessTranslationService.Negotiator Namespace Reference

Classes

class  BrowserAccept
class  CookieAccept
class  RequestGetAccept
class  Negotiator

Functions

def getAcceptedHelper
def registerLangPrefsMethod
def getLangPrefs
def lang_normalize
def str_lower
def str_strip
def type_accepted
def lang_accepted
def _false
def setCookieLanguage
def negotiate

Variables

dictionary _langPrefsRegistry = {}
tuple negotiator = Negotiator()

Function Documentation

def PlacelessTranslationService.Negotiator._false (   a,
  kw 
) [private]

Definition at line 57 of file Negotiator.py.

00057 
00058 def _false(*a, **kw):
00059     pass
00060 

def PlacelessTranslationService.Negotiator.getAcceptedHelper (   self,
  request,
  kind = 'language' 
)
this is patched on prefs classes which don't define the getAccepted
classes but define the deprecated getPreferredLanguages method

Definition at line 7 of file Negotiator.py.

00007 
00008 def getAcceptedHelper(self, request, kind='language'):
00009     """this is patched on prefs classes which don't define the getAccepted
00010     classes but define the deprecated getPreferredLanguages method"""
00011     return self.getPreferredLanguages()

def PlacelessTranslationService.Negotiator.getLangPrefs (   env,
  kind = 'language' 
)
get higest prio method for kind

Definition at line 24 of file Negotiator.py.

00024 
00025 def getLangPrefs(env, kind='language'):
00026     """get higest prio method for kind"""
00027     for pref in  _langPrefsRegistry[kind]:
00028         handler = pref['klass'](env)
00029         accepted = handler.getAccepted(env, kind)
00030         if accepted:
00031             return accepted
00032     return ()

Here is the caller graph for this function:

def PlacelessTranslationService.Negotiator.lang_accepted (   available,
  preferred 
)

Definition at line 53 of file Negotiator.py.

00053 
00054 def lang_accepted(available, preferred):
00055     # ex: available is pt, preferred is pt-br
00056     return available.startswith(preferred)

filter

Definition at line 33 of file Negotiator.py.

00033 
00034 def lang_normalize(lang):
00035     """filter"""
00036     return lang.replace('_', '-')

Here is the caller graph for this function:

def PlacelessTranslationService.Negotiator.negotiate (   langs,
  request 
)

Definition at line 250 of file Negotiator.py.

00250 
00251 def negotiate(langs, request):
00252     return negotiator.negotiate(langs, request, 'language')
def PlacelessTranslationService.Negotiator.registerLangPrefsMethod (   prefs,
  kind = 'language' 
)

Definition at line 12 of file Negotiator.py.

00012 
00013 def registerLangPrefsMethod(prefs, kind='language'):
00014     # check for correct format of prefs
00015     if type(prefs) is not type({}): prefs = {'klass':prefs,'priority':0}
00016     # add chain for kind
00017     if not _langPrefsRegistry.has_key(kind): _langPrefsRegistry[kind]=[]
00018     # backwards compatibilty monkey patch
00019     if not hasattr(prefs['klass'], 'getAccepted'): prefs['klass'].getAccepted = getAcceptedHelper
00020     # add this pref helper
00021     _langPrefsRegistry[kind].append(prefs)
00022     # sort by priority
00023     _langPrefsRegistry[kind].sort(lambda x, y: cmp(y['priority'], x['priority']))

Here is the call graph for this function:

Here is the caller graph for this function:

def PlacelessTranslationService.Negotiator.setCookieLanguage (   request,
  lang,
  REQUEST = None 
)
sets the language to a cookie

request - the request object
lang - language as string like de or pt_BR (it's normalizd)

Definition at line 159 of file Negotiator.py.

00159 
00160 def setCookieLanguage(request, lang, REQUEST=None):
00161     """sets the language to a cookie
00162 
00163     request - the request object
00164     lang - language as string like de or pt_BR (it's normalizd)
00165     """
00166     if type(lang) is types.TupleType:
00167         lang = lang[1]
00168     lang = str_lower(lang_normalize(lang))
00169     request.RESPONSE.setCookie('pts_language', lang)
00170     if REQUEST:
00171         REQUEST.RESPONSE.redirect(REQUEST.URL0)
00172     else:
00173         return lang
00174 
00175 # BBB: This handler will be removed in PTS 1.5. It is not registered anymore
# in 1.4 as it interferes with forms that include a field called language.

Here is the call graph for this function:

Here is the caller graph for this function:

filter

Definition at line 37 of file Negotiator.py.

00037 
00038 def str_lower(aString):
00039     """filter"""
00040     return aString.lower()

Here is the caller graph for this function:

filter

Definition at line 41 of file Negotiator.py.

00041 
00042 def str_strip(aString):
00043     """filter"""
00044     return aString.strip()

def PlacelessTranslationService.Negotiator.type_accepted (   available,
  preferred 
)

Definition at line 45 of file Negotiator.py.

00045 
00046 def type_accepted(available, preferred):
00047     # ex: preferred is text/* and available is text/html
00048     av = available.split('/')
00049     pr = preferred.split('/')
00050     if len(av) < 2 or len(pr) < 2:
00051         return False
00052     return pr[1] == '*' and pr[0] == av[0]


Variable Documentation

Definition at line 5 of file Negotiator.py.

Definition at line 248 of file Negotiator.py.