Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate validation error messages in the deserializer #1743

Merged
merged 8 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1742.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Translate validation error messages in the deserializer. @wesleybl
5 changes: 5 additions & 0 deletions requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
docutils<0.17,>=0.15 # sphinx-book-theme 0.2.0 has requirement docutils<0.17,>=0.15
Sphinx<5,>=3 # sphinx-book-theme 0.3.3 has requirement sphinx<5,>=3
sphinxcontrib-applehelp==1.0.4 # newer versions require sphinx 5
sphinxcontrib-devhelp==1.0.2 # newer versions require sphinx 5
sphinxcontrib-htmlhelp==2.0.1 # newer versions require sphinx 5
sphinxcontrib-qthelp==1.0.3 # newer versions require sphinx 5
sphinxcontrib-serializinghtml==1.1.5 # newer versions require sphinx 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need all those sphinxcontrib-*helps?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevepiercy Yes, they are dependencies of Sphinx. They released new versions yesterday which are not compatible with Sphinx<5, but no longer declare that in their package metadata.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that we don't build those types of outputs, so why add them at all? We use only HTML and linkcheck.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevepiercy And I meant that as far as I can tell, we don't have a choice, because they are pulled in by sphinx.

jsx-lexer
lesscpy
linkify-it-py
Expand Down
15 changes: 9 additions & 6 deletions src/plone/restapi/deserializer/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from plone.autoform.interfaces import WRITE_PERMISSIONS_KEY
from plone.dexterity.interfaces import IDexterityContent
from plone.dexterity.utils import iterSchemata
from plone.restapi import _
from plone.restapi.deserializer import json_body
from plone.restapi.interfaces import IDeserializeFromJson
from plone.restapi.interfaces import IFieldDeserializer
Expand All @@ -14,6 +15,7 @@
from zope.component import queryMultiAdapter
from zope.component import queryUtility
from zope.event import notify
from zope.i18n import translate
from zope.interface import implementer
from zope.interface import Interface
from zope.lifecycleevent import Attributes
Expand Down Expand Up @@ -49,14 +51,16 @@ def __call__(
(self.context, self.request, None, schema, None), IManagerValidator
)
for error in validator.validate(field_data):
errors.append({"error": error, "message": str(error)})
errors.append({"error": error, "message": error.args[0]})

if errors:
if mask_validation_errors:
# Drop Python specific error classes in order to be able to better handle
# errors on front-end
for error in errors:
error["error"] = "ValidationError"
for error in errors:
error["message"] = translate(error["message"], context=self.request)
raise BadRequest(errors)

# We'll set the layout after the validation and even if there
Expand Down Expand Up @@ -109,11 +113,10 @@ def get_schema_data(self, data, validate_all, create=False):
errors.append(
{
"field": field.__name__,
"message": (
"{} is a required field.".format(
field.__name__
),
"Setting it to null is not allowed.",
"message": _(
"${field_name} is a required field."
" Setting it to null is not allowed.",
mapping={"field_name": field.__name__},
),
}
)
Expand Down
6 changes: 5 additions & 1 deletion src/plone/restapi/locales/de/LC_MESSAGES/plone.restapi.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2023-09-25 20:32+0000\n"
"POT-Creation-Date: 2023-12-22 19:57+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
"Last-Translator: Timo Stollenwerk <[email protected]>\n"
"Language-Team: German <[email protected]>\n"
Expand All @@ -14,6 +14,10 @@ msgstr ""
"Preferred-Encodings: utf-8 latin1\n"
"Domain: plone.restapi\n"

#: plone/restapi/deserializer/dxcontent.py:116
msgid "${field_name} is a required field. Setting it to null is not allowed."
msgstr ""

#: plone/restapi/services/email_send/post.py:88
msgid "${sender_fullname} via ${portal_title}"
msgstr "${sender_fullname} via ${portal_title}"
Expand Down
6 changes: 5 additions & 1 deletion src/plone/restapi/locales/es/LC_MESSAGES/plone.restapi.po
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
msgid ""
msgstr ""
"Project-Id-Version: Plone\n"
"POT-Creation-Date: 2023-09-25 20:32+0000\n"
"POT-Creation-Date: 2023-12-22 19:57+0000\n"
"PO-Revision-Date: 2023-08-23 21:19-0400\n"
"Last-Translator: Leonardo J. Caballero G. <[email protected]>\n"
"Language-Team: ES <[email protected]>\n"
Expand All @@ -20,6 +20,10 @@ msgstr ""
"X-Generator: Poedit 3.3.2\n"
"X-Is-Fallback-For: es-ar es-bo es-cl es-co es-cr es-do es-ec es-es es-sv es-gt es-hn es-mx es-ni es-pa es-py es-pe es-pr es-us es-uy es-ve\n"

#: plone/restapi/deserializer/dxcontent.py:116
msgid "${field_name} is a required field. Setting it to null is not allowed."
msgstr ""

#: plone/restapi/services/email_send/post.py:88
msgid "${sender_fullname} via ${portal_title}"
msgstr "${sender_fullname} vía ${portal_title}"
Expand Down
6 changes: 5 additions & 1 deletion src/plone/restapi/locales/fr/LC_MESSAGES/plone.restapi.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2023-09-25 20:32+0000\n"
"POT-Creation-Date: 2023-12-22 19:57+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
"Last-Translator: Julien Chandelle <[email protected]>\n"
"Language-Team: French <[email protected]>\n"
Expand All @@ -14,6 +14,10 @@ msgstr ""
"Preferred-Encodings: utf-8 latin1\n"
"Domain: plone.restapi\n"

#: plone/restapi/deserializer/dxcontent.py:116
msgid "${field_name} is a required field. Setting it to null is not allowed."
msgstr ""

#: plone/restapi/services/email_send/post.py:88
msgid "${sender_fullname} via ${portal_title}"
msgstr "${sender_fullname} via ${portal_title}"
Expand Down
6 changes: 5 additions & 1 deletion src/plone/restapi/locales/plone.restapi.pot
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2023-09-25 20:32+0000\n"
"POT-Creation-Date: 2023-12-22 19:57+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,6 +17,10 @@ msgstr ""
"Preferred-Encodings: utf-8 latin1\n"
"Domain: plone.restapi\n"

#: plone/restapi/deserializer/dxcontent.py:116
msgid "${field_name} is a required field. Setting it to null is not allowed."
msgstr ""

#: plone/restapi/services/email_send/post.py:88
msgid "${sender_fullname} via ${portal_title}"
msgstr ""
Expand Down
6 changes: 5 additions & 1 deletion src/plone/restapi/locales/pt_BR/LC_MESSAGES/plone.restapi.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: plone.restapi\n"
"POT-Creation-Date: 2023-09-25 20:32+0000\n"
"POT-Creation-Date: 2023-12-22 19:57+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
"Last-Translator: Wesley Barroso Lopes\n"
"Language-Team: \n"
Expand All @@ -14,6 +14,10 @@ msgstr ""
"Preferred-Encodings: utf-8 latin1\n"
"Domain: plone.restapi\n"

#: plone/restapi/deserializer/dxcontent.py:116
msgid "${field_name} is a required field. Setting it to null is not allowed."
msgstr "${field_name} é um campo obrigatório. Não é permitido defini-lo como nulo."

#: plone/restapi/services/email_send/post.py:88
msgid "${sender_fullname} via ${portal_title}"
msgstr ""
Expand Down
5 changes: 1 addition & 4 deletions src/plone/restapi/tests/test_dxcontent_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ def test_deserializer_sets_missing_value_on_required_field(self):
"valid value", self.portal.doc1.test_missing_value_required_field
)
self.assertEqual(
(
"test_missing_value_required_field is a required field.",
"Setting it to null is not allowed.",
),
"test_missing_value_required_field is a required field. Setting it to null is not allowed.",
cm.exception.args[0][0]["message"],
)
self.assertEqual(
Expand Down