diff --git a/SubTranslate.lua b/SubTranslate.lua index bb1d0e4..e8ca899 100644 --- a/SubTranslate.lua +++ b/SubTranslate.lua @@ -53,15 +53,22 @@ local function translate_subtitle() -- Parse the JSON returned by Google Translate 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.") return end - -- Extract the translated text - local translation = data[1][1][1] + -- Concatenate translation chunks + -- 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 -- Bind the function to a hotkey, e.g. Ctrl+T