From db9f92a1c3f5f1f9b961dc69c5177c4ccf6364c2 Mon Sep 17 00:00:00 2001 From: Ng Ky <15950171+ng-ky@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:35:11 +0800 Subject: [PATCH 1/3] Add support for "auto" when translating files --- libretranslate/app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index b210a94..cfedea8 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -830,7 +830,7 @@ def create_app(args): 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)) tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) @@ -852,6 +852,11 @@ def create_app(args): if char_limit > 0: 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) + src_lang = candidate_langs[0] + translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_filename = os.path.basename(translated_file_path) From 039f878f7fab1e0f4efa8240458660e496293dc5 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 28 Oct 2024 16:10:27 -0400 Subject: [PATCH 2/3] Bump argos-translate-files, fix src_lang initialization, checks --- libretranslate/app.py | 5 ++++- pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index cfedea8..705058f 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -855,7 +855,10 @@ def create_app(args): if source_lang == "auto": src_texts = argostranslatefiles.get_texts(filepath) candidate_langs = detect_languages(src_texts) - src_lang = candidate_langs[0] + 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=candidate_langs[0])) translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_filename = os.path.basename(translated_file_path) diff --git a/pyproject.toml b/pyproject.toml index 930e952..49709b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,7 @@ dependencies = [ "appdirs ==1.4.4", "APScheduler ==3.9.1", "translatehtml ==1.5.2", - "argos-translate-files ==1.1.4", + "argos-translate-files ==1.2.0", "itsdangerous ==2.1.2", "Werkzeug ==2.3.8", "requests ==2.31.0", From 6f293f6013f8077d4bf40ecde2e4f9081aaac9c7 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 28 Oct 2024 16:12:59 -0400 Subject: [PATCH 3/3] Fix 400 error msg --- libretranslate/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index 705058f..c1fd4ac 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -858,7 +858,7 @@ def create_app(args): 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=candidate_langs[0])) + 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_filename = os.path.basename(translated_file_path)