argosopentech / argosopentech/translate-html

Translated comments shown in final document

Open
#9 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
60
Forks
11
PR merge metrics
No merged PRs in 30d

Description

The contents of the comments get translated correctly, but when reconstructing the `BeautifulSoup` object their tags are lost, causing the final translated document to show the contents of the comments when opened with a web browser.

The issue, as far as I can work out, is that neither `itag_of_soup` nor `soup_of_itag` differentiate between a `bs4.element.NavigableString` and a `bs4.element.Comment` (which inherits from the former).

So, `itag_of_soup` returns an `str` object regardless of whether its processing a `NavigableString` or a `Comment`. When `soup_of_itag` is called, it checks if the object passed to it is an instance of `str` and if so constructs a `NavigableString`, which for the case of comments results in losing the `` characters in the final document.

Here's an example:

```python
import argostranslate.translate
import translatehtml

# Original "file"
content = """


Test



Welcome to Test!


"""

# Define languages for translation from English to Hindi
en = argostranslate.translate.get_language_from_code("en")
hi = argostranslate.translate.get_language_from_code("hi")
ut = en.get_translation(hi)

# Translate the file with translate_html
content = translatehtml.translate_html(ut, content)

# Write the translated file
with open("test.html", "wt") as fp:
fp.write(str(content))
```

|![Screenshot from 2023-02-15 12-33-32](https://user-images.githubusercontent.com/41118412/219074817-a1eebcf4-bed4-455d-8c5f-7f5b60b7351c.png)|
|:---:|
|*Original file*|

|![Screenshot from 2023-02-15 12-33-45](https://user-images.githubusercontent.com/41118412/219074827-bb51051a-9ef6-4908-8668-94d729b6a918.png)|
|:---:|
|*Translation*|

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing itag_of_soup and soup_of_itag from translate_html using the HTML example in the issue. Check how BeautifulSoup distinguishes NavigableString from Comment, then verify that translated comments are reconstructed as comments and remain hidden when the resulting document is opened in a browser.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.