mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
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:
parent
4a46e0321f
commit
034cc4b983
50
InvenTree/InvenTree/translation.py
Normal file
50
InvenTree/InvenTree/translation.py
Normal 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)
|
@ -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)
|
||||
|
@ -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"""
|
||||
|
||||
|
@ -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 %}>
|
||||
|
Loading…
Reference in New Issue
Block a user