#  $Id: Localizer.py,v 1.1.1.1 2002/02/23 13:40:19 dieter Exp $
'''Site Access Rule to facilitate localization.

The function "localize" below is installed as a SiteAccess rule
in a folder, say 'F'. It should help with localization by
redirecting requests to a language specific folder for the
current language or a default language folder.

The function assumes the following structure:

  .../F/<non-language-specific-content>
  .../en/F_localized/<english specific content>
  .../en/F_localized/SiteRoot(Path='')
  .../de/F_localized/<german specific content>
  .../de/F_localized/SiteRoot(Path='')
  etc for all languages

It assumes that the currently needed target language can
be found via a property 'language'. This can be ensured
either with a SiteAccessRule in the language folders
that set a corresponding REQUEST variable or by
giving the folders a corresponding property.


It affects the traversal of URLs of the form '.../F/O...'
and does not have effects for other URLs.

If 'language' is undefined or is empty, the function does nothing
special.

If 'O' is accessable, the function does nothing special.

Otherwise, it checks for 'O' in '<language>/F<LocalizerSuffix>' and
'<LocalizerDefaultLanguage>/F<LocalizerSuffix>' in that order.
If successful. it forces Zope to visit that folder
and sets up "SiteRootPATH" for the SiteRoots in these
folders. These SiteRoutes should have an empty base and path.
Of cause, the language folders must be accessible from 'F'.

'LocalizerSuffix' defaults to '_localized'; it should not be empty.

'LocalizerDefaultLanguage' defaults to 'en' (english).

If 'LocalizerTransform' is defined and not empty, it is seen
as a *module.function* specification. The function is used
to transform the language.
'''

import string

def localize(self,REQUEST,dummy):
  '''see above'''
  path= REQUEST.path

  if not path: return

  language= getattr(self,'language',None) or REQUEST.get('language')
  if not language: return

  target= path[-1]
  if hasattr(self,target): return

  suffix= getattr(self,'LocalizerSuffix',None) or REQUEST.get('LocalizerSuffix','_localized')

  default= getattr(self,'LocalizerDefaultLanguage',None) or REQUEST.get('LocalizerDefaultLanguage','en')

  transform= getattr(self,'LocalizerTransform',None) or REQUEST.get('LocalizerTransform')
  if transform: transform= string.strip(transform)
  if transform:
    tl= string.split(transform,'.')
    if len(tl) > 1:
      transform= tl[-1]
      exec "from %s import %s" % (string.join(tl[:-1],'.'), tl[-1])
    transform= eval(transform)
    language= transform(language); default= transform(language)

  localized= "%s%s" % (REQUEST.steps[-1], suffix)

  for src in (language, default):
    if not src: continue
    lf= getattr(self,src,None)
    if not lf: continue
    Lf= getattr(lf,localized,None)
    if not Lf: continue
    if getattr(Lf,target,None):
      # success
      path.extend([localized, src])
      REQUEST.set('SiteRootPATH',string.join(REQUEST.steps,'/'))
      return


