concatenate all returned chunks from the JSON response

This commit is contained in:
Bernd 2025-01-16 17:51:40 +05:00
parent ec197d8d5e
commit af4e34c7f1

View File

@ -53,15 +53,22 @@ local function translate_subtitle()
-- Parse the JSON returned by Google Translate -- Parse the JSON returned by Google Translate
local data = utils.parse_json(res.stdout) local data = utils.parse_json(res.stdout)
if not data or not data[1] or not data[1][1] or not data[1][1][1] then if not data or not data[1] then
mp.osd_message("Failed to parse translation.") mp.osd_message("Failed to parse translation.")
return return
end end
-- Extract the translated text -- Concatenate translation chunks
local translation = data[1][1][1] -- Google may split the translation into multiple segments for punctuation, etc.
local translation_parts = {}
for _, segment in ipairs(data[1]) do
if segment and segment[1] then
table.insert(translation_parts, segment[1])
end
end
local translation = table.concat(translation_parts, "")
mp.osd_message("Translation:\n" .. translation, o.duration) mp.osd_message(translation, o.duration)
end end
-- Bind the function to a hotkey, e.g. Ctrl+T -- Bind the function to a hotkey, e.g. Ctrl+T