Commit 833cb3ad authored by Georgios Dagkakis's avatar Georgios Dagkakis

Formulator: Allow translation of ValidationError messages

Depending on how the message is defined.

Before this commit, translation happened typically in higher level,
where the error message was rendered.
but this imposed translation in everything, including mapped messages,
pulluting Localizer with unneeded messages.

This commit introduces getMessage, the logic is:
- If the message is defined in the code that raises, then it should be translated there
- If the message is defined in the field (in 'manage_messagesForm')
then we need to translate in ValidationError class. Caller should
pass translation_service, because we do not want to import it in Formulator
parent 25ce6bb7
......@@ -5,6 +5,7 @@
# from Products.Formulator.Errors import ValidationError, FormValidationError
from Products.PythonScripts.Utility import allow_class
from AccessControl import ClassSecurityInfo
class FormValidationError(Exception):
......@@ -17,6 +18,8 @@ allow_class(FormValidationError)
class ValidationError(Exception):
security = ClassSecurityInfo()
def __init__(self, error_key, field, error_text=None):
Exception.__init__(self, error_key)
self.error_key = error_key
......@@ -24,8 +27,18 @@ class ValidationError(Exception):
self.field = field
if error_text is not None:
self.error_text = error_text
self.is_message_to_translate = False
else:
self.error_text = field.get_error_message(error_key)
self.is_message_to_translate = True
security.declarePublic('getMessage')
def getMessage(self, translation_service=None):
if not self.is_message_to_translate:
return self.error_text
if translation_service is None:
return self.error_text
return translation_service(self.error_text)
allow_class(ValidationError)
......
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