Back to index

plone3  3.1.7
i18n.py
Go to the documentation of this file.
00001 """ IMPORTANT: this module is unusable before initialize is called
00002     this must be so because we want to make sure all products
00003     (eg, whatever translation service we're supposed to use)
00004     is already there and ready
00005 """
00006 from Products.Archetypes.debug import deprecated
00007 deprecated('generator.i18n is deprecated and will be removed in Archetypes 1.6.'
00008            'If your product uses PTS or Localizer use the translate method of '
00009            'the GlobalTranslationService instead. Usually you should simply do '
00010            '"from zope.i18n import translate" and use that method.')
00011 
00012 from Products.PageTemplates.GlobalTranslationService import \
00013      getGlobalTranslationService, DummyTranslationService
00014 
00015 service = None
00016 translate = None
00017 
00018 def translate_wrapper(domain, msgid, mapping=None, context=None,
00019                       target_language=None, default=None):
00020     """Wrapper for calling the translate() method with a fallback value."""
00021     res = service.translate(domain, msgid, mapping=mapping, context=context,
00022                             target_language=target_language,
00023                             default=default)
00024 
00025     if res is None or res is msgid:
00026         return default
00027     return res
00028 
00029 def null_translate(domain, msgid, mapping=None, context=None,
00030                    target_language=None, default=None):
00031     return default
00032 
00033 def initialize():
00034     """Must be called after Products are there and ready."""
00035     global service, translate
00036     service = getGlobalTranslationService()
00037     if service is DummyTranslationService:
00038         translate = null_translate
00039     elif hasattr(service, '_fallbacks'):
00040         # It accepts the "default" argument
00041         translate = service.translate
00042     else:
00043         translate = translate_wrapper
00044 
00045 def initial_translate(domain, msgid, mapping=None, context=None,
00046                       target_language=None, default=None):
00047     initialize()
00048     deprecated('Archetypes.generator.i18n.translate is deprecated and will be '
00049                'removed in Archetypes 1.6. Please use "from zope.i18n import '
00050                'translate" instead.')
00051     return translate(domain, msgid, mapping, context, target_language, default)
00052 
00053 translate = initial_translate