Does Google Translate API support placeholders?
Asked Answered
D

2

8

Let's say I have a tweet I'll like to translate through the google api.

Text: #sf #fiesta #verano #baile #vamos Fiesta aquí 😎 @my_friend

I'm sending the following to the API endpoint https://www.googleapis.com/language/translate/v2:

{1 /} {2 /} {3 /} {4 /} {5 /} Fiesta aquí 😎 {0 /}

This is the response:

{1} {2} {3}} {4} {5}} party over here {0}

This is not what I expected. However, I would like to put in placeholders otherwise the actual hashtags get translated and that doesn't point to the same results as the original tweet.

There's no mention of placeholders on the API docs, so I guess I'm not too surprised that it failed. Maybe they aren't supported. The only thing I've found is this and I'm not sure that it's related.

Is there a documented way to get this working ?

Driedup answered 10/7, 2017 at 18:40 Comment(0)
H
8

I just tried the Google Translate API with the text containing the hashtags and the Translate API seems to be already taking care of the hashtags as well as @ mentions. It does not translate any of the hashtags or the @ mentions.

Example

$ echo '{q: ["#sf #fiesta #verano #baile #vamos Fiesta aquí 😎 @my_friend"], source: "es", target: "en" }' | http --print=bB POST 'https://translation.googleapis.com/language/translate/v2?key=MY_API_KEY'

{
    q: ["#sf #fiesta #verano #baile #vamos Fiesta aquí 😎 @my_friend"],
    source: "es",
    target: "en"
}


{
    "data": {
        "translations": [
            {
                "translatedText": "#sf #fiesta #verano #baile #vamos Party here 😎 @my_friend"
            }
        ]
    }
}

Skip translation for a portion of the text

If you want a piece of text to not be translated you can enclose it within <span class="notranslate">CONTENT_NOT_TO_BE_TRANSLATED</span>. You should then be able to replace all such span blocks in the result with just the content within using some simple regex pattern replace operation.

$ echo '{q: ["#sf #fiesta #verano #baile #vamos <span class=\"notranslate\">Fiesta</span> aquí 😎   @my_friend"], source: "es", target: "en" }' | http --print=bB POST 'https://translation.googleapis.com/language/translate/v2?key=MY_API_KEY'

{
    q: ["#sf #fiesta #verano #baile #vamos <span class=\"notranslate\">Fiesta</span> aquí 😎   @my_friend"],
    source: "es",
    target: "en"
}


{
    "data": {
        "translations": [
            {
                "#sf #fiesta #verano #baile #vamos <span class=\"notranslate\">Fiesta</span> here 😎 @my_friend"
            }
        ]
    }
}
Heigho answered 11/7, 2017 at 2:12 Comment(0)
M
0

Depending on the language pair, the results will be inconsistent. Google has not been built to handle placeholders and if it works, it won't be by design but by luck.

For example the following simple sentences in English:

I read 0 books.
I read 1 book.
I read 2 books.

Should be translated to this in Vietnamese:

Tôi đọc 0 cuốn sách.
Tôi đã đọc 1 cuốn sách.
Tôi đọc 2 cuốn sách.

But when using markup to try to identify placeholders, results are unexpected:

Tôi đọc <span class = "notranslate"> 0 </span> sách.
Tôi đã đọc <span class = "notranslate"> 1 </span> cuốn sách.
Tôi đã đọc cuốn sách <span class = "notranslate"> 2 </span>.

If you try another strategy, for example, this sentence with a placeholder identified by $count:

I read $count book.

Shoud translate to (in French):

J'ai lu $count livre.

But will be translated to:

J'ai lu le livre $count.

Which Google interprets $count as the name of the book and not a placeholder.

You can try other syntaxes but in some cases, even the placeholder itself is translated...

⚠️ Use Google Translate and placeholders at your own risk but expect surprises 😅

Mesitylene answered 13/7, 2022 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.