Commit 836cfa73 authored by Jérome Perrin's avatar Jérome Perrin

monkey patch Localizer so that it gets a translate method callable from the outside


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5325 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 869fb374
......@@ -12,21 +12,19 @@
#
##############################################################################
from Products.CMFCore.utils import getToolByName
# Template() is a new method of python 2.4, that's why we have the string.py
# file in ERP5Form.
# file in patches directory.
try:
from string import Template
except ImportError:
from Products.ERP5Type.patches.string import Template
from Products.CMFCore.utils import getToolByName
from Products.Localizer.MessageCatalog import _marker
class LocalizerTranslationService:
def translate( self, domain, msgid
, mapping=None, context=None, target_language=None, default=None
, mapping=None, context=None, target_language=None, default=_marker
, *args, **kw):
"""
This translate() method use Localizer and support catalog aliases.
......@@ -38,20 +36,14 @@ class LocalizerTranslationService:
}
# Get Localizer
localizer = getToolByName(context, 'Localizer')
localizer = getToolByName(self, 'Localizer')
# Get the Localizer catalog id
catalog_id = None
if domain in message_catalog_aliases.keys():
catalog_id = message_catalog_aliases[domain]
else:
catalog_id = domain
catalog_id = message_catalog_aliases.get(domain, domain)
if catalog_id not in localizer.objectIds():
# No catalog found: return the untranslated string
return msgid
catalog_obj = localizer[catalog_id]
# Adapt Translation Service default value to the Localizer one
if default == None:
default = msgid
# Call the Message Catalog gettext method
translated_str = catalog_obj.gettext( message = msgid
, lang = target_language
......@@ -62,11 +54,10 @@ class LocalizerTranslationService:
return translated_str
# Use the patched translate() method
from Products.PageTemplates import GlobalTranslationService
from Products.Localizer import GlobalTranslationService, Localizer
def setGlobalTranslationService(service):
GlobalTranslationService.translationService = LocalizerTranslationService()
GlobalTranslationService.translate = LocalizerTranslationService.translate
Localizer.Localizer.translate = LocalizerTranslationService.translate
Localizer.Localizer.tranlate__roles__ = None # public
GlobalTranslationService.setGlobalTranslationService = setGlobalTranslationService
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment