From 19acb40c7bf3eb0cd1db09e5ee2593d5e5d0bd3a Mon Sep 17 00:00:00 2001 From: Exequiel Arona Date: Thu, 25 Apr 2024 12:56:39 -0300 Subject: [PATCH] feat: add extractor for custom fields in fixtures --- frappe/gettext/extractors/custom_fields.py | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 frappe/gettext/extractors/custom_fields.py diff --git a/frappe/gettext/extractors/custom_fields.py b/frappe/gettext/extractors/custom_fields.py new file mode 100644 index 0000000000..b4a8dcdb85 --- /dev/null +++ b/frappe/gettext/extractors/custom_fields.py @@ -0,0 +1,66 @@ +import json + +EXCLUDE_SELECT_OPTIONS = [ + "naming_series", + "number_format", + "float_precision", + "currency_precision", + "minimum_password_score", +] + +def extract(fileobj, *args, **kwargs): + """ + Extract messages from DocType JSON files. To be used to babel extractor + :param fileobj: the file-like object the messages should be extracted from + :rtype: `iterator` + """ + data = json.load(fileobj) + + if not isinstance(data, list): + return + + messages = [] + fields = data + + for field in fields: + print(field) + fieldtype = field.get("fieldtype") + fieldname = field.get("fieldname") + label = field.get("label") + doctype = field.get("dt") + + if label: + messages.append((label, f"Label of a {fieldtype} Custom Field in DocType '{doctype}'")) + _label = label + else: + _label = fieldname + + if description := field.get("description"): + messages.append( + (description, f"Description of the '{_label}' ({fieldtype}) Custom Field in DocType '{doctype}'") + ) + + if message := field.get("options"): + if fieldtype == "Select": + if fieldname in EXCLUDE_SELECT_OPTIONS: + continue + + select_options = [option for option in message.split("\n") if option and not option.isdigit()] + + if select_options and "icon" in select_options[0]: + continue + + messages.extend( + ( + option, + f"Option for the '{_label}' ({fieldtype}) Custom Field in DocType '{doctype}'", + ) + for option in select_options + ) + elif fieldtype == "HTML": + messages.append( + (message, f"Content of the '{_label}' ({fieldtype}) Custom Field in DocType '{doctype}'") + ) + + # By using "pgettext" as the function name we can supply the doctype as context + yield from ((None, "pgettext", (doctype, message), [comment]) for message, comment in messages)