Translation stat fix (#5250)

* Add helper function for translation coverage

- Handle case where locale name does not match exactly

* remove debug statement

* Fixes
This commit is contained in:
Oliver 2023-07-15 08:38:39 +10:00 committed by GitHub
parent 4a46e0321f
commit 034cc4b983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 12 deletions

View File

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

View File

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

View File

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

View File

@ -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 %}
<option value="{{ lang_code }}"{% if lang_code == LANGUAGE_CODE %} selected{% endif %}>