Back to index

plone3  3.1.7
Public Member Functions | Public Attributes | Static Public Attributes
PloneLanguageTool.LanguageTool.LanguageTool Class Reference
Inheritance diagram for PloneLanguageTool.LanguageTool.LanguageTool:
Inheritance graph
[legend]
Collaboration diagram for PloneLanguageTool.LanguageTool.LanguageTool:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __call__
def manage_setLanguageSettings
def startNeutral
def showFlags
def getSupportedLanguages
def listSupportedLanguages
def getAvailableLanguages
def getCcTLDInformation
def listAvailableLanguages
def listAvailableLanguageInformation
def getAvailableLanguageInformation
def getDefaultLanguage
def setDefaultLanguage
def getNameForLanguageCode
def getFlagForLanguageCode
def addSupportedLanguage
def removeSupportedLanguages
def setLanguageCookie
def getLanguageCookie
def getPreferredLanguage
def manage_beforeDelete
def manage_afterAdd
def getPathLanguage
def getCcTLDLanguages
def getRequestLanguages
def setLanguageBindings
def getLanguageBindings
def isTranslatable
def getAvailableCountries
def listAvailableCountries
def getNameForCountryCode

Public Attributes

 id
 use_path_negotiation
 use_cookie_negotiation
 use_request_negotiation
 use_cctld_negotiation
 use_combined_language_codes
 force_language_urls
 allow_content_language_fallback
 display_flags
 start_neutral
 supported_langs
 default_lang

Static Public Attributes

string id = 'portal_languages'
string title = 'Manages available languages'
string meta_type = 'Plone Language Tool'
tuple security = ClassSecurityInfo()
list supported_langs = ['en']
int use_path_negotiation = 1
int use_cookie_negotiation = 1
int use_request_negotiation = 1
int use_cctld_negotiation = 0
int use_combined_language_codes = 0
int force_language_urls = 1
int allow_content_language_fallback = 0
int display_flags = 1
int start_neutral = 0
tuple manage_options
tuple manage_configForm = PageTemplateFile('www/config', globals())

Detailed Description

Language Administration Tool For Plone.

Definition at line 32 of file LanguageTool.py.


Constructor & Destructor Documentation


Member Function Documentation

def PloneLanguageTool.LanguageTool.LanguageTool.__call__ (   self,
  container,
  req 
)
The __before_publishing_traverse__ hook.

Definition at line 75 of file LanguageTool.py.

00075 
00076     def __call__(self, container, req):
00077         """The __before_publishing_traverse__ hook."""
00078         if req.__class__ is not HTTPRequest:
00079             return None
00080         if not req['REQUEST_METHOD'] in ('HEAD', 'GET', 'PUT', 'POST'):
00081             return None
00082         if req.environ.has_key('WEBDAV_SOURCE_PORT'):
00083             return None
00084         # Bind the languages
00085         self.setLanguageBindings()

Here is the call graph for this function:

Here is the caller graph for this function:

Registers a language code as supported.

Definition at line 278 of file LanguageTool.py.

00278 
00279     def addSupportedLanguage(self, langCode):
00280         """Registers a language code as supported."""
00281         alist = self.supported_langs[:]
00282         if (langCode in self.getAvailableLanguages().keys()) and not langCode in alist:
00283             alist.append(langCode)
00284             self.supported_langs = alist

Here is the call graph for this function:

Returns the dictionary of available countries.

Definition at line 481 of file LanguageTool.py.

00481 
00482     def getAvailableCountries(self):
00483         """Returns the dictionary of available countries."""
00484         util = queryUtility(ICountryAvailability)
00485         return util.getCountries()

Here is the caller graph for this function:

Returns the dictionary of available languages.

Definition at line 219 of file LanguageTool.py.

