diff --git a/InvenTree/InvenTree/translation.py b/InvenTree/InvenTree/translation.py new file mode 100644 index 0000000000..d965f72df2 --- /dev/null +++ b/InvenTree/InvenTree/translation.py @@ -0,0 +1,50 @@ +"""Translation helper functions""" + +import json + +from django.conf import settings + +# translation completion stats +_translation_stats = None + + +def reload_translation_stats(): + """Reload the translation stats from the compiled file""" + global _translation_stats + + STATS_FILE = settings.BASE_DIR.joinpath('InvenTree/locale_stats.json').absolute() + + try: + with open(STATS_FILE, 'r') as f: + _translation_stats = json.load(f) + except Exception: + _translation_stats = None + return + + keys = _translation_stats.keys() + + # Note that the names used in the stats file may not align 100% + for (code, _lang) in settings.LANGUAGES: + if code in keys: + # Direct match, move on + continue + + code_lower = code.lower().replace('-', '_') + + for k in keys: + if k.lower() == code_lower: + # Make a copy of the code which matches + _translation_stats[code] = _translation_stats[k] + break + + +def get_translation_percent(lang_code): + """Return the translation percentage for the given language code""" + + if _translation_stats is None: + reload_translation_stats() + + if _translation_stats is None: + return 0 + + return _translation_stats.get(lang_code, 0) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 838e0d7f1b..f21d6ea8fb 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -4,9 +4,6 @@ In particular these views provide base functionality for rendering Django forms as JSON objects and passing them to modal forms (using jQuery / bootstrap). """ -import json - -from django.conf import settings from django.contrib.auth import password_validation from django.contrib.auth.mixins import (LoginRequiredMixin, PermissionRequiredMixin) @@ -531,14 +528,6 @@ class SettingsView(TemplateView): except Exception: ctx["rates_updated"] = None - # load locale stats - STAT_FILE = settings.BASE_DIR.joinpath('InvenTree/locale_stats.json').absolute() - - try: - ctx["locale_stats"] = json.load(open(STAT_FILE, 'r')) - except Exception: - ctx["locale_stats"] = {} - # Forms and context for allauth ctx['add_email_form'] = AddEmailForm ctx["can_add_email"] = EmailAddress.objects.can_add_email(self.request.user) diff --git a/InvenTree/part/templatetags/i18n.py b/InvenTree/part/templatetags/i18n.py index fcd9df4628..ac74a07713 100644 --- a/InvenTree/part/templatetags/i18n.py +++ b/InvenTree/part/templatetags/i18n.py @@ -10,9 +10,21 @@ from django.templatetags.i18n import TranslateNode import bleach +import InvenTree.translation + register = template.Library() +@register.simple_tag() +def translation_stats(lang_code): + """Return the translation percentage for the given language code""" + + if lang_code is None: + return None + + return InvenTree.translation.get_translation_percent(lang_code) + + class CustomTranslateNode(TranslateNode): """Custom translation node class, which sanitizes the translated strings for javascript use""" diff --git a/InvenTree/templates/InvenTree/settings/user_display.html b/InvenTree/templates/InvenTree/settings/user_display.html index 8bec3e7c1e..7ae7ae19e3 100644 --- a/InvenTree/templates/InvenTree/settings/user_display.html +++ b/InvenTree/templates/InvenTree/settings/user_display.html @@ -74,7 +74,7 @@ {% if 'alllang' in request.GET %}{% define True as ALL_LANG %}{% endif %} {% for language in languages %} {% define language.code as lang_code %} - {% define locale_stats|keyvalue:lang_code as lang_translated %} + {% translation_stats lang_code as lang_translated %} {% if lang_translated > 10 or lang_code == 'en' or lang_code == LANGUAGE_CODE %}{% define True as use_lang %}{% else %}{% define False as use_lang %}{% endif %} {% if ALL_LANG or use_lang %}