mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +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:
		
							
								
								
									
										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).
 | 
					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 import password_validation
 | 
				
			||||||
from django.contrib.auth.mixins import (LoginRequiredMixin,
 | 
					from django.contrib.auth.mixins import (LoginRequiredMixin,
 | 
				
			||||||
                                        PermissionRequiredMixin)
 | 
					                                        PermissionRequiredMixin)
 | 
				
			||||||
@@ -531,14 +528,6 @@ class SettingsView(TemplateView):
 | 
				
			|||||||
        except Exception:
 | 
					        except Exception:
 | 
				
			||||||
            ctx["rates_updated"] = None
 | 
					            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
 | 
					        # Forms and context for allauth
 | 
				
			||||||
        ctx['add_email_form'] = AddEmailForm
 | 
					        ctx['add_email_form'] = AddEmailForm
 | 
				
			||||||
        ctx["can_add_email"] = EmailAddress.objects.can_add_email(self.request.user)
 | 
					        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 bleach
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import InvenTree.translation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
register = template.Library()
 | 
					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):
 | 
					class CustomTranslateNode(TranslateNode):
 | 
				
			||||||
    """Custom translation node class, which sanitizes the translated strings for javascript use"""
 | 
					    """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 %}
 | 
					                    {% if 'alllang' in request.GET %}{% define True as ALL_LANG %}{% endif %}
 | 
				
			||||||
                    {% for language in languages %}
 | 
					                    {% for language in languages %}
 | 
				
			||||||
                        {% define language.code as lang_code %}
 | 
					                        {% 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 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 %}
 | 
					                        {% if ALL_LANG or use_lang %}
 | 
				
			||||||
                        <option value="{{ lang_code }}"{% if lang_code == LANGUAGE_CODE %} selected{% endif %}>
 | 
					                        <option value="{{ lang_code }}"{% if lang_code == LANGUAGE_CODE %} selected{% endif %}>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user