From d741e68dc0cb3b8caefb912c71a90beab4432c46 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 16 Dec 2022 21:43:34 +0000 Subject: [PATCH] Message text direction for certain languages --- epicyon-profile.css | 11 +++++++++++ utils.py | 27 +++++++++++++++++++++++++++ webapp_post.py | 13 ++++++++++++- webapp_utils.py | 9 +++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/epicyon-profile.css b/epicyon-profile.css index 8a157fff3..68c3a46ec 100644 --- a/epicyon-profile.css +++ b/epicyon-profile.css @@ -653,6 +653,17 @@ a:focus { hyphens: auto; text-wrap: pretty; text-align: justify; + direction: ltr; +} + +.message_rtl { + margin-left: 0%; + margin-right: 0%; + width: 100%; + hyphens: auto; + text-wrap: pretty; + text-align: justify; + direction: rtl; } .addedHashtag:link { diff --git a/utils.py b/utils.py index 66dc5fa45..a3f4723df 100644 --- a/utils.py +++ b/utils.py @@ -219,6 +219,33 @@ def get_content_from_post(post_json_object: {}, system_language: str, return standardize_text(content) +def get_language_from_post(post_json_object: {}, system_language: str, + languages_understood: [], + content_type: str = "content") -> str: + """Returns the content language from the post + including searching for a matching entry within contentMap + """ + this_post_json = post_json_object + if has_object_dict(post_json_object): + this_post_json = post_json_object['object'] + if not this_post_json.get(content_type): + return system_language + map_dict = content_type + 'Map' + if this_post_json.get(map_dict): + if isinstance(this_post_json[map_dict], dict): + if this_post_json[map_dict].get(system_language): + sys_lang = this_post_json[map_dict][system_language] + if isinstance(sys_lang, str): + return system_language + else: + # is there a contentMap/summaryMap entry for one of + # the understood languages? + for lang in languages_understood: + if this_post_json[map_dict].get(lang): + return lang + return system_language + + def get_media_descriptions_from_post(post_json_object: {}) -> str: """Returns all attached media descriptions as a single text. This is used for filtering diff --git a/webapp_post.py b/webapp_post.py index 720588964..5a45f7618 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -35,6 +35,7 @@ from utils import remove_html from utils import get_actor_languages_list from utils import get_base_content_from_post from utils import get_content_from_post +from utils import get_language_from_post from utils import get_summary_from_post from utils import has_object_dict from utils import update_announce_collection @@ -78,6 +79,7 @@ from content import get_mentions_from_html from content import switch_words from person import is_person_snoozed from person import get_person_avatar_url +from webapp_utils import language_right_to_left from webapp_utils import get_banner_file from webapp_utils import get_avatar_image_url from webapp_utils import update_avatar_image_cache @@ -2163,6 +2165,9 @@ def individual_post_as_html(signing_priv_key_pem: str, languages_understood = get_actor_languages_list(actor_json) content_str = get_content_from_post(post_json_object, system_language, languages_understood) + content_language = \ + get_language_from_post(post_json_object, system_language, + languages_understood) content_str = dont_speak_hashtags(content_str) attachment_str, gallery_str = \ @@ -2248,6 +2253,9 @@ def individual_post_as_html(signing_priv_key_pem: str, if not content_str: content_str = get_content_from_post(post_json_object, system_language, languages_understood) + content_language = \ + get_language_from_post(post_json_object, system_language, + languages_understood) content_str = dont_speak_hashtags(content_str) if not content_str: content_str = \ @@ -2429,7 +2437,10 @@ def individual_post_as_html(signing_priv_key_pem: str, content_str = '' else: if not is_patch: - content_str = '
' + \ + message_class = 'message' + if language_right_to_left(content_language): + message_class = 'message_rtl' + content_str = '
' + \ content_str + \ '
\n' else: diff --git a/webapp_utils.py b/webapp_utils.py index 45afec2f2..c818cff63 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -1944,3 +1944,12 @@ def text_mode_browser(ua_str: str) -> bool: if agent in ua_str: return True return False + + +def language_right_to_left(language: str) -> bool: + """is the given language written from right to left? + """ + rtl_languages = ('ar', 'fa') + if language in rtl_languages: + return True + return False