00219 
00220     def getAvailableLanguageInformation(self):
00221         """Returns the dictionary of available languages."""
00222         util = queryUtility(IContentLanguageAvailability)
00223         if self.use_combined_language_codes:
00224             languages = util.getLanguages(combined=True)
00225         else:
00226             languages = util.getLanguages()
00227 
00228         for lang in languages:
00229             languages[lang]['code'] = lang
00230             if lang in self.supported_langs:
00231                 languages[lang]['selected'] = True
00232             else:
00233                 languages[lang]['selected'] = False
00234         return languages

Here is the caller graph for this function:

Returns the dictionary of available languages.

Definition at line 179 of file LanguageTool.py.

00179 
00180     def getAvailableLanguages(self):
00181         """Returns the dictionary of available languages.
00182         """
00183         util = queryUtility(IContentLanguageAvailability)
00184         if self.use_combined_language_codes:
00185             languages = util.getLanguages(combined=True)
00186         else:
00187             languages = util.getLanguages()
00188         return languages

Here is the caller graph for this function:

Definition at line 190 of file LanguageTool.py.

00190 
00191     def getCcTLDInformation(self):
00192         util = queryUtility(ICcTLDInformation)
00193         return util.getTLDs()

Here is the caller graph for this function:

Definition at line 359 of file LanguageTool.py.

00359 
00360     def getCcTLDLanguages(self):
00361         if not hasattr(self, 'REQUEST'):
00362             return None
00363         request = self.REQUEST
00364         if not "HTTP_HOST" in request:
00365             return None
00366         host=request["HTTP_HOST"].split(":")[0].lower()
00367         tld=host.split(".")[-1]
00368         wanted = self.getCcTLDInformation().get(tld, [])
00369         allowed = self.getSupportedLanguages()
00370         return [lang for lang in wanted if lang in allowed]

Here is the call graph for this function:

Returns the default language.

Definition at line 236 of file LanguageTool.py.

00236 
00237     def getDefaultLanguage(self):
00238         """Returns the default language."""
00239         portal_properties = getToolByName(self, "portal_properties", None)
00240         if portal_properties is None:
00241             return 'en'
00242         site_properties = portal_properties.site_properties
00243         if site_properties.hasProperty('default_language'):
00244             return site_properties.getProperty('default_language')
00245         portal = getUtility(ISiteRoot)
00246         if portal.hasProperty('default_language'):
00247             return portal.getProperty('default_language')
00248         return getattr(self, 'default_lang', 'en')

Here is the call graph for this function:

Here is the caller graph for this function:

Returns the name of the flag for a language code.

Definition at line 270 of file LanguageTool.py.

00270 
00271     def getFlagForLanguageCode(self, langCode):
00272         """Returns the name of the flag for a language code."""
00273         info = self.getAvailableLanguageInformation().get(langCode, None)
00274         if info is not None:
00275             return info.get(u'flag', None)
00276         return None

Here is the call graph for this function:

Returns the bound languages.

(language, default_language, languages_list)

Definition at line 455 of file LanguageTool.py.

00455 
00456     def getLanguageBindings(self):
00457         """Returns the bound languages.
00458 
00459         (language, default_language, languages_list)
00460         """
00461         if not hasattr(self, 'REQUEST'):
00462             # Can't do anything
00463             return (None, self.getDefaultLanguage(), [])
00464         binding = self.REQUEST.get('LANGUAGE_TOOL', None)
00465         if not isinstance(binding, LanguageBinding):
00466             # Not bound -> bind
00467             self.setLanguageBindings()
00468             binding = self.REQUEST.get('LANGUAGE_TOOL')
00469         return binding.getLanguageBindings()

Here is the call graph for this function:

Here is the caller graph for this function:

Gets the preferred cookie language.

Definition at line 307 of file LanguageTool.py.

00307 
00308     def getLanguageCookie(self):
00309         """Gets the preferred cookie language."""
00310         if not hasattr(self, 'REQUEST'):
00311             return None
00312         langCookie = self.REQUEST.cookies.get('I18N_LANGUAGE')
00313         if langCookie in self.getSupportedLanguages():
00314             return langCookie
00315         return None

Here is the call graph for this function:

Here is the caller graph for this function:

Returns the name for a country code.

Definition at line 495 of file LanguageTool.py.

00495 
00496     def getNameForCountryCode(self, countryCode):
00497         """Returns the name for a country code."""
00498         return self.getAvailableCountries().get(countryCode, countryCode)
00499 

Here is the call graph for this function:

Returns the name for a language code.

Definition at line 262 of file LanguageTool.py.

00262 
00263     def getNameForLanguageCode(self, langCode):
00264         """Returns the name for a language code."""
00265         info = self.getAvailableLanguageInformation().get(langCode, None)
00266         if info is not None:
00267             return info.get(u'name', None)
00268         return None

Here is the call graph for this function:

Checks if a language is part of the current path.

Definition at line 342 of file LanguageTool.py.

00342 
00343     def getPathLanguage(self):
00344         """Checks if a language is part of the current path."""
00345         if not hasattr(self, 'REQUEST'):
00346             return []
00347         items = self.REQUEST.get('TraversalRequestNameStack')
00348         # XXX Why this try/except?
00349         try:
00350             for item in items:
00351                 if item in self.getSupportedLanguages():
00352                     return item
00353         except (ConflictError, KeyboardInterrupt):
00354             raise
00355         except:
00356             pass
00357         return None

Here is the call graph for this function:

Gets the preferred site language.

Definition at line 317 of file LanguageTool.py.

00317 
00318     def getPreferredLanguage(self):
00319         """Gets the preferred site language."""
00320         l = self.getLanguageBindings()
00321         if l[0]:
00322             if not self.use_combined_language_codes:
00323                 return l[0].split('-')[0]
00324             else:
00325                 return l[0]
00326             return l[0]
00327         # this is the default language
00328         return l[1]

Here is the call graph for this function:

Parses the request and return language list.

Definition at line 372 of file LanguageTool.py.

00372 
00373     def getRequestLanguages(self):
00374         """Parses the request and return language list."""
00375 
00376         if not hasattr(self, 'REQUEST'):
00377             return None
00378 
00379         # Get browser accept languages
00380         browser_pref_langs = self.REQUEST.get('HTTP_ACCEPT_LANGUAGE', '')
00381         browser_pref_langs = browser_pref_langs.split(',')
00382 
00383         i = 0
00384         langs = []
00385         length = len(browser_pref_langs)
00386 
00387         # Parse quality strings and build a tuple like
00388         # ((float(quality), lang), (float(quality), lang))
00389         # which is sorted afterwards
00390         # If no quality string is given then the list order
00391         # is used as quality indicator
00392         for lang in browser_pref_langs:
00393             lang = lang.strip().lower().replace('_', '-')
00394             if lang:
00395                 l = lang.split(';', 2)
00396                 quality = []
00397 
00398                 if len(l) == 2:
00399                     try:
00400                         q = l[1]
00401                         if q.startswith('q='):
00402                             q = q.split('=', 2)[1]
00403                             quality = float(q)
00404                     except:
00405                         pass
00406 
00407                 if quality == []:
00408                     quality = float(length-i)
00409 
00410                 language = l[0]
00411                 if self.use_combined_language_codes:
00412                     if language in self.getSupportedLanguages():
00413                         # If allowed the add language
00414                         langs.append((quality, language))
00415                 else:
00416                     # if we only use simply language codes, we should recognize
00417                     # combined codes as their base code. So 'de-de' is treated
00418                     # as 'de'.
00419                     baselanguage = language.split('-')[0]
00420                     if baselanguage in self.getSupportedLanguages():
00421                         langs.append((quality, baselanguage))
00422                 i = i + 1
00423 
00424         # Sort and reverse it
00425         langs.sort()
00426         langs.reverse()
00427 
00428         # Filter quality string
00429         langs = map(lambda x: x[1], langs)
00430 
00431         return langs

