diff --git a/zerver/lib/i18n.py b/zerver/lib/i18n.py index 0548cadec1..f51ac95c75 100644 --- a/zerver/lib/i18n.py +++ b/zerver/lib/i18n.py @@ -77,3 +77,18 @@ def get_available_language_codes() -> List[str]: language_list = get_language_list() codes = [language['code'] for language in language_list] 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 {} diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 10146ede27..bea2f38f13 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -170,6 +170,7 @@ class HomeTest(ZulipTestCase): "test_suite", "timezone", "translate_emoticons", + "translation_data", "twenty_four_hour_time", "two_fa_enabled", "two_fa_enabled_user", @@ -752,3 +753,15 @@ class HomeTest(ZulipTestCase): idle_user_msg_list = get_user_messages(long_term_idle_user) self.assertEqual(idle_user_msg_list[-1].content, message) 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. diff --git a/zerver/views/home.py b/zerver/views/home.py index 9436794e12..e0adcd0a09 100644 --- a/zerver/views/home.py +++ b/zerver/views/home.py @@ -21,7 +21,7 @@ from zerver.lib.actions import update_user_presence, do_change_tos_version, \ do_update_pointer, realm_user_count from zerver.lib.avatar import avatar_url 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.push_notifications import num_push_devices_for_user 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"],) + 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) response = render(request, 'zerver/app/index.html', context={'user_profile': user_profile,