From f396642d163c2ce5d76bb0e89908b35e86c29b55 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 14 Jan 2024 13:29:36 +1100 Subject: [PATCH] Fix escape codes in translated strings (#6234) * Fix escape codes in translated strings - Only add escape characters for javascript files * import fix * more import fix --- InvenTree/InvenTree/api.py | 2 +- .../{part => InvenTree}/templatetags/__init__.py | 0 InvenTree/{part => InvenTree}/templatetags/i18n.py | 14 +++++++++----- .../templatetags/inventree_extras.py | 0 InvenTree/{part => InvenTree}/templatetags/sso.py | 0 InvenTree/part/test_part.py | 2 +- tasks.py | 9 +++++---- 7 files changed, 16 insertions(+), 11 deletions(-) rename InvenTree/{part => InvenTree}/templatetags/__init__.py (100%) rename InvenTree/{part => InvenTree}/templatetags/i18n.py (89%) rename InvenTree/{part => InvenTree}/templatetags/inventree_extras.py (100%) rename InvenTree/{part => InvenTree}/templatetags/sso.py (100%) diff --git a/InvenTree/InvenTree/api.py b/InvenTree/InvenTree/api.py index e8d2c2c465..29b233e707 100644 --- a/InvenTree/InvenTree/api.py +++ b/InvenTree/InvenTree/api.py @@ -17,7 +17,7 @@ import users.models from InvenTree.filters import SEARCH_ORDER_FILTER from InvenTree.mixins import ListCreateAPI from InvenTree.permissions import RolePermission -from part.templatetags.inventree_extras import plugins_info +from InvenTree.templatetags.inventree_extras import plugins_info from plugin.serializers import MetadataSerializer from users.models import ApiToken diff --git a/InvenTree/part/templatetags/__init__.py b/InvenTree/InvenTree/templatetags/__init__.py similarity index 100% rename from InvenTree/part/templatetags/__init__.py rename to InvenTree/InvenTree/templatetags/__init__.py diff --git a/InvenTree/part/templatetags/i18n.py b/InvenTree/InvenTree/templatetags/i18n.py similarity index 89% rename from InvenTree/part/templatetags/i18n.py rename to InvenTree/InvenTree/templatetags/i18n.py index bbbc376332..f313947c02 100644 --- a/InvenTree/part/templatetags/i18n.py +++ b/InvenTree/InvenTree/templatetags/i18n.py @@ -41,9 +41,12 @@ class CustomTranslateNode(TranslateNode): for c in ['\\', '`', ';', '|', '&']: result = result.replace(c, '') - # Escape any quotes contained in the string - result = result.replace("'", r'\'') - result = result.replace('"', r'\"') + # Escape any quotes contained in the string, if the request is for a javascript file + request = context.get('request', None) + + if request and request.path.endswith('.js'): + result = result.replace("'", r'\'') + result = result.replace('"', r'\"') # Return the 'clean' resulting string return result @@ -52,9 +55,10 @@ class CustomTranslateNode(TranslateNode): @register.tag('translate') @register.tag('trans') def do_translate(parser, token): - """Custom translation function, lifted from https://github.com/django/django/blob/main/django/templatetags/i18n.py. + """Custom translation function. - The only difference is that we pass this to our custom rendering node class + - Lifted from https://github.com/django/django/blob/main/django/templatetags/i18n.py. + - The only difference is that we pass this to our custom rendering node class """ bits = token.split_contents() if len(bits) < 2: diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/InvenTree/templatetags/inventree_extras.py similarity index 100% rename from InvenTree/part/templatetags/inventree_extras.py rename to InvenTree/InvenTree/templatetags/inventree_extras.py diff --git a/InvenTree/part/templatetags/sso.py b/InvenTree/InvenTree/templatetags/sso.py similarity index 100% rename from InvenTree/part/templatetags/sso.py rename to InvenTree/InvenTree/templatetags/sso.py diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py index 3a39455850..c1865b8366 100644 --- a/InvenTree/part/test_part.py +++ b/InvenTree/part/test_part.py @@ -18,6 +18,7 @@ from common.models import ( ) from common.notifications import UIMessageNotification, storage from InvenTree import version +from InvenTree.templatetags import inventree_extras from InvenTree.unit_test import InvenTreeTestCase from .models import ( @@ -30,7 +31,6 @@ from .models import ( PartTestTemplate, rename_part_image, ) -from .templatetags import inventree_extras class TemplateTagTest(InvenTreeTestCase): diff --git a/tasks.py b/tasks.py index dd9dd8f795..9a3088ce6e 100644 --- a/tasks.py +++ b/tasks.py @@ -292,23 +292,24 @@ def translate_stats(c): @task(post=[translate_stats]) -def translate(c): +def translate(c, ignore_static=False, no_frontend=False): """Rebuild translation source files. Advanced use only! Note: This command should not be used on a local install, it is performed as part of the InvenTree translation toolchain. """ - # Translate applicable .py / .html / .js / .tsx files + # Translate applicable .py / .html / .js files manage(c, 'makemessages --all -e py,html,js --no-wrap') manage(c, 'compilemessages') - if node_available(): + if not no_frontend and node_available(): frontend_install(c) frontend_trans(c) frontend_build(c) # Update static files - static(c) + if not ignore_static: + static(c) @task