2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-20 22:06:28 +00:00

initial implementation as in #1485

This commit is contained in:
2021-04-20 13:37:19 +02:00
parent 157d58b157
commit 2943dc6083
6 changed files with 120 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import os
from django import template
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.templatetags.static import StaticNode
from InvenTree import version, settings
import InvenTree.helpers
@ -180,3 +181,31 @@ def object_link(url_name, pk, ref):
ref_url = reverse(url_name, kwargs={'pk': pk})
return mark_safe('<b><a href="{}">{}</a></b>'.format(ref_url, ref))
class I18nStaticNode(StaticNode):
"""
custom StaticNode
replaces a variable named *lng* in the path with the current language
"""
def render(self, context):
self.path.var = self.path.var.format(lng=context.request.LANGUAGE_CODE)
ret = super().render(context)
return ret
@register.tag('i18n_static')
def do_i18n_static(parser, token):
"""
Overrides normal static, adds language - lookup for prerenderd files #1485
usage (like static):
{% i18n_static path [as varname] %}
"""
bits = token.split_contents()
loc_name = settings.STATICFILES_I18_PREFIX
# change path to called ressource
bits[1] = f"'{loc_name}/{{lng}}.{bits[1][1:-1]}'"
token.contents = ' '.join(bits)
return I18nStaticNode.handle_token(parser, token)