Google translate - Disable translating of a part of my text
Asked Answered
P

5

23

I'm working on an admin page to create post for a blog. I have a french textarea and an english textarea. So, for those who cannot translate by there own, I created a button "translate with google":

<a id="tr_textefr" href="http://translate.google.fr/#fr/en/" target="_blank">
  Traduire avec Google
</a>

And my french textarea has a javascript function called onkeyup:

function translate(what){
  var button = "tr_" + what;
  var textarea = document.getElementById(what);
  var google = "http://translate.google.fr/#fr/en/" + textarea.value;

  document.getElementById(button).setAttribute('href', google);
}

For exemple, if I write "Voulez-vous coucher avec moi ce soir ?", it will change the href attribute for "http://translate.google.fr/#fr/en/Voulez-vous coucher avec moi ce soir ?". The link will redirect at the translated version of my text (by google translate).

This code works fine by the way. The thing is that I could have sometimes bbcode inside my text: "Voulez-vous [b]coucher[/b] avec moi ce soir ?".

So, is there a way with google translate to disable translating of some words or sentences ? For exemple, I don't wanna translate the words between two @ : "Voulez-vous @[b]@coucher@[/b]@ avec moi ce soir ?"

Populate answered 13/2, 2013 at 20:16 Comment(0)
Q
29

Edit (Sep. 2020): Seems that with some API changes this solution is not applicable anymore (at least on the web).

Protecting parts of the string from translation is possible by wrapping them in a <span> tag with a specific class value (as explained here):

<span class="notranslate">[bold]</span>

Example

Also, Google Translate API will provide you with more flexibility if you don't mind paying a small fee ($20 per 1 M characters).

Quickwitted answered 11/4, 2013 at 23:13 Comment(4)
I couldn't find said flexibility, but the <span> trick works. Would be nice if you could get the translated text back without the marker though.Kushner
It seems that you can use Google Translate API now only if you pay the small fee. Could you explain what the improved solution is from the API?Crewel
The Example did not work for me because I was not using pure HTML text. Just put the text between paragraph: <p>my text to translate<span class="notranslate">[bold]</span> </p> and will work. In 2019Caneghem
translate.google.com/… Check this, for some reasons it dosent works for me either.Depredation
S
8

Just add notranslate class class="notranslate" where you need and google translator do not touch it.. https://cloud.google.com/translate/v2/faq#technical

Sufism answered 13/2, 2013 at 20:16 Comment(2)
translate.google.com/… Check this, for some reasons it dosent works for me either.Depredation
@TheRahulJha class="notranslate" trick is not for the GUI ( translate.google.com ) , however it will work on the google translation api v2Teece
S
1

I want to add a simple way to not translate text between double quotes is using for loop.

var text = "This is the \"Word\" I dont want to translate.";
var splitText = text.Split(" ").ToList();
bool Found = false;
string temp = string.Empty;
for (int i = 0; i < splitText.Count; i++)
{
    if (splitText[i].Contains("\"") && !Found)
    {
        splitText[i] = "<span class='notranslate'>" + splitText[i];
        Found = true;
        temp = splitText[i];
    }
    if (splitText[i].Contains("\"") && Found && splitText[i] != temp)
    {
                splitText[i] = splitText[i] + "</span>";
                Found = false;
    }
}
text = String.Join(" ", splitText).ToString();

As you can see, I add a span with notranslate class into every double quote.

Shellieshellproof answered 11/9, 2021 at 5:55 Comment(0)
M
0

Besides <span class="notranslate"></span>, use CAPITAL letter. Like EXAMPLE.COM instead of example.com.

Marcomarconi answered 5/7, 2022 at 8:22 Comment(0)
R
0

I also had this problem when I called Google cloud translation api using Golang language, I had some emoji like , like <quad 37> that I didn't want to be translated by Google cloud. So I mapped them to Unicode emoji, and then replaced the emoji with the corresponding in the translated text

    func generateEmoji(num int) string {
    // 将数字转换为对应的 Unicode 码点
    var codepoint int
    if num < 0x10000 {
        // 如果数字在 BMP 范围内,直接使用对应的码点
        codepoint = 0x1F600 + num
    } else {
        // 如果数字在辅助平面范围内,使用 UTF-16 编码中的高代理项和低代理项来表示码点
        highSurrogate := 0xD800 + ((num - 0x10000) >> 10)
        lowSurrogate := 0xDC00 + ((num - 0x10000) & 0x3FF)
        codepoint = (highSurrogate << 16) | lowSurrogate
    }
    // 将码点转换为 emoji
    emoji := string(rune(codepoint))
    return emoji
   }

    // 将 <quad 数字> 映射为随机 emoji,并记录映射关系
    func emojiTTT(str string) (string, map[string]string) {
        rand.Seed(time.Now().UnixNano())
        quadMap := make(map[string]string)
        emojiMap := make(map[string]string)
        re := regexp.MustCompile("<quad ([0-9]+)>")
        for _, match := range re.FindAllStringSubmatch(str, -1) {
            quadNum := match[1]
            if _, ok := quadMap["<quad "+quadNum+">"]; ok { // 已经有映射关系
                continue
            }
            num, err := strconv.Atoi(quadNum)
            if err != nil {
                continue
            }
            emoji := generateEmoji(num)
    
            quadMap["<quad "+quadNum+">"] = emoji
            emojiMap[emoji] = "<quad " + quadNum + ">"
            str = strings.ReplaceAll(str, "<quad "+quadNum+">", emoji)
        }
        return str, emojiMap
    }

Then you just need to use it

filterText, emojiMap := emojiTTT(req.Text) //<quad num> to emoji
resp, err := client.Translate(ctx, []string{filterText}, target, nil
//deal the resp
newStr := resp[0].Text
for k, v := range emojiMap {
    newStr = strings.Replace(newStr, k, v, -1)
}
return newStr
Radiotherapy answered 23/2, 2023 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.