@@ -78,10 +78,43 @@ def __init__(self):
7878 def is_text_html (cls , text ):
7979 return bool (BeautifulSoup (text , "html.parser" ).find ())
8080
81+ @classmethod
82+ def find_last_slashtable (cls , text , limit ):
83+ tag = "</table>"
84+ truncate_here = text [:limit ].rfind (tag )
85+ if truncate_here != - 1 :
86+ truncate_here += len (tag )
87+ return truncate_here
88+
89+ @classmethod
90+ def find_last_slashp (cls , text , limit ):
91+ tag = "</p>"
92+ truncate_here = text [:limit ].rfind (tag )
93+ if truncate_here != - 1 :
94+ truncate_here += len (tag )
95+ return truncate_here
96+
8197 def translate_text (self , text , dest_language , source_language = None ):
8298 if settings .TESTING :
8399 # NOTE: Mocking for test purpose
84100 return self ._fake_translation (text , dest_language , source_language )
101+
102+ # A dirty workaround to handle oversized HTML+CSS texts, usually tables:
103+ textTail = ""
104+ if len (text ) > settings .AZURE_TRANSL_LIMIT :
105+ truncate_here = self .find_last_slashtable (text , settings .AZURE_TRANSL_LIMIT )
106+ if truncate_here != - 1 :
107+ textTail = text [truncate_here :]
108+ text = text [:truncate_here ]
109+ else :
110+ truncate_here = self .find_last_slashp (text , settings .AZURE_TRANSL_LIMIT )
111+ if truncate_here != - 1 :
112+ textTail = text [truncate_here :]
113+ text = text [:truncate_here ]
114+ else :
115+ textTail = text [settings .AZURE_TRANSL_LIMIT :]
116+ text = text [: settings .AZURE_TRANSL_LIMIT ]
117+
85118 payload = {
86119 "text" : text ,
87120 "from" : source_language ,
@@ -96,7 +129,10 @@ def translate_text(self, text, dest_language, source_language=None):
96129 headers = self .headers ,
97130 json = payload ,
98131 )
99- return response .json ()[0 ]["translations" ][0 ]["text" ]
132+
133+ # Not using == 200 – it would break tests with MagicMock name=requests.post() results
134+ if response .status_code != 500 :
135+ return response .json ()[0 ]["translations" ][0 ]["text" ] + textTail
100136
101137
102138def get_translator_class ():
0 commit comments