Repository: translate/pootle

Issue:
Overview dropdown is using ISO639 names and not simplified toolkit names
E.g. nso is using "Pedi; Sepedi; Nothern Sotho" instead of simply 'Northern Sotho'



Discussion:

Resolving patch:
diff --git a/pootle/apps/pootle_language/models.py b/pootle/apps/pootle_language/models.py
--- a/pootle/apps/pootle_language/models.py
+++ b/pootle/apps/pootle_language/models.py
@@ -7,6 +7,7 @@
 # or later license. See the LICENSE file for a copy of the license and the
 # AUTHORS file for copyright and authorship information.
 
+import locale
 from collections import OrderedDict
 
 from django.conf import settings
@@ -17,15 +18,12 @@
 from django.dispatch import receiver
 from django.utils.translation import ugettext_lazy as _
 
+from pootle.core.cache import make_method_key
 from pootle.core.mixins import TreeItem
 from pootle.core.url_helpers import get_editor_filter
 from pootle.i18n.gettext import tr_lang, language_dir
 
 
-# FIXME: Generate key dynamically
-CACHE_KEY = 'pootle-languages'
-
-
 class LanguageManager(models.Manager):
 
     def get_queryset(self):
@@ -51,13 +49,23 @@ def get_queryset(self):
                 project__isnull=True,
             ).distinct()
 
-    def cached_dict(self):
-        languages = cache.get(CACHE_KEY)
-        if not languages:
+    def cached_dict(self, locale_code='en-us'):
+        """Retrieves a sorted list of live language codes and names.
+
+        :param locale_code: the UI locale for which language full names need to
+            be localized.
+        :return: an `OrderedDict`
+        """
+        key = make_method_key(self, 'cached_dict', locale_code)
+        languages = cache.get(key, None)
+        if languages is None:
             languages = OrderedDict(
-                self.order_by('fullname').values_list('code', 'fullname')
+                sorted([(lang[0], tr_lang(lang[1]))
+                        for lang in self.values_list('code', 'fullname')],
+                        cmp=locale.strcoll,
+                        key=lambda x: x[1])
             )
-            cache.set(CACHE_KEY, languages, settings.OBJECT_CACHE_TIMEOUT)
+            cache.set(key, languages, settings.OBJECT_CACHE_TIMEOUT)
 
         return languages
 
@@ -166,4 +174,5 @@ def invalidate_language_list_cache(sender, instance, **kwargs):
     if instance.__class__.__name__ not in ['Language', 'TranslationProject']:
         return
 
-    cache.delete(CACHE_KEY)
+    key = make_method_key('LiveLanguageManager', 'cached_dict', '*')
+    cache.delete_pattern(key)
diff --git a/pootle/apps/pootle_misc/context_processors.py b/pootle/apps/pootle_misc/context_processors.py
--- a/pootle/apps/pootle_misc/context_processors.py
+++ b/pootle/apps/pootle_misc/context_processors.py
@@ -8,6 +8,7 @@
 # AUTHORS file for copyright and authorship information.
 
 from django.conf import settings
+from django.utils import translation
 
 from pootle.__version__ import sver
 from pootle_language.models import Language
@@ -43,7 +44,7 @@ def pootle_context(request):
             'DEBUG': settings.DEBUG,
         },
         'custom': settings.CUSTOM_TEMPLATE_CONTEXT,
-        'ALL_LANGUAGES': Language.live.cached_dict(),
+        'ALL_LANGUAGES': Language.live.cached_dict(translation.get_language()),
         'ALL_PROJECTS': Project.objects.cached_dict(request.user),
         'display_agreement': _agreement_context(request),
     }


Test patch:
diff --git a/tests/fixtures/cache.py b/tests/fixtures/cache.py
new file mode 100644
--- /dev/null
+++ b/tests/fixtures/cache.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) Pootle contributors.
+#
+# This file is a part of the Pootle project. It is distributed under the GPL3
+# or later license. See the LICENSE file for a copy of the license and the
+# AUTHORS file for copyright and authorship information.
+
+import pytest
+
+
+@pytest.fixture(scope='session', autouse=True)
+def delete_pattern():
+    """Adds the no-op `delete_pattern()` method to `LocMemCache`."""
+    from django.core.cache.backends.locmem import LocMemCache
+    LocMemCache.delete_pattern = lambda x, y: 0