Commit 822ef930 authored by Kevin Deldycke's avatar Kevin Deldycke

Make Jerome patch working.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5374 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent cea1dc88
No related merge requests found
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
from Products.CMFCore.utils import getToolByName 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 ERP5Form.
try: try:
...@@ -31,14 +30,32 @@ class LocalizerTranslationService: ...@@ -31,14 +30,32 @@ class LocalizerTranslationService:
""" """
This translate() method use Localizer and support catalog aliases. This translate() method use Localizer and support catalog aliases.
""" """
# This dict define the alias between old Translation Service catalog id # This dict define the alias between old Translation Service catalog id
# and new Localizer Message Catalog. # and new Localizer Message Catalog.
message_catalog_aliases = { "ui" : "erp5_ui" message_catalog_aliases = { "ui" : "erp5_ui"
, "content": "erp5_content" , "content": "erp5_content"
} }
# Get Localizer # Be carefull to not import Localizer anywhere in that Localizer patch:
localizer = getToolByName(context, 'Localizer') # it can break everything !
# To update this file in the right way, keep in mind that translate() method
# must be able to work outside its LocalizerTranslationService class.
from Products.Localizer.Localizer import Localizer
# Get Localizer object installed in ERP5 instance
if isinstance(self, Localizer):
# In this case we call the translate method from a script (Base_translateString generally).
# Exemple of call from a ZODB python script:
# context.Localizer.translate('ui', 'Print button')
# This branch of "if" statement is needed to make the "New Localizer Feature" coded
# at the end of the file work.
localizer = self
else:
# This is the normal case
# = the one when translation is done via <i18n:translate> tag in Page Template
localizer = getToolByName(context, 'Localizer', None)
# Get the Localizer catalog id # Get the Localizer catalog id
catalog_id = None catalog_id = None
if domain in message_catalog_aliases.keys(): if domain in message_catalog_aliases.keys():
...@@ -49,6 +66,7 @@ class LocalizerTranslationService: ...@@ -49,6 +66,7 @@ class LocalizerTranslationService:
# 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 # Adapt Translation Service default value to the Localizer one
if default == None: if default == None:
default = msgid default = msgid
...@@ -57,16 +75,24 @@ class LocalizerTranslationService: ...@@ -57,16 +75,24 @@ class LocalizerTranslationService:
, lang = target_language , lang = target_language
, default = default , default = default
) )
# Map the translated string with given parameters
if type(mapping) is type({}): if type(mapping) is type({}):
return Template(translated_str).substitute(mapping) return Template(translated_str).substitute(mapping)
return translated_str return translated_str
# Use the patched translate() method # Apply the monkey patch
from Products.PageTemplates import GlobalTranslationService from Products.PageTemplates import GlobalTranslationService
def setGlobalTranslationService(service): def setGlobalTranslationService(service):
GlobalTranslationService.translationService = LocalizerTranslationService() GlobalTranslationService.translationService = LocalizerTranslationService()
GlobalTranslationService.setGlobalTranslationService = setGlobalTranslationService GlobalTranslationService.setGlobalTranslationService = setGlobalTranslationService
### New Localizer Feature ###
# Allow call of translate() in python scripts directly from Localizer object
from Products.Localizer import Localizer
Localizer.Localizer.translate = LocalizerTranslationService.translate
Localizer.Localizer.tranlate__roles__ = None # public
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