Repository: inventree/InvenTree

Issue:
Tranlsation improvments
@SchrodingersGat we have a lot of translations that are barely started. I would like to try fixing that with two small UI changes that might encourage users to submit translations:
- [x] showing the percentage of translated strings int he language selection box
- [x] Adding the information how to translate stuff to the selection box itself (maybe with a setting to disable that)

A more proactive step would be to ask admin-users with a language that is not translated a lot to consider submitting translations - I do not think that would find support but would be a possibility.  
I just think we should do something because languages with 10% translations are not helping anyone.


Discussion:
That's a nice idea. Any way we can encourage translation efforts would be great.

Resolving patch:
diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py
--- a/InvenTree/InvenTree/views.py
+++ b/InvenTree/InvenTree/views.py
@@ -7,12 +7,15 @@
 
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
+import os
+import json
 
 from django.utils.translation import gettext_lazy as _
 from django.template.loader import render_to_string
 from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
 from django.urls import reverse_lazy
 from django.shortcuts import redirect
+from django.conf import settings
 
 from django.contrib.auth.mixins import PermissionRequiredMixin
 
@@ -802,6 +805,13 @@ def get_context_data(self, **kwargs):
         except:
             ctx["rates_updated"] = None
 
+        # load locale stats
+        STAT_FILE = os.path.abspath(os.path.join(settings.BASE_DIR, 'InvenTree/locale_stats.json'))
+        try:
+            ctx["locale_stats"] = json.load(open(STAT_FILE, 'r'))
+        except:
+            ctx["locale_stats"] = {}
+
         return ctx
 
 
diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py
--- a/InvenTree/part/templatetags/inventree_extras.py
+++ b/InvenTree/part/templatetags/inventree_extras.py
@@ -269,7 +269,7 @@ def keyvalue(dict, key):
     usage:
     {% mydict|keyvalue:mykey %}
     """
-    return dict[key]
+    return dict.get(key)
 
 
 @register.simple_tag()
diff --git a/InvenTree/script/translation_stats.py b/InvenTree/script/translation_stats.py
--- a/InvenTree/script/translation_stats.py
+++ b/InvenTree/script/translation_stats.py
@@ -3,6 +3,7 @@
 """
 
 import os
+import json
 
 
 def calculate_coverage(filename):
@@ -36,8 +37,10 @@ def calculate_coverage(filename):
 
     MY_DIR = os.path.dirname(os.path.realpath(__file__))
     LC_DIR = os.path.abspath(os.path.join(MY_DIR, '..', 'locale'))
+    STAT_FILE = os.path.abspath(os.path.join(MY_DIR, '..', 'InvenTree/locale_stats.json'))
 
     locales = {}
+    locales_perc = {}
 
     print("InvenTree translation coverage:")
 
@@ -64,5 +67,10 @@ def calculate_coverage(filename):
             percentage = 0
 
         print(f"| {locale.ljust(4, ' ')} : {str(percentage).rjust(4, ' ')}% |")
+        locales_perc[locale] = percentage
 
     print("-" * 16)
+
+    # write locale stats
+    with open(STAT_FILE, 'w') as target:
+        json.dump(locales_perc, target)


Test patch: