Back to index

plone3  3.1.7
__init__.py
Go to the documentation of this file.
00001 """
00002 $Id: __init__.py 69676 2008-08-09 09:04:27Z hannosch $
00003 """
00004 
00005 import os
00006 from zope.deprecation import deprecate
00007 
00008 from Products.PageTemplates.GlobalTranslationService import \
00009     setGlobalTranslationService
00010 from Globals import package_home
00011 import Globals
00012 pts_globals = globals()
00013 
00014 from AccessControl import ModuleSecurityInfo, allow_module
00015 from AccessControl.Permissions import view
00016 
00017 from PlacelessTranslationService import PlacelessTranslationService
00018 from PlacelessTranslationService import PTSWrapper
00019 from PlacelessTranslationService import PTS_IS_RTL
00020 
00021 import logging
00022 from utils import log
00023 
00024 from GettextMessageCatalog import purgeMoFileCache
00025 
00026 # Apply import time patches
00027 if not bool(os.getenv('DISABLE_PTS')):
00028     import patches
00029 
00030 # id to use in the Control Panel
00031 cp_id = 'TranslationService'
00032 
00033 # module level translation service
00034 translation_service = None
00035 
00036 # icon
00037 misc_ = {
00038     'PlacelessTranslationService.png':
00039     Globals.ImageFile('www/PlacelessTranslationService.png', globals()),
00040     'GettextMessageCatalog.png':
00041     Globals.ImageFile('www/GettextMessageCatalog.png', globals()),
00042     }
00043 
00044 # set product-wide attrs for importing
00045 security = ModuleSecurityInfo('Products.PlacelessTranslationService')
00046 allow_module('Products.PlacelessTranslationService')
00047 
00048 security.declareProtected(view, 'getTranslationService')
00049 def getTranslationService():
00050     """returns the PTS instance
00051     """
00052     return translation_service
00053 
00054 security.declareProtected(view, 'translate')
00055 @deprecate("The translate method of the PTS package is deprecated and will be "
00056            "removed in the next PTS release. Use the translate method of the "
00057            "global translation service instead.")
00058 def translate(*args, **kwargs):
00059     """see PlaceslessTranslationService.PlaceslessTranslationService
00060     """
00061     return getTranslationService().translate(*args, **kwargs)
00062 
00063 security.declareProtected(view, 'utranslate')
00064 @deprecate("The utranslate method of the PTS package is deprecated and will be "
00065            "removed in the next PTS release. Use the translate method of the "
00066            "global translation service instead.")
00067 def utranslate(*args, **kwargs):
00068     """see PlaceslessTranslationService.PlaceslessTranslationService
00069     """
00070     return getTranslationService().translate(*args, **kwargs)
00071 
00072 security.declareProtected(view, 'getLanguages')
00073 @deprecate("The getLanguages method of the PTS package is deprecated and will "
00074            "be removed in the next PTS release. Use the getLanguages method on "
00075            "the translation service instead.")
00076 def getLanguages(*args, **kwargs):
00077     """see PlaceslessTranslationService.PlaceslessTranslationService
00078     """
00079     return getTranslationService().getLanguages(*args, **kwargs)
00080 
00081 security.declareProtected(view, 'getLanguageName')
00082 @deprecate("The getLanguageName method of the PTS package is deprecated and "
00083            "will be removed in the next PTS release. Use the getLanguageName "
00084            "method on the translation service instead.")
00085 def getLanguageName(*args, **kwargs):
00086     """see PlaceslessTranslationService.PTSWrapper
00087     """
00088     return getTranslationService().getLanguageName(*args, **kwargs)
00089 
00090 security.declareProtected(view, 'isRTL')
00091 @deprecate("The isRTL method of the PTS package is deprecated and will be "
00092            "removed in the next PTS release. Use the information found in the "
00093            "Zope3 locale instead.")
00094 def isRTL(context, domain):
00095     """Returns true for a rtl language and false for a ltr language
00096     """
00097     return getTranslationService().isRTL(context, domain)
00098 
00099 def make_translation_service(cp):
00100     """Control_Panel translation service
00101     """
00102     global translation_service
00103     translation_service = PlacelessTranslationService('default')
00104     translation_service.id = cp_id
00105     cp._setObject(cp_id, translation_service)
00106     # We need to get the service in the context of the control panel so its
00107     # physical path is correct.
00108     cp_ts = getattr(cp, cp_id)
00109     translation_service = PTSWrapper(cp_ts)
00110     return cp_ts
00111 
00112 def initialize(context):
00113     # hook into the Control Panel
00114     global translation_service
00115 
00116     # allow for disabling PTS entirely by setting an environment variable.
00117     if bool(os.getenv('DISABLE_PTS')):
00118         log('Disabled by environment variable "DISABLE_PTS".', logging.WARNING)
00119         return
00120 
00121     cp = context._ProductContext__app.Control_Panel # argh
00122     if cp_id in cp.objectIds():
00123         cp_ts = getattr(cp, cp_id)
00124         # use the ts in the acquisition context of the control panel
00125         # translation_service = translation_service.__of__(cp)
00126         translation_service = PTSWrapper(cp_ts)
00127     else:
00128         cp_ts = make_translation_service(cp)
00129 
00130     # don't touch - this is the last version that didn't have the
00131     # attribute (0.4)
00132     instance_version = getattr(cp_ts, '_instance_version', (0, 4, 0, 0))
00133     if instance_version[3] > 99:
00134         log('development mode: translation service recreated',
00135             detail = '(found %s.%s.%s.%s)\n' % instance_version)
00136         cp._delObject(cp_id)
00137         cp_ts = make_translation_service(cp)
00138 
00139     if instance_version < PlacelessTranslationService._class_version:
00140         log('outdated translation service found, recreating',
00141             detail = '(found %s.%s.%s.%s)\n' % instance_version)
00142         cp._delObject(cp_id)
00143         purgeMoFileCache()
00144         cp_ts = make_translation_service(cp)
00145 
00146     # sweep products
00147     products = [getattr(p, 'package_name', 'Products.' + p.id) for
00148                 p in cp.Products.objectValues() if
00149                 getattr(p, 'thisIsAnInstalledProduct', False)]
00150     # Sort the products by lower-cased package name to gurantee a stable
00151     # load order
00152     products.sort(key=lambda p: p.lower())
00153     log('products: %r' % products, logging.DEBUG)
00154     for prod in products:
00155         # prod is a package name, we fake a globals dict with it
00156         try:
00157             prod_path = package_home({'__name__' : prod})
00158             cp_ts._load_i18n_dir(os.path.join(prod_path, 'i18n'))
00159         except KeyError:
00160             log("You have a stale entry for '%s' in your ZMI Products section."
00161                 "You should consider removing it." % prod, logging.INFO)
00162             continue
00163 
00164     # sweep the i18n directory for local catalogs
00165     instance_i18n = os.path.join(Globals.INSTANCE_HOME, 'i18n')
00166     if os.path.isdir(instance_i18n):
00167         cp_ts._load_i18n_dir(instance_i18n)
00168 
00169     instance_locales = os.path.join(Globals.INSTANCE_HOME, 'locales')
00170     if os.path.isdir(instance_locales):
00171         cp_ts._load_locales_dir(instance_locales)
00172 
00173     # didn't found any catalogs
00174     if not cp_ts.objectIds():
00175         log('no translations found!', logging.DEBUG)
00176 
00177     # set ZPT's translation service
00178     # NOTE: since this registry is a global var we can't register the
00179     #       persistent service itself (zodb connection) therefore a
00180     #       wrapper is created around it
00181     setGlobalTranslationService(PTSWrapper(cp_ts))