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 @@ ...@@ -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 # 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: try:
from string import Template from string import Template
except ImportError: except ImportError:
from Products.ERP5Type.patches.string import Template from Products.ERP5Type.patches.string import Template
from Products.CMFCore.utils import getToolByName
from Products.Localizer.MessageCatalog import _marker
class LocalizerTranslationService: class LocalizerTranslationService:
def translate( self, domain, msgid 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): , *args, **kw):
""" """
This translate() method use Localizer and support catalog aliases. This translate() method use Localizer and support catalog aliases.
...@@ -36,22 +34,16 @@ class LocalizerTranslationService: ...@@ -36,22 +34,16 @@ class LocalizerTranslationService:
message_catalog_aliases = { "ui" : "erp5_ui" message_catalog_aliases = { "ui" : "erp5_ui"
, "content": "erp5_content" , "content": "erp5_content"
} }
# Get Localizer # Get Localizer
localizer = getToolByName(context, 'Localizer') localizer = getToolByName(self, 'Localizer')
# Get the Localizer catalog id # Get the Localizer catalog id
catalog_id = None catalog_id = message_catalog_aliases.get(domain, domain)
if domain in message_catalog_aliases.keys():
catalog_id = message_catalog_aliases[domain]
else:
catalog_id = domain
if catalog_id not in localizer.objectIds(): if catalog_id not in localizer.objectIds():
# No catalog found: return the untranslated string # No catalog found: return the untranslated string
return msgid return msgid
catalog_obj = localizer[catalog_id] 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 # Call the Message Catalog gettext method
translated_str = catalog_obj.gettext( message = msgid translated_str = catalog_obj.gettext( message = msgid
, lang = target_language , lang = target_language
...@@ -62,11 +54,10 @@ class LocalizerTranslationService: ...@@ -62,11 +54,10 @@ class LocalizerTranslationService:
return translated_str return translated_str
# Use the patched translate() method # Use the patched translate() method
from Products.PageTemplates import GlobalTranslationService from Products.Localizer import GlobalTranslationService, Localizer
def setGlobalTranslationService(service): GlobalTranslationService.translate = LocalizerTranslationService.translate
GlobalTranslationService.translationService = LocalizerTranslationService() 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