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
This commit is contained in:
Oliver 2024-01-14 13:29:36 +11:00 committed by GitHub
parent d2d59e0709
commit f396642d16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 11 deletions

View File

@ -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

View File

@ -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:

View File

@ -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):

View File

@ -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