diff --git a/libretranslate/app.py b/libretranslate/app.py index eb4fafe..beb5bb0 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -555,18 +555,14 @@ def create_app(args): if source_lang == "auto": candidate_langs = detect_languages(q if batch else [q]) - source_langs = [candidate_langs[0]] + detected_src_lang = candidate_langs[0] else: - if batch: - source_langs = [ {"confidence": 100.0, "language": source_lang} for text in q] - else: - source_langs = [ {"confidence": 100.0, "language": source_lang} ] + detected_src_lang = {"confidence": 100.0, "language": source_lang} - src_langs = [next(iter([l for l in languages if l.code == source_lang["language"]]), None) for source_lang in source_langs] + src_lang = next(iter([l for l in languages if l.code == detected_src_lang["language"]]), None) - for idx, lang in enumerate(src_langs): - if lang is None: - abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx])) + if src_lang is None: + 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) @@ -582,10 +578,10 @@ def create_app(args): try: if batch: results = [] - for idx, text in enumerate(q): - translator = src_langs[idx].get_translation(tgt_lang) + for _idx, text in enumerate(q): + translator = src_lang.get_translation(tgt_lang) if translator is None: - abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_langs[idx].name), scode=src_langs[idx].code)) + abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_lang.name), scode=src_lang.code)) if text_format == "html": translated_text = str(translate_html(translator, text)) @@ -597,7 +593,7 @@ def create_app(args): return jsonify( { "translatedText": results, - "detectedLanguage": source_langs + "detectedLanguage": [detected_src_lang] * len(q) } ) else: @@ -607,9 +603,9 @@ def create_app(args): } ) else: - translator = src_langs[0].get_translation(tgt_lang) + translator = src_lang.get_translation(tgt_lang) if translator is None: - abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_langs[0].name), scode=src_langs[0].code)) + abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_lang.name), scode=src_lang.code)) if text_format == "html": translated_text = str(translate_html(translator, q)) @@ -620,7 +616,7 @@ def create_app(args): return jsonify( { "translatedText": unescape(translated_text), - "detectedLanguage": source_langs[0] + "detectedLanguage": detected_src_lang } ) else: