diff --git a/daemon.py b/daemon.py index 24b6b4a9a..6c3b88933 100644 --- a/daemon.py +++ b/daemon.py @@ -264,6 +264,8 @@ from shares import shares_catalog_account_endpoint from shares import shares_catalog_csv_endpoint from categories import set_hashtag_category from categories import update_hashtag_categories +from languages import load_default_post_languages +from languages import set_default_post_language from languages import get_actor_languages from languages import set_actor_languages from languages import get_understood_languages @@ -3492,6 +3494,10 @@ class PubServer(BaseHTTPRequestHandler): self.server.domain_full, self.server.person_cache) + default_post_language = self.server.system_language + if self.server.default_post_language.get(nickname): + default_post_language = \ + self.server.default_post_language[nickname] msg = \ html_new_post({}, False, self.server.translate, base_dir, @@ -3532,7 +3538,7 @@ class PubServer(BaseHTTPRequestHandler): bold_reading, self.server.dogwhistles, self.server.min_images_for_accounts, - None, None) + None, None, default_post_language) if msg: msg = msg.encode('utf-8') msglen = len(msg) @@ -3648,6 +3654,10 @@ class PubServer(BaseHTTPRequestHandler): self.server.domain_full, self.server.person_cache) + default_post_language = self.server.system_language + if self.server.default_post_language.get(nickname): + default_post_language = \ + self.server.default_post_language[nickname] msg = \ html_new_post({}, False, self.server.translate, base_dir, @@ -3687,7 +3697,7 @@ class PubServer(BaseHTTPRequestHandler): bold_reading, self.server.dogwhistles, self.server.min_images_for_accounts, - None, None) + None, None, default_post_language) if msg: msg = msg.encode('utf-8') msglen = len(msg) @@ -15780,6 +15790,10 @@ class PubServer(BaseHTTPRequestHandler): self.server.domain_full, self.server.person_cache) + default_post_language = self.server.system_language + if self.server.default_post_language.get(nickname): + default_post_language = \ + self.server.default_post_language[nickname] msg = \ html_new_post(edit_post_params, media_instance, translate, @@ -15822,7 +15836,8 @@ class PubServer(BaseHTTPRequestHandler): bold_reading, self.server.dogwhistles, self.server.min_images_for_accounts, - new_post_month, new_post_year) + new_post_month, new_post_year, + default_post_language) if not msg: print('Error replying to ' + in_reply_to_url) self._404() @@ -20065,6 +20080,11 @@ class PubServer(BaseHTTPRequestHandler): fields['location'] = None if not fields.get('languagesDropdown'): fields['languagesDropdown'] = self.server.system_language + set_default_post_language(self.server.base_dir, nickname, + self.server.domain, + fields['languagesDropdown']) + self.server.default_post_language[nickname] = \ + fields['languagesDropdown'] if not citations_button_press: # Store a file which contains the time in seconds @@ -22344,6 +22364,9 @@ def run_daemon(max_hashtags: int, # which accounts should minimize all attached images by default httpd.min_images_for_accounts = load_min_images_for_accounts(base_dir) + # default language for each account when creating a new post + httpd.default_post_language = load_default_post_languages(base_dir) + # caches css files httpd.css_cache = {} diff --git a/languages.py b/languages.py index 4563cf8ab..7ac861046 100644 --- a/languages.py +++ b/languages.py @@ -8,7 +8,10 @@ __status__ = "Production" __module_group__ = "Core" import json +import os from urllib import request, parse +from utils import is_account_dir +from utils import acct_dir from utils import get_actor_languages_list from utils import remove_html from utils import has_object_dict @@ -326,3 +329,43 @@ def auto_translate_post(base_dir: str, post_json_object: {}, translated_text return translated_text return '' + + +def set_default_post_language(base_dir: str, nickname: str, domain: str, + language: str) -> None: + """Sets the default language for new posts + """ + default_post_language_filename = \ + acct_dir(base_dir, nickname, domain) + '/.new_post_language' + try: + with open(default_post_language_filename, 'w+', + encoding='utf-8') as fp_lang: + fp_lang.write(language) + except OSError: + print('EX: Unable to write default post language ' + + default_post_language_filename) + + +def load_default_post_languages(base_dir: str) -> {}: + """Returns a dictionary containing the default languages + for new posts for each account + """ + result = {} + for _, dirs, _ in os.walk(base_dir + '/accounts'): + for handle in dirs: + if not is_account_dir(handle): + continue + nickname = handle.split('@')[0] + domain = handle.split('@')[1] + default_post_language_filename = \ + acct_dir(base_dir, nickname, domain) + '/.new_post_language' + if not os.path.isfile(default_post_language_filename): + continue + try: + with open(default_post_language_filename, 'r', + encoding='utf-8') as fp_lang: + result[nickname] = fp_lang.read() + except OSError: + print('EX: Unable to read default post language ' + + default_post_language_filename) + return result diff --git a/webapp_create_post.py b/webapp_create_post.py index ba963f0d9..33bf69c64 100644 --- a/webapp_create_post.py +++ b/webapp_create_post.py @@ -279,7 +279,8 @@ def html_new_post(edit_post_params: {}, reply_is_chat: bool, bold_reading: bool, dogwhistles: {}, min_images_for_accounts: [], - default_month: int, default_year) -> str: + default_month: int, default_year: int, + default_post_language: str) -> str: """New post screen """ # get the json if this is an edited post @@ -796,9 +797,9 @@ def html_new_post(edit_post_params: {}, languages_dropdown += '
' languages_dropdown = \ languages_dropdown.replace('