fix: [Language] fix language source

This commit is contained in:
terrtia 2024-01-16 12:04:39 +01:00
parent 1c46bb4296
commit f586baa0c5
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
5 changed files with 17 additions and 41 deletions

View file

@ -388,6 +388,8 @@ class LanguageTranslator:
return language return language
def translate(self, content, source=None, target="en"): # TODO source target def translate(self, content, source=None, target="en"): # TODO source target
if target not in LIST_LANGUAGES:
return None
translation = None translation = None
if content: if content:
if not source: if not source:
@ -407,15 +409,17 @@ class LanguageTranslator:
return translation return translation
LIST_LANGUAGES = [] LIST_LANGUAGES = {}
def get_translation_languages(): def get_translation_languages():
global LIST_LANGUAGES global LIST_LANGUAGES
if not LIST_LANGUAGES: if not LIST_LANGUAGES:
try: try:
LIST_LANGUAGES = LanguageTranslator().languages() LIST_LANGUAGES = {}
for lang in LanguageTranslator().languages():
LIST_LANGUAGES[lang['iso']] = lang['language']
except Exception as e: except Exception as e:
print(e) print(e)
LIST_LANGUAGES = [] LIST_LANGUAGES = {}
return LIST_LANGUAGES return LIST_LANGUAGES

View file

@ -23,6 +23,7 @@ from lib.objects import ChatThreads
from lib.objects import Messages from lib.objects import Messages
from lib.objects import UsersAccount from lib.objects import UsersAccount
from lib.objects import Usernames from lib.objects import Usernames
from lib import Language
config_loader = ConfigLoader() config_loader = ConfigLoader()
r_db = config_loader.get_db_conn("Kvrocks_DB") r_db = config_loader.get_db_conn("Kvrocks_DB")
@ -341,6 +342,8 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa
if meta['subchannels']: if meta['subchannels']:
meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels']) meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels'])
else: else:
if translation_target not in Language.LIST_LANGUAGES:
translation_target = None
meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page) meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page)
return meta, 200 return meta, 200

View file

@ -629,43 +629,6 @@ def get_item_metadata(item_id, item_content=None):
def get_item_content(item_id): def get_item_content(item_id):
return item_basic.get_item_content(item_id) return item_basic.get_item_content(item_id)
def get_item_content_html2text(item_id, item_content=None, ignore_links=False):
if not item_content:
item_content = get_item_content(item_id)
h = html2text.HTML2Text()
h.ignore_links = ignore_links
h.ignore_images = ignore_links
return h.handle(item_content)
def remove_all_urls_from_content(item_id, item_content=None):
if not item_content:
item_content = get_item_content(item_id)
regex = r'\b(?:http://|https://)?(?:[a-zA-Z\d-]{,63}(?:\.[a-zA-Z\d-]{,63})+)(?:\:[0-9]+)*(?:/(?:$|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*\b'
url_regex = re.compile(regex)
urls = url_regex.findall(item_content)
urls = sorted(urls, key=len, reverse=True)
for url in urls:
item_content = item_content.replace(url, '')
regex_pgp_public_blocs = r'-----BEGIN PGP PUBLIC KEY BLOCK-----[\s\S]+?-----END PGP PUBLIC KEY BLOCK-----'
regex_pgp_signature = r'-----BEGIN PGP SIGNATURE-----[\s\S]+?-----END PGP SIGNATURE-----'
regex_pgp_message = r'-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----'
re.compile(regex_pgp_public_blocs)
re.compile(regex_pgp_signature)
re.compile(regex_pgp_message)
res = re.findall(regex_pgp_public_blocs, item_content)
for it in res:
item_content = item_content.replace(it, '')
res = re.findall(regex_pgp_signature, item_content)
for it in res:
item_content = item_content.replace(it, '')
res = re.findall(regex_pgp_message, item_content)
for it in res:
item_content = item_content.replace(it, '')
return item_content
# API # API
# def get_item(request_dict): # def get_item(request_dict):

View file

@ -82,6 +82,8 @@ def chats_explorer_chat():
chat_id = request.args.get('id') chat_id = request.args.get('id')
instance_uuid = request.args.get('uuid') instance_uuid = request.args.get('uuid')
target = request.args.get('target') target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb') nb_messages = request.args.get('nb')
page = request.args.get('page') page = request.args.get('page')
chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
@ -111,6 +113,8 @@ def objects_subchannel_messages():
subchannel_id = request.args.get('id') subchannel_id = request.args.get('id')
instance_uuid = request.args.get('uuid') instance_uuid = request.args.get('uuid')
target = request.args.get('target') target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb') nb_messages = request.args.get('nb')
page = request.args.get('page') page = request.args.get('page')
subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
@ -128,6 +132,8 @@ def objects_thread_messages():
thread_id = request.args.get('id') thread_id = request.args.get('id')
instance_uuid = request.args.get('uuid') instance_uuid = request.args.get('uuid')
target = request.args.get('target') target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb') nb_messages = request.args.get('nb')
page = request.args.get('page') page = request.args.get('page')
thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)

View file

@ -12,7 +12,7 @@
<option selected value="{{ translation_target }}">{{ translation_target }}</option> <option selected value="{{ translation_target }}">{{ translation_target }}</option>
{% endif %} {% endif %}
{% for language in translation_languages %} {% for language in translation_languages %}
<option value="{{ language['iso'] }}">{{ language['language'] }}</option> <option value="{{ language }}">{{ translation_languages[language] }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>