Skip to content

Commit 892cf33

Browse files
Merge pull request #2305 from IFRCGo/feature/fix-log-dump-and-tranlsation-error
Fix translation limit errors at 50000 chars
2 parents 96f724f + 56f5b6e commit 892cf33

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ RUN perl -pi -e 's/ is not -1 / != 1 /' ${AZUREROOT}blob/baseblobservice.py
3939
RUN perl -pi -e "s/ is '' / == '' /" ${AZUREROOT}common/_connection.py
4040
RUN perl -pi -e "s/ is '' / == '' /" ${AZUREROOT}_connection.py
4141

42+
# To avoid dump of "Queue is full. Dropping telemetry." messages in log, 20241111:
43+
ENV OPENCENSUSINIT=/usr/local/lib/python3.11/site-packages/opencensus/common/schedule/__init__.py
44+
RUN perl -pi -e "s/logger.warning.*/pass/" ${OPENCENSUSINIT} 2>/dev/null
45+
4246
COPY main/nginx.conf /etc/nginx/sites-available/
4347
RUN \
4448
ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled; \

lang/translation.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

102138
def get_translator_class():

main/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,5 @@ def decode_base64(env_key, fallback_env_key):
702702

703703
# Need to load this to overwrite modeltranslation module
704704
import main.translation # noqa: F401 E402
705+
706+
AZURE_TRANSL_LIMIT = 49990

0 commit comments

Comments
 (0)