@@ -238,21 +238,27 @@ def translate_text_block(text_block, translator):
238238
239239 :param text_block: String containing multiple lines to translate
240240 :param translator: DeepLClient instance
241- :return: List of translated lines or original lines if limit exceeded
241+ :return: List of translated lines or original lines if limit exceeded or translation fails
242242 """
243243
244244 verbose_output (f"{ BackgroundColors .GREEN } Translating text block...{ Style .RESET_ALL } " ) # Output the verbose message
245245
246246 remaining_chars = get_remaining_characters (translator ) # Check remaining characters
247247
248248 if remaining_chars is not None : # If there is a limit on remaining characters
249- print (f"{ BackgroundColors .GREEN } Current remaining characters in DeepL API: { BackgroundColors .CYAN } { remaining_chars } { BackgroundColors .GREEN } characters{ Style .RESET_ALL } " ) # Output remaining characters
250249 if len (text_block ) > remaining_chars : # Exceeding limit
251250 print (f"{ BackgroundColors .YELLOW } Warning: Translation limit would be exceeded. Current block size: { BackgroundColors .CYAN } { len (text_block )} { BackgroundColors .YELLOW } . Exceeding limit by: { BackgroundColors .CYAN } { len (text_block ) - remaining_chars } { BackgroundColors .YELLOW } characters. Skipping translation for this block.{ Style .RESET_ALL } " ) # Output warning message
252251 return text_block .split ("\n " ) # Return original lines
253- else : # Perform translation
252+
253+ try : # Perform translation
254254 result = translator .translate_text (text_block , source_lang = "EN" , target_lang = "PT-BR" ) # Translate text block
255- return result .text .split ("\n " ) # Return translated lines
255+ if result is not None and hasattr (result , "text" ) and result .text : # Ensure result is valid
256+ return result .text .split ("\n " ) # Return translated lines
257+ else :
258+ return text_block .split ("\n " ) # Fallback to original lines
259+ except Exception as e : # Handle any translation error
260+ print (f"{ BackgroundColors .RED } Translation failed: { e } . Returning original lines.{ Style .RESET_ALL } " )
261+ return text_block .split ("\n " ) # Return original lines on failure
256262
257263def translate_srt_lines (lines ):
258264 """
@@ -274,14 +280,20 @@ def translate_srt_lines(lines):
274280
275281 if stripped == "" or stripped .replace (":" , "" ).replace ("," , "" ).isdigit () or "-->" in line : # If line is empty, timing, or index
276282 if buffer : # If buffer contains text to translate
277- translated_lines .extend (translate_text_block ("\n " .join (buffer ), translator )) # Translate buffer
283+ translated = translate_text_block ("\n " .join (buffer ), translator ) # Translate buffer
284+ if translated is None : # Defensive check, should never happen
285+ translated = buffer
286+ translated_lines .extend (translated ) # Add translated lines
278287 buffer = [] # Clear buffer
279- translated_lines .append (line .rstrip ("\n " )) # Keep timing/index/empty line as is
288+ translated_lines .append (line .rstrip ("\n " )) # Keep timing/index/empty line as is
280289 else : # Line is text to be translated
281290 buffer .append (stripped ) # Add line to buffer
282291
283292 if buffer : # Translate any remaining text in buffer
284- translated_lines .extend (translate_text_block ("\n " .join (buffer ), translator ))
293+ translated = translate_text_block ("\n " .join (buffer ), translator )
294+ if translated is None :
295+ translated = buffer
296+ translated_lines .extend (translated )
285297
286298 return translated_lines # Return all translated lines
287299
0 commit comments