i18n: Pass translation data in page_params.

This is a preliminary step to make translation_data available as a
resource to `i18next.init()`.
This commit is contained in:
Shubham Dhama 2018-05-03 14:38:50 +05:30 committed by Tim Abbott
parent 647c63050f
commit 1d9afc7328
3 changed files with 33 additions and 1 deletions

View File

@ -77,3 +77,18 @@ def get_available_language_codes() -> List[str]:
language_list = get_language_list() language_list = get_language_list()
codes = [language['code'] for language in language_list] codes = [language['code'] for language in language_list]
return codes return codes
def get_language_translation_data(language: str) -> Dict[str, str]:
if language == 'zh-hans':
language = 'zh_Hans'
elif language == 'zh-hant':
language = 'zh_Hant'
elif language == 'id-id':
language = 'id_ID'
path = os.path.join(settings.STATIC_ROOT, 'locale', language, 'translations.json')
try:
with open(path, 'r') as reader:
return ujson.load(reader)
except FileNotFoundError:
print('Translation for {} not found at {}'.format(language, path))
return {}

View File

@ -170,6 +170,7 @@ class HomeTest(ZulipTestCase):
"test_suite", "test_suite",
"timezone", "timezone",
"translate_emoticons", "translate_emoticons",
"translation_data",
"twenty_four_hour_time", "twenty_four_hour_time",
"two_fa_enabled", "two_fa_enabled",
"two_fa_enabled_user", "two_fa_enabled_user",
@ -752,3 +753,15 @@ class HomeTest(ZulipTestCase):
idle_user_msg_list = get_user_messages(long_term_idle_user) idle_user_msg_list = get_user_messages(long_term_idle_user)
self.assertEqual(idle_user_msg_list[-1].content, message) self.assertEqual(idle_user_msg_list[-1].content, message)
self.logout() self.logout()
def test_translation_data(self) -> None:
user = self.example_user("hamlet")
user.default_language = 'es'
user.save()
self.login(user.email)
result = self._get_home_page()
self.assertEqual(result.status_code, 200)
# TODO: Ideally, this should validate something about the translation
# data, but that requires parsing page_param from the HTML or doing
# more complex interception, which we haven't found worth doing.

View File

@ -21,7 +21,7 @@ from zerver.lib.actions import update_user_presence, do_change_tos_version, \
do_update_pointer, realm_user_count do_update_pointer, realm_user_count
from zerver.lib.avatar import avatar_url from zerver.lib.avatar import avatar_url
from zerver.lib.i18n import get_language_list, get_language_name, \ from zerver.lib.i18n import get_language_list, get_language_name, \
get_language_list_for_templates get_language_list_for_templates, get_language_translation_data
from zerver.lib.json_encoder_for_html import JSONEncoderForHTML from zerver.lib.json_encoder_for_html import JSONEncoderForHTML
from zerver.lib.push_notifications import num_push_devices_for_user from zerver.lib.push_notifications import num_push_devices_for_user
from zerver.lib.streams import access_stream_by_name from zerver.lib.streams import access_stream_by_name
@ -244,6 +244,10 @@ def home_real(request: HttpRequest) -> HttpResponse:
request._log_data['extra'] = "[%s]" % (register_ret["queue_id"],) request._log_data['extra'] = "[%s]" % (register_ret["queue_id"],)
page_params['translation_data'] = {}
if not default_language == 'en':
page_params['translation_data'] = get_language_translation_data(translation.get_language())
csp_nonce = generate_random_token(48) csp_nonce = generate_random_token(48)
response = render(request, 'zerver/app/index.html', response = render(request, 'zerver/app/index.html',
context={'user_profile': user_profile, context={'user_profile': user_profile,