Here is the call graph for this function:

Returns a list of supported language codes.

Definition at line 164 of file LanguageTool.py.

00164 
00165     def getSupportedLanguages(self):
00166         """Returns a list of supported language codes."""
00167         return self.supported_langs

Here is the caller graph for this function:

Checks if ITranslatable interface is implemented.

Definition at line 471 of file LanguageTool.py.

00471 
00472     def isTranslatable(self, obj):
00473         """Checks if ITranslatable interface is implemented."""
00474         try:
00475             if obj.checkCreationFlag():
00476                 return False
00477         except NameError:
00478             pass
00479         return ITranslatable.isProvidedBy(obj)

Returns the sorted list of available countries (code, name).

Definition at line 487 of file LanguageTool.py.

00487 
00488     def listAvailableCountries(self):
00489         """Returns the sorted list of available countries (code, name)."""
00490         util = queryUtility(ICountryAvailability)
00491         countries = util.getCountryListing()
00492         countries.sort(lambda x, y: cmp(x[1], y[1]))
00493         return countries

Returns list of available languages.

Definition at line 206 of file LanguageTool.py.

00206 
00207     def listAvailableLanguageInformation(self):
00208         """Returns list of available languages."""
00209         langs = self.getAvailableLanguageInformation()
00210         new_langs = []
00211         for lang in langs:
00212             # add language-code to dict
00213             langs[lang][u'code'] = lang
00214             # flatten outer dict to list to make it sortable
00215             new_langs.append(langs[lang])
00216         new_langs.sort(lambda x, y: cmp(x.get(u'native', x.get(u'name')), y.get(u'native', y.get(u'name'))))
00217         return new_langs

Here is the call graph for this function:

Returns sorted list of available languages (code, name).

Definition at line 195 of file LanguageTool.py.

00195 
00196     def listAvailableLanguages(self):
00197         """Returns sorted list of available languages (code, name)."""
00198         util = queryUtility(IContentLanguageAvailability)
00199         if self.use_combined_language_codes:
00200             languages = util.getLanguageListing(combined=True)
00201         else:
00202             languages = util.getLanguageListing()
00203         languages.sort(lambda x, y: cmp(x[1], y[1]))
00204         return languages

Returns a list of supported language names.

Definition at line 169 of file LanguageTool.py.

00169 
00170     def listSupportedLanguages(self):
00171         """Returns a list of supported language names."""
00172         r = []
00173         available = self.getAvailableLanguages()
00174         for i in self.supported_langs:
00175             if available.get(i):
00176                 r.append((i,available[i][u'name']))
00177         return r

Here is the call graph for this function:

def PloneLanguageTool.LanguageTool.LanguageTool.manage_afterAdd (   self,
  item,
  container 
)

Definition at line 334 of file LanguageTool.py.

00334 
00335     def manage_afterAdd(self, item, container):
00336         if item is self:
00337             handle = self.meta_type + '/' + self.getId()
00338             container = container.this()
00339             nc = BeforeTraverse.NameCaller(self.getId())
00340             BeforeTraverse.registerBeforeTraverse(container, nc, handle)

def PloneLanguageTool.LanguageTool.LanguageTool.manage_beforeDelete (   self,
  item,
  container 
)

Definition at line 329 of file LanguageTool.py.

00329 
00330     def manage_beforeDelete(self, item, container):
00331         if item is self:
00332             handle = self.meta_type + '/' + self.getId()
00333             BeforeTraverse.unregisterBeforeTraverse(container, handle)

def PloneLanguageTool.LanguageTool.LanguageTool.manage_setLanguageSettings (   self,
  defaultLanguage,
  supportedLanguages,
  setCookieN = None,
  setRequestN = None,
  setPathN = None,
  setForcelanguageUrls = None,
  setAllowContentLanguageFallback = None,
  setUseCombinedLanguageCodes = None,
  displayFlags = None,
  startNeutral = None,
  setCcTLDN = None,
  REQUEST = None 
)
Stores the tool settings.

