i18n: Move locale select logic in home to i18n.py.

Part of #16094.
Moved the language selection preference logic from home.py to a new
function in i18n.py to avoid repetition in analytics views and home
views.
This commit is contained in:
Abhijeet Prasad Bodas
2020-10-02 17:53:45 +05:30
committed by Tim Abbott
parent 55a67ee7c5
commit a20d22de43
2 changed files with 28 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ from two_factor.utils import default_device
from zerver.lib.events import do_events_register from zerver.lib.events import do_events_register
from zerver.lib.i18n import ( from zerver.lib.i18n import (
get_and_set_request_language,
get_language_list, get_language_list,
get_language_list_for_templates, get_language_list_for_templates,
get_language_name, get_language_name,
@@ -136,17 +137,11 @@ def build_page_params_for_home_page_load(
furthest_read_time = get_furthest_read_time(user_profile) furthest_read_time = get_furthest_read_time(user_profile)
# We pick a language for the user as follows: request_language = get_and_set_request_language(
# * First priority is the language in the URL, for debugging. request,
# * If not in the URL, we use the language from the user's settings. register_ret['default_language'],
request_language = translation.get_language_from_path(request.path_info) translation.get_language_from_path(request.path_info)
if request_language is None: )
request_language = register_ret['default_language']
translation.activate(request_language)
# We also save the language to the user's session, so that
# something reasonable will happen in logged-in portico pages.
request.session[translation.LANGUAGE_SESSION_KEY] = translation.get_language()
two_fa_enabled = ( two_fa_enabled = (
settings.TWO_FACTOR_AUTHENTICATION_ENABLED and user_profile is not None settings.TWO_FACTOR_AUTHENTICATION_ENABLED and user_profile is not None

View File

@@ -5,10 +5,12 @@ import operator
import os import os
from functools import lru_cache from functools import lru_cache
from itertools import zip_longest from itertools import zip_longest
from typing import Any, Dict, List from typing import Any, Dict, List, Optional
import orjson import orjson
from django.conf import settings from django.conf import settings
from django.http import HttpRequest
from django.utils import translation
@lru_cache() @lru_cache()
@@ -84,3 +86,22 @@ def get_language_translation_data(language: str) -> Dict[str, str]:
except FileNotFoundError: except FileNotFoundError:
print(f'Translation for {language} not found at {path}') print(f'Translation for {language} not found at {path}')
return {} return {}
def get_and_set_request_language(
request: HttpRequest,
user_configured_language: str,
testing_url_language: Optional[str]=None
) -> str:
# We pick a language for the user as follows:
# * First priority is the language in the URL, for debugging.
# * If not in the URL, we use the language from the user's settings.
request_language = testing_url_language
if request_language is None:
request_language = user_configured_language
translation.activate(request_language)
# We also save the language to the user's session, so that
# something reasonable will happen in logged-in portico pages.
request.session[translation.LANGUAGE_SESSION_KEY] = translation.get_language()
return request_language