Handle post summary translations

merge-requests/30/head
Bob Mottram 2022-01-28 10:07:35 +00:00
parent 2363ced253
commit 0d32c0a32a
2 changed files with 35 additions and 17 deletions

View File

@ -89,34 +89,45 @@ def get_actor_languages_list(actor_json: {}) -> []:
def get_content_from_post(post_json_object: {}, system_language: str, def get_content_from_post(post_json_object: {}, system_language: str,
languages_understood: []) -> str: languages_understood: [],
contentType: str = "content") -> str:
"""Returns the content from the post in the given language """Returns the content from the post in the given language
including searching for a matching entry within contentMap including searching for a matching entry within contentMap
""" """
this_post_json = post_json_object this_post_json = post_json_object
if has_object_dict(post_json_object): if has_object_dict(post_json_object):
this_post_json = post_json_object['object'] this_post_json = post_json_object['object']
if not this_post_json.get('content'): if not this_post_json.get(contentType):
return '' return ''
content = '' content = ''
if this_post_json.get('contentMap'): mapDict = contentType + 'Map'
if isinstance(this_post_json['contentMap'], dict): if this_post_json.get(mapDict):
if this_post_json['contentMap'].get(system_language): if isinstance(this_post_json[mapDict], dict):
sys_lang = this_post_json['contentMap'][system_language] if this_post_json[mapDict].get(system_language):
sys_lang = this_post_json[mapDict][system_language]
if isinstance(sys_lang, str): if isinstance(sys_lang, str):
return this_post_json['contentMap'][system_language] return this_post_json[mapDict][system_language]
else: else:
# is there a contentMap entry for one of # is there a contentMap/summaryMap entry for one of
# the understood languages? # the understood languages?
for lang in languages_understood: for lang in languages_understood:
if this_post_json['contentMap'].get(lang): if this_post_json[mapDict].get(lang):
return this_post_json['contentMap'][lang] return this_post_json[mapDict][lang]
else: else:
if isinstance(this_post_json['content'], str): if isinstance(this_post_json[contentType], str):
content = this_post_json['content'] content = this_post_json[contentType]
return content return content
def get_summary_from_post(post_json_object: {}, system_language: str,
languages_understood: []) -> str:
"""Returns the summary from the post in the given language
including searching for a matching entry within summaryMap
"""
return get_content_from_post(post_json_object, system_language,
languages_understood, "summary")
def get_base_content_from_post(post_json_object: {}, def get_base_content_from_post(post_json_object: {},
system_language: str) -> str: system_language: str) -> str:
"""Returns the content from the post in the given language """Returns the content from the post in the given language

View File

@ -28,6 +28,7 @@ from utils import remove_html
from utils import get_actor_languages_list from utils import get_actor_languages_list
from utils import get_base_content_from_post from utils import get_base_content_from_post
from utils import get_content_from_post from utils import get_content_from_post
from utils import get_summary_from_post
from utils import has_object_dict from utils import has_object_dict
from utils import update_announce_collection from utils import update_announce_collection
from utils import is_pgp_encrypted from utils import is_pgp_encrypted
@ -1852,6 +1853,9 @@ def individual_post_as_html(signing_priv_key_pem: str,
if translate.get(sensitive_str): if translate.get(sensitive_str):
sensitive_str = translate[sensitive_str] sensitive_str = translate[sensitive_str]
post_json_object['object']['summary'] = sensitive_str post_json_object['object']['summary'] = sensitive_str
post_json_object['object']['summaryMap'] = {
system_language: sensitive_str
}
# add an extra line if there is a content warning, # add an extra line if there is a content warning,
# for better vertical spacing on mobile # for better vertical spacing on mobile
@ -1860,6 +1864,9 @@ def individual_post_as_html(signing_priv_key_pem: str,
if not post_json_object['object'].get('summary'): if not post_json_object['object'].get('summary'):
post_json_object['object']['summary'] = '' post_json_object['object']['summary'] = ''
post_json_object['object']['summaryMap'] = {
system_language: ''
}
if post_json_object['object'].get('cipherText'): if post_json_object['object'].get('cipherText'):
post_json_object['object']['content'] = \ post_json_object['object']['content'] = \
@ -1883,10 +1890,11 @@ def individual_post_as_html(signing_priv_key_pem: str,
if not content_str: if not content_str:
return '' return ''
summary_str = get_summary_from_post(post_json_object, system_language,
languages_understood)
is_patch = is_git_patch(base_dir, nickname, domain, is_patch = is_git_patch(base_dir, nickname, domain,
post_json_object['object']['type'], post_json_object['object']['type'],
post_json_object['object']['summary'], summary_str, content_str)
content_str)
_log_post_timing(enable_timing_log, post_start_time, '16') _log_post_timing(enable_timing_log, post_start_time, '16')
@ -1921,12 +1929,11 @@ def individual_post_as_html(signing_priv_key_pem: str,
else: else:
post_id = 'post' + str(create_password(8)) post_id = 'post' + str(create_password(8))
content_str = '' content_str = ''
if post_json_object['object'].get('summary'): if summary_str:
cw_str = str(post_json_object['object']['summary'])
cw_str = \ cw_str = \
add_emoji_to_display_name(session, base_dir, http_prefix, add_emoji_to_display_name(session, base_dir, http_prefix,
nickname, domain, nickname, domain,
cw_str, False) summary_str, False)
content_str += \ content_str += \
'<label class="cw">' + cw_str + '</label>\n ' '<label class="cw">' + cw_str + '</label>\n '
if is_moderation_post: if is_moderation_post: