Merge pull request #705 from ng-ky/main

Add support for "auto" when translating files
This commit is contained in:
Piero Toffanin 2024-10-28 16:13:41 -04:00 committed by GitHub
commit 4de3f7d0be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View file

@ -830,7 +830,7 @@ def create_app(args):
src_lang = next(iter([l for l in languages if l.code == source_lang]), None) src_lang = next(iter([l for l in languages if l.code == source_lang]), None)
if src_lang is None: if src_lang is None and source_lang != "auto":
abort(400, description=_("%(lang)s is not supported", lang=source_lang)) abort(400, description=_("%(lang)s is not supported", lang=source_lang))
tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None)
@ -852,6 +852,14 @@ def create_app(args):
if char_limit > 0: if char_limit > 0:
request.req_cost = max(1, int(os.path.getsize(filepath) / char_limit)) request.req_cost = max(1, int(os.path.getsize(filepath) / char_limit))
if source_lang == "auto":
src_texts = argostranslatefiles.get_texts(filepath)
candidate_langs = detect_languages(src_texts)
detected_src_lang = candidate_langs[0]
src_lang = next(iter([l for l in languages if l.code == detected_src_lang["language"]]), None)
if src_lang is None:
abort(400, description=_("%(lang)s is not supported", lang=detected_src_lang["language"]))
translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath)
translated_filename = os.path.basename(translated_file_path) translated_filename = os.path.basename(translated_file_path)

View file

@ -48,7 +48,7 @@ dependencies = [
"appdirs ==1.4.4", "appdirs ==1.4.4",
"APScheduler ==3.9.1", "APScheduler ==3.9.1",
"translatehtml ==1.5.2", "translatehtml ==1.5.2",
"argos-translate-files ==1.1.4", "argos-translate-files ==1.2.0",
"itsdangerous ==2.1.2", "itsdangerous ==2.1.2",
"Werkzeug ==2.3.8", "Werkzeug ==2.3.8",
"requests ==2.31.0", "requests ==2.31.0",