From 2faa425caf1ff90f0a1824e84d16d8050e7fd9d3 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Thu, 28 Jan 2021 22:06:02 +0100 Subject: [PATCH] localization for websockets --- src-ui/messages.xlf | 91 +++++++++++++++++++ .../app/services/consumer-status.service.ts | 22 ++++- src/documents/consumer.py | 50 ++++++---- src/documents/parsers.py | 5 + src/locale/en-us/LC_MESSAGES/django.po | 64 ++----------- 5 files changed, 154 insertions(+), 78 deletions(-) diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf index 93f970d86..eab8ed80e 100644 --- a/src-ui/messages.xlf +++ b/src-ui/messages.xlf @@ -1617,6 +1617,97 @@ 85 + + Document already exists. + + src/app/services/consumer-status.service.ts + 14 + + + + File not found. + + src/app/services/consumer-status.service.ts + 15 + + + + Pre-consume script does not exist. + + src/app/services/consumer-status.service.ts + 16 + + + + Error while executing pre-consume script. + + src/app/services/consumer-status.service.ts + 17 + + + + Post-consume script does not exist. + + src/app/services/consumer-status.service.ts + 18 + + + + Error while executing post-consume script. + + src/app/services/consumer-status.service.ts + 19 + + + + Received new file. + + src/app/services/consumer-status.service.ts + 20 + + + + File type not supported. + + src/app/services/consumer-status.service.ts + 21 + + + + Processing document... + + src/app/services/consumer-status.service.ts + 22 + + + + Generating thumbnail... + + src/app/services/consumer-status.service.ts + 23 + + + + Retrieving date from document... + + src/app/services/consumer-status.service.ts + 24 + + + + Saving document... + + src/app/services/consumer-status.service.ts + 25 + + + + Finished. + + src/app/services/consumer-status.service.ts + 26 + + Error diff --git a/src-ui/src/app/services/consumer-status.service.ts b/src-ui/src/app/services/consumer-status.service.ts index 8151b1c18..026c3c64f 100644 --- a/src-ui/src/app/services/consumer-status.service.ts +++ b/src-ui/src/app/services/consumer-status.service.ts @@ -10,6 +10,22 @@ export enum FileStatusPhase { FAILED = 4 } +export const FILE_STATUS_MESSAGES = { + "document_already_exists": $localize`Document already exists.`, + "file_not_found": $localize`File not found.`, + "pre_consume_script_not_found": $localize`Pre-consume script does not exist.`, + "pre_consume_script_error": $localize`Error while executing pre-consume script.`, + "post_consume_script_not_found": $localize`Post-consume script does not exist.`, + "post_consume_script_error": $localize`Error while executing post-consume script.`, + "new_file": $localize`Received new file.`, + "unsupported_type": $localize`File type not supported.`, + "parsing_document": $localize`Processing document...`, + "generating_thumbnail": $localize`Generating thumbnail...`, + "parse_date": $localize`Retrieving date from document...`, + "save_document": $localize`Saving document...`, + "finished": $localize`Finished.` +} + export class FileStatus { filename: string @@ -116,7 +132,11 @@ export class ConsumerStatusService { let created = statusMessageGet.created status.updateProgress(FileStatusPhase.PROCESSING, statusMessage.current_progress, statusMessage.max_progress) - status.message = statusMessage.message + if (statusMessage.message && statusMessage.message in FILE_STATUS_MESSAGES) { + status.message = FILE_STATUS_MESSAGES[statusMessage.message] + } else if (statusMessage.message) { + status.message = statusMessage.message + } status.documentId = statusMessage.document_id if (created && statusMessage.status == 'STARTING') { diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 05043201b..146b11014 100755 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -25,17 +25,30 @@ from .signals import ( document_consumption_started ) -from django.utils.translation import gettext as _ - class ConsumerError(Exception): pass +MESSAGE_DOCUMENT_ALREADY_EXISTS = "document_already_exists" +MESSAGE_FILE_NOT_FOUND = "file_not_found" +MESSAGE_PRE_CONSUME_SCRIPT_NOT_FOUND = "pre_consume_script_not_found" +MESSAGE_PRE_CONSUME_SCRIPT_ERROR = "pre_consume_script_error" +MESSAGE_POST_CONSUME_SCRIPT_NOT_FOUND = "post_consume_script_not_found" +MESSAGE_POST_CONSUME_SCRIPT_ERROR = "post_consume_script_error" +MESSAGE_NEW_FILE = "new_file" +MESSAGE_UNSUPPORTED_TYPE = "unsupported_type" +MESSAGE_PARSING_DOCUMENT = "parsing_document" +MESSAGE_GENERATING_THUMBNAIL = "generating_thumbnail" +MESSAGE_PARSE_DATE = "parse_date" +MESSAGE_SAVE_DOCUMENT = "save_document" +MESSAGE_FINISHED = "finished" + + class Consumer(LoggingMixin): def _send_progress(self, current_progress, max_progress, status, - message, document_id=None): + message=None, document_id=None): payload = { 'filename': os.path.basename(self.filename) if self.filename else None, # NOQA: E501 'task_id': self.task_id, @@ -69,7 +82,7 @@ class Consumer(LoggingMixin): def pre_check_file_exists(self): if not os.path.isfile(self.path): self._fail( - _("File not found"), + MESSAGE_FILE_NOT_FOUND, f"Cannot consume {self.path}: File not found." ) @@ -80,7 +93,7 @@ class Consumer(LoggingMixin): if settings.CONSUMER_DELETE_DUPLICATES: os.unlink(self.path) self._fail( - _("Document already exists"), + MESSAGE_DOCUMENT_ALREADY_EXISTS, f"Not consuming {self.filename}: It is a duplicate." ) @@ -96,7 +109,7 @@ class Consumer(LoggingMixin): if not os.path.isfile(settings.PRE_CONSUME_SCRIPT): self._fail( - _("Pre-consume script does not exist."), + MESSAGE_PRE_CONSUME_SCRIPT_NOT_FOUND, f"Configured pre-consume script " f"{settings.PRE_CONSUME_SCRIPT} does not exist.") @@ -104,7 +117,7 @@ class Consumer(LoggingMixin): Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait() except Exception as e: self._fail( - _("Error while executing pre-consume script"), + MESSAGE_PRE_CONSUME_SCRIPT_ERROR, f"Error while executing pre-consume script: {e}" ) @@ -114,7 +127,7 @@ class Consumer(LoggingMixin): if not os.path.isfile(settings.POST_CONSUME_SCRIPT): self._fail( - _("Post-consume script does not exist."), + MESSAGE_POST_CONSUME_SCRIPT_NOT_FOUND, f"Configured post-consume script " f"{settings.POST_CONSUME_SCRIPT} does not exist." ) @@ -134,7 +147,7 @@ class Consumer(LoggingMixin): )).wait() except Exception as e: self._fail( - _("Error while executing post-consume script"), + MESSAGE_POST_CONSUME_SCRIPT_ERROR, f"Error while executing post-consume script: {e}" ) @@ -158,7 +171,7 @@ class Consumer(LoggingMixin): self.override_tag_ids = override_tag_ids self.task_id = task_id or str(uuid.uuid4()) - self._send_progress(0, 100, 'STARTING', _('Received new file')) + self._send_progress(0, 100, 'STARTING', MESSAGE_NEW_FILE) # this is for grouping logging entries for this particular file # together. @@ -182,8 +195,7 @@ class Consumer(LoggingMixin): parser_class = get_parser_class_for_mime_type(mime_type) if not parser_class: self._fail( - _("File type %(type)s not supported") % - {'type': mime_type}, + MESSAGE_UNSUPPORTED_TYPE, f"Unsupported mime type {mime_type}" ) else: @@ -199,10 +211,10 @@ class Consumer(LoggingMixin): self.run_pre_consume_script() - def progress_callback(current_progress, max_progress, message): + def progress_callback(current_progress, max_progress): # recalculate progress to be within 20 and 80 p = int((current_progress / max_progress) * 50 + 20) - self._send_progress(p, 100, "WORKING", message) + self._send_progress(p, 100, "WORKING") # This doesn't parse the document yet, but gives us a parser. @@ -219,13 +231,13 @@ class Consumer(LoggingMixin): archive_path = None try: - self._send_progress(20, 100, 'WORKING', _('Parsing document...')) + self._send_progress(20, 100, 'WORKING', MESSAGE_PARSING_DOCUMENT) self.log("debug", "Parsing {}...".format(self.filename)) document_parser.parse(self.path, mime_type, self.filename) self.log("debug", f"Generating thumbnail for {self.filename}...") self._send_progress(70, 100, 'WORKING', - _('Generating thumbnail...')) + MESSAGE_GENERATING_THUMBNAIL) thumbnail = document_parser.get_optimised_thumbnail( self.path, mime_type) @@ -233,7 +245,7 @@ class Consumer(LoggingMixin): date = document_parser.get_date() if not date: self._send_progress(90, 100, 'WORKING', - _('Getting date from document...')) + MESSAGE_PARSE_DATE) date = parse_date(self.filename, text) archive_path = document_parser.get_archive_path() @@ -258,7 +270,7 @@ class Consumer(LoggingMixin): "warning", f"Cannot classify documents: {e}.") classifier = None - self._send_progress(95, 100, 'WORKING', _('Saving document...')) + self._send_progress(95, 100, 'WORKING', MESSAGE_SAVE_DOCUMENT) # now that everything is done, we can start to store the document # in the system. This will be a transaction and reasonably fast. try: @@ -327,7 +339,7 @@ class Consumer(LoggingMixin): "Document {} consumption finished".format(document) ) - self._send_progress(100, 100, 'SUCCESS', _('Finished.'), document.id) + self._send_progress(100, 100, 'SUCCESS', MESSAGE_FINISHED, document.id) return document diff --git a/src/documents/parsers.py b/src/documents/parsers.py index ddad6897a..3f0879b3c 100644 --- a/src/documents/parsers.py +++ b/src/documents/parsers.py @@ -273,6 +273,11 @@ class DocumentParser(LoggingMixin): self.date = None self.progress_callback = progress_callback + def progress(self, current, max): + print(self.progress_callback) + if self.progress_callback: + self.progress_callback(current, max) + def extract_metadata(self, document_path, mime_type): return [] diff --git a/src/locale/en-us/LC_MESSAGES/django.po b/src/locale/en-us/LC_MESSAGES/django.po index 8513c577b..fdf3fd809 100644 --- a/src/locale/en-us/LC_MESSAGES/django.po +++ b/src/locale/en-us/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-01-27 17:57+0100\n" +"POT-Creation-Date: 2021-01-28 22:02+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -21,60 +21,6 @@ msgstr "" msgid "Documents" msgstr "" -#: documents/consumer.py:72 -msgid "File not found" -msgstr "Datei nicht gefunden" - -#: documents/consumer.py:83 -msgid "Document already exists" -msgstr "" - -#: documents/consumer.py:99 -msgid "Pre-consume script does not exist." -msgstr "" - -#: documents/consumer.py:107 -msgid "Error while executing pre-consume script" -msgstr "" - -#: documents/consumer.py:117 -msgid "Post-consume script does not exist." -msgstr "" - -#: documents/consumer.py:137 -msgid "Error while executing post-consume script" -msgstr "" - -#: documents/consumer.py:161 -msgid "Received new file" -msgstr "" - -#: documents/consumer.py:185 documents/serialisers.py:383 -#, fuzzy, python-format -#| msgid "File type {type} not supported." -msgid "File type %(type)s not supported" -msgstr "Dateityp {type} wird nicht unterstützt" - -#: documents/consumer.py:222 -msgid "Parsing document..." -msgstr "" - -#: documents/consumer.py:228 -msgid "Generating thumbnail..." -msgstr "" - -#: documents/consumer.py:236 -msgid "Getting date from document..." -msgstr "" - -#: documents/consumer.py:261 -msgid "Saving document..." -msgstr "" - -#: documents/consumer.py:330 -msgid "Finished." -msgstr "" - #: documents/models.py:33 msgid "Any word" msgstr "" @@ -392,6 +338,11 @@ msgstr "" msgid "filter rules" msgstr "" +#: documents/serialisers.py:383 +#, python-format +msgid "File type %(type)s not supported" +msgstr "" + #: documents/templates/index.html:20 msgid "Paperless-ng is loading..." msgstr "" @@ -674,6 +625,3 @@ msgstr "" #: paperless_mail/models.py:205 msgid "assign this correspondent" msgstr "" - -#~ msgid "Document is a duplicate" -#~ msgstr "Dokument existiert bereits"