mirror of
https://github.com/zulip/zulip.git
synced 2025-10-29 02:53:52 +00:00
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:
@@ -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]]]
|
||||
|
||||
Reference in New Issue
Block a user