i18n: Optimize get_language_list().

compilemessages command now does all the heavy lifting by creating a
language_name_map.json file under locale directory. This file is used
by get_language_list to retrieve the require information.

Fixes: #6486
This commit is contained in:
Umair Khan
2017-09-13 10:04:22 +05:00
committed by Tim Abbott
parent df70fa962e
commit 1f93c06b76
3 changed files with 32 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import operator
from django.conf import settings
from django.utils import translation
from django.utils.translation import ugettext as _
from django.utils.lru_cache import lru_cache
from six.moves import urllib, zip_longest, zip, range
from typing import Any, List, Dict, Optional, Text
@@ -14,24 +15,23 @@ import ujson
def with_language(string, language):
# type: (Text, Text) -> Text
"""
This is an expensive function. If you are using it in a loop, it will
make your code slow.
"""
old_language = translation.get_language()
translation.activate(language)
result = _(string)
translation.activate(old_language)
return result
@lru_cache()
def get_language_list():
# type: () -> List[Dict[str, Any]]
path = os.path.join(settings.STATIC_ROOT, 'locale', 'language_options.json')
path = os.path.join(settings.STATIC_ROOT, 'locale', 'language_name_map.json')
with open(path, 'r') as reader:
languages = ujson.load(reader)
lang_list = []
for lang_info in languages['languages']:
name = lang_info['name']
lang_info['name'] = with_language(name, lang_info['code'])
lang_list.append(lang_info)
return sorted(lang_list, key=lambda i: i['name'])
return languages['name_map']
def get_language_list_for_templates(default_language):
# type: (Text) -> List[Dict[str, Dict[str, str]]]