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 ?"

KyleMit
  • 30,350
  • 66
  • 462
  • 664
pmrotule
  • 9,065
  • 4
  • 50
  • 58

5 Answers5

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).

Shervin
  • 1,936
  • 17
  • 27
  • 3
    I couldn't find said flexibility, but the `` trick works. Would be nice if you could get the translated text back without the marker though. – theblang Dec 15 '15 at 22:24
  • 2
    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? – Adam May 29 '17 at 10:06
  • The Example did not work for me because I was not using pure HTML text. Just put the text between paragraph:

    my text to translate[bold]

    and will work. In 2019
    – 4lberto Apr 08 '19 at 16:23
  • 3
    https://translate.google.com/#view=home&op=translate&sl=en&tl=hi&text=%3Cp%3Emy%20text%20to%20translate%3Cspan%20class%3D%22notranslate%22%3E%5Bhello%20this%20is%20a%20test%5D%3C%2Fspan%3E%20%3C%2Fp%3E Check this, for some reasons it dosent works for me either. –  Sep 08 '20 at 08:47
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

Makarand Mane
  • 503
  • 8
  • 16
  • https://translate.google.com/#view=home&op=translate&sl=en&tl=hi&text=%3Cp%3Emy%20text%20to%20translate%3Cspan%20class%3D%22notranslate%22%3E%5Bhello%20this%20is%20a%20test%5D%3C%2Fspan%3E%20%3C%2Fp%3E Check this, for some reasons it dosent works for me either. –  Sep 08 '20 at 08:47
  • @TheRahulJha `class="notranslate"` trick is not for the GUI ( https://translate.google.com/ ) , however it will work on the google translation api v2 – user889030 Jan 02 '23 at 05:03
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.

Duy Lan Le
  • 228
  • 4
  • 11
0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
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
赵楚涵
  • 13
  • 4