mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-17 04:50:11 +01:00
Merge pull request #533 from pierotofy/batchfix
Fix batch auto language detection
This commit is contained in:
commit
4761ae5c43
@ -555,18 +555,14 @@ def create_app(args):
|
|||||||
|
|
||||||
if source_lang == "auto":
|
if source_lang == "auto":
|
||||||
candidate_langs = detect_languages(q if batch else [q])
|
candidate_langs = detect_languages(q if batch else [q])
|
||||||
source_langs = [candidate_langs[0]]
|
detected_src_lang = candidate_langs[0]
|
||||||
else:
|
else:
|
||||||
if batch:
|
detected_src_lang = {"confidence": 100.0, "language": source_lang}
|
||||||
source_langs = [ {"confidence": 100.0, "language": source_lang} for text in q]
|
|
||||||
else:
|
|
||||||
source_langs = [ {"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 src_lang is None:
|
||||||
if lang is None:
|
abort(400, description=_("%(lang)s is not supported", lang=source_lang))
|
||||||
abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx]))
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@ -582,10 +578,10 @@ def create_app(args):
|
|||||||
try:
|
try:
|
||||||
if batch:
|
if batch:
|
||||||
results = []
|
results = []
|
||||||
for idx, text in enumerate(q):
|
for text in q:
|
||||||
translator = src_langs[idx].get_translation(tgt_lang)
|
translator = src_lang.get_translation(tgt_lang)
|
||||||
if translator is None:
|
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":
|
if text_format == "html":
|
||||||
translated_text = str(translate_html(translator, text))
|
translated_text = str(translate_html(translator, text))
|
||||||
@ -597,7 +593,7 @@ def create_app(args):
|
|||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedText": results,
|
"translatedText": results,
|
||||||
"detectedLanguage": source_langs
|
"detectedLanguage": [detected_src_lang] * len(q)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -607,9 +603,9 @@ def create_app(args):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
translator = src_langs[0].get_translation(tgt_lang)
|
translator = src_lang.get_translation(tgt_lang)
|
||||||
if translator is None:
|
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":
|
if text_format == "html":
|
||||||
translated_text = str(translate_html(translator, q))
|
translated_text = str(translate_html(translator, q))
|
||||||
@ -620,7 +616,7 @@ def create_app(args):
|
|||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedText": unescape(translated_text),
|
"translatedText": unescape(translated_text),
|
||||||
"detectedLanguage": source_langs[0]
|
"detectedLanguage": detected_src_lang
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -737,12 +733,10 @@ def create_app(args):
|
|||||||
if os.path.splitext(file.filename)[1] not in frontend_argos_supported_files_format:
|
if os.path.splitext(file.filename)[1] not in frontend_argos_supported_files_format:
|
||||||
abort(400, description=_("Invalid request: file format not supported"))
|
abort(400, description=_("Invalid request: file format not supported"))
|
||||||
|
|
||||||
source_langs = [source_lang]
|
src_lang = next(iter([l for l in languages if l.code == source_lang]), None)
|
||||||
src_langs = [next(iter([l for l in languages if l.code == source_lang]), None) for source_lang in source_langs]
|
|
||||||
|
|
||||||
for idx, lang in enumerate(src_langs):
|
if src_lang is None:
|
||||||
if lang is None:
|
abort(400, description=_("%(lang)s is not supported", lang=source_lang))
|
||||||
abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx]))
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@ -755,7 +749,7 @@ def create_app(args):
|
|||||||
|
|
||||||
file.save(filepath)
|
file.save(filepath)
|
||||||
|
|
||||||
translated_file_path = argostranslatefiles.translate_file(src_langs[0].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)
|
||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
|
Loading…
Reference in New Issue
Block a user