How to translate only those html tags whose content is not in English?

64 views
Skip to first unread message

Neculai I. Fantanaru

unread,
Jun 28, 2021, 11:00:19 AM6/28/21
to Google Cloud Translation API
hello. I want to use Python for a task.My question is how to translate all html tags whose content is not in English, in several files?

For example, I have this 3 tags, whose content is in English, in Bosnian and Indonesian.

<p class="obisnuit">I walk my children to school. I bicycle to work nearly every day. I own one small car that serves a family of four.</p>

<p class="obisnuit">Pješačim djecu do škole. Skoro svaki dan vozim bicikl na posao. Posjedujem jedan mali automobil koji opslužuje četveročlanu porodicu.</p>

<p class="obisnuit">Saya mengantar anak-anak saya ke sekolah. Saya bersepeda ke tempat kerja hampir setiap hari. Saya memiliki satu mobil kecil yang melayani keluarga dengan empat orang.</p>

So I want to find out all html tags whose content is written only in English, so as to translate them. I first thought of using for searching non-ASCII characters, something like [\x{4E00}-\x{9FFF}\x{FF00}-\x{FFEF}] , but I believe this will work only in chinesse or Hindi. And I do't know how to do that.

Can anyone help me ?

Efim (Cloud Platform Support)

unread,
Jul 2, 2021, 10:47:21 AM7/2/21
to Google Cloud Translation API
According to this page, you have to send the API request to 
https://translation.googleapis.com/language/translate/v2/detect

The same page also has Python code snippet for language detection.
```
def detect_language(text):
    """Detects the text's language."""
    from google.cloud import translate_v2 as translate

    translate_client = translate.Client()

    # Text can also be a sequence of strings, in which case this method
    # will return a sequence of results for each text.
    result = translate_client.detect_language(text)

    print("Text: {}".format(text))
    print("Confidence: {}".format(result["confidence"]))
    print("Language: {}".format(result["language"]))
```
Reply all
Reply to author
Forward
0 new messages