|
1 |
| -import re |
2 |
| - |
3 |
| -from bs4 import BeautifulSoup, element |
| 1 | +from bs4 import element |
4 | 2 | from django.apps import apps
|
5 | 3 | from django.conf import settings
|
6 | 4 | from django.core.exceptions import PermissionDenied
|
|
23 | 21 | from hypha.apply.funds.models.assigned_reviewers import AssignedReviewers
|
24 | 22 | from hypha.apply.funds.workflow import INITIAL_STATE
|
25 | 23 | from hypha.apply.review.options import DISAGREE, MAYBE
|
26 |
| -from hypha.apply.translate import translate |
27 | 24 |
|
28 | 25 |
|
29 | 26 | def bulk_archive_submissions(
|
@@ -266,63 +263,6 @@ def annotate_review_recommendation_and_count(submissions: QuerySet) -> QuerySet:
|
266 | 263 | return submissions
|
267 | 264 |
|
268 | 265 |
|
269 |
| -def translate_application_form_data(application, from_code: str, to_code: str) -> dict: |
270 |
| - """Translate the content of an application's live revision `form_data`. |
271 |
| - Will parse fields that contain both plaintext & HTML, extracting & replacing strings. |
272 |
| -
|
273 |
| - NOTE: Mixed formatting like `<p>Hey from <strong>Hypha</strong></p>` will result in a |
274 |
| - string that is stripped of text formatting (untranslated: `<p>Hey from Hypha</p>`). On |
275 |
| - the other hand, unmixed strings like `<p><strong>Hey from Hypha</strong></p>` will be |
276 |
| - replaced within formatting tags. |
277 |
| -
|
278 |
| - Args: |
279 |
| - application: the application to translate |
280 |
| - from_code: the ISO 639 code of the original language |
281 |
| - to_code: the ISO 639 code of the language to translate to |
282 |
| -
|
283 |
| - Returns: |
284 |
| - The `form_data` with values translated (including nested HTML strings) |
285 |
| -
|
286 |
| - Raises: |
287 |
| - ValueError if an invalid `from_code` or `to_code` is requested |
288 |
| - """ |
289 |
| - form_data: dict = application.live_revision.form_data |
290 |
| - |
291 |
| - translated_form_data = form_data.copy() |
292 |
| - |
293 |
| - # Only translate content fields or the title - don't with name, email, etc. |
294 |
| - translated_form_data["title"] = translate.translate( |
295 |
| - form_data["title"], from_code, to_code |
296 |
| - ) |
297 |
| - |
298 |
| - # RegEx to match wagtail's generated field UIDs - ie. "97c51cea-ab47-4a64-a64a-15d893788ef2" |
299 |
| - uid_regex = re.compile(r"([a-z]|\d){8}(-([a-z]|\d){4}){3}-([a-z]|\d){12}") |
300 |
| - fields_to_translate = [ |
301 |
| - key |
302 |
| - for key in form_data |
303 |
| - if uid_regex.match(key) and isinstance(form_data[key], str) |
304 |
| - ] |
305 |
| - |
306 |
| - for key in fields_to_translate: |
307 |
| - field_html = BeautifulSoup(form_data[key], "html.parser") |
308 |
| - if field_html.find(): # Check if BS detected any HTML |
309 |
| - for field in field_html.find_all(has_valid_str): |
310 |
| - # Removes formatting if mixed into the tag to prioritize context in translation |
311 |
| - # ie. `<p>Hey <strong>y'all</strong></p>` -> `<p>Hey y'all</p>` (but translated) |
312 |
| - to_translate = field.string if field.string else field.text |
313 |
| - field.clear() |
314 |
| - field.string = translate.translate(to_translate, from_code, to_code) |
315 |
| - |
316 |
| - translated_form_data[key] = str(field_html) |
317 |
| - # Ensure the field value isn't empty & translate as is |
318 |
| - elif form_data[key].strip(): |
319 |
| - translated_form_data[key] = translate.translate( |
320 |
| - form_data[key], from_code, to_code |
321 |
| - ) |
322 |
| - |
323 |
| - return translated_form_data |
324 |
| - |
325 |
| - |
326 | 266 | def has_valid_str(tag: element.Tag) -> bool:
|
327 | 267 | """Checks that an Tag contains a valid text element and/or string.
|
328 | 268 |
|
|
0 commit comments