Definition at line 93 of file LanguageTool.py.

00093 
00094                                    setCcTLDN=None, REQUEST=None):
00095         """Stores the tool settings."""
00096         if supportedLanguages and type(supportedLanguages) == type([]):
00097             self.supported_langs = supportedLanguages
00098 
00099         if defaultLanguage in self.supported_langs:
00100             self.setDefaultLanguage(defaultLanguage)
00101         else:
00102             self.setDefaultLanguage(self.supported_langs[0])
00103 
00104         if setCookieN:
00105             self.use_cookie_negotiation = 1
00106         else:
00107             self.use_cookie_negotiation = 0
00108 
00109         if setRequestN:
00110             self.use_request_negotiation = 1
00111         else:
00112             self.use_request_negotiation = 0
00113 
00114         if setPathN:
00115             self.use_path_negotiation = 1
00116         else:
00117             self.use_path_negotiation = 0
00118 
00119         if setCcTLDN:
00120             self.use_cctld_negotiation = 1
00121         else:
00122             self.use_cctld_negotiation = 0
00123 
00124         if setForcelanguageUrls:
00125             self.force_language_urls = 1
00126         else:
00127             self.force_language_urls = 0
00128 
00129         if setAllowContentLanguageFallback:
00130             self.allow_content_language_fallback = 1
00131         else:
00132             self.allow_content_language_fallback = 0
00133 
00134         if setUseCombinedLanguageCodes:
00135             self.use_combined_language_codes = 1
00136         else:
00137             self.use_combined_language_codes = 0
00138 
00139         if displayFlags:
00140             self.display_flags = 1
00141         else:
00142             self.display_flags = 0
00143 
00144         if startNeutral:
00145             self.start_neutral = 1
00146         else:
00147             self.start_neutral = 0
00148 
00149         if REQUEST:
00150             REQUEST.RESPONSE.redirect(REQUEST['HTTP_REFERER'])

Here is the caller graph for this function:

Unregisters language codes from supported.

Definition at line 286 of file LanguageTool.py.

00286 
00287     def removeSupportedLanguages(self, langCodes):
00288         """Unregisters language codes from supported."""
00289         alist = self.supported_langs[:]
00290         for i in langCodes:
00291             alist.remove(i)
00292         self.supported_langs = alist

Sets the default language.

Definition at line 250 of file LanguageTool.py.

00250 
00251     def setDefaultLanguage(self, langCode):
00252         """Sets the default language."""
00253         portal_properties = getToolByName(self, "portal_properties")
00254         site_properties = portal_properties.site_properties
00255         if site_properties.hasProperty('default_language'):
00256             return site_properties._updateProperty('default_language', langCode)
00257         portal = getUtility(ISiteRoot)
00258         if portal.hasProperty('default_language'):
00259             return portal._updateProperty('default_language', langCode)
00260         self.default_lang = langCode

Here is the call graph for this function:

Setups the current language stuff.

Definition at line 433 of file LanguageTool.py.

00433 
00434     def setLanguageBindings(self):
00435         """Setups the current language stuff."""
00436         useCcTLD = self.use_cctld_negotiation
00437         usePath = self.use_path_negotiation
00438         useCookie = self.use_cookie_negotiation
00439         useRequest = self.use_request_negotiation
00440         useDefault = 1 # This should never be disabled
00441         if not hasattr(self, 'REQUEST'):
00442             return
00443         binding = self.REQUEST.get('LANGUAGE_TOOL', None)
00444         if not isinstance(binding, LanguageBinding):
00445             # Create new binding instance
00446             binding = LanguageBinding(self)
00447         # Bind languages
00448         lang = binding.setLanguageBindings(usePath, useCookie, useRequest, useDefault, useCcTLD)
00449         # Set LANGUAGE to request
00450         self.REQUEST['LANGUAGE'] = lang
00451         # Set bindings instance to request
00452         self.REQUEST['LANGUAGE_TOOL'] = binding
00453         return lang

Here is the caller graph for this function:

def PloneLanguageTool.LanguageTool.LanguageTool.setLanguageCookie (   self,
  lang = None,
  REQUEST = None,
  noredir = None 
)
Sets a cookie for overriding language negotiation.

Definition at line 294 of file LanguageTool.py.

00294 
00295     def setLanguageCookie(self, lang=None, REQUEST=None, noredir=None):
00296         """Sets a cookie for overriding language negotiation."""
00297         res = None
00298         if lang and lang in self.getSupportedLanguages():
00299             if lang != self.getLanguageCookie():
00300                 self.REQUEST.RESPONSE.setCookie('I18N_LANGUAGE', lang, path='/')
00301             res = lang
00302         if noredir is None:
00303             if REQUEST:
00304                 REQUEST.RESPONSE.redirect(REQUEST['HTTP_REFERER'])
00305         return res

Here is the call graph for this function:

Shows the flags in language listings or not.

Definition at line 159 of file LanguageTool.py.

00159 
00160     def showFlags(self):
00161         """Shows the flags in language listings or not."""
00162         return self.display_flags

Checks if the content start as language neutral or using the
preferred language.

Definition at line 152 of file LanguageTool.py.

00152 
00153     def startNeutral(self):
00154         """Checks if the content start as language neutral or using the
00155         preferred language.
00156         """
00157         return self.start_neutral


Member Data Documentation

Definition at line 50 of file LanguageTool.py.

Definition at line 71 of file LanguageTool.py.

Definition at line 259 of file LanguageTool.py.

Definition at line 51 of file LanguageTool.py.

Definition at line 72 of file LanguageTool.py.

Definition at line 49 of file LanguageTool.py.

Definition at line 70 of file LanguageTool.py.

string PloneLanguageTool.LanguageTool.LanguageTool.id = 'portal_languages' [static]

Definition at line 35 of file LanguageTool.py.

Definition at line 64 of file LanguageTool.py.

tuple PloneLanguageTool.LanguageTool.LanguageTool.manage_configForm = PageTemplateFile('www/config', globals()) [static]

Definition at line 61 of file LanguageTool.py.

Initial value:
(
        ({ 'label'  : 'LanguageConfig',
           'action' : 'manage_configForm',
           },
         ) + SimpleItem.manage_options
        )

Definition at line 54 of file LanguageTool.py.

string PloneLanguageTool.LanguageTool.LanguageTool.meta_type = 'Plone Language Tool' [static]

Definition at line 37 of file LanguageTool.py.

tuple PloneLanguageTool.LanguageTool.LanguageTool.security = ClassSecurityInfo() [static]

Definition at line 41 of file LanguageTool.py.

Definition at line 52 of file LanguageTool.py.

Definition at line 73 of file LanguageTool.py.

Definition at line 43 of file LanguageTool.py.

Definition at line 96 of file LanguageTool.py.

string PloneLanguageTool.LanguageTool.LanguageTool.title = 'Manages available languages' [static]

Definition at line 36 of file LanguageTool.py.

Definition at line 47 of file LanguageTool.py.

Definition at line 68 of file LanguageTool.py.

Definition at line 48 of file LanguageTool.py.

Definition at line 69 of file LanguageTool.py.

Definition at line 45 of file LanguageTool.py.

Definition at line 66 of file LanguageTool.py.

Definition at line 44 of file LanguageTool.py.

Definition at line 65 of file LanguageTool.py.

Definition at line 46 of file LanguageTool.py.

Definition at line 67 of file LanguageTool.py.


The documentation for this class was generated from the following file: