9

How do I Google Translate such that all desired texts are left untranslated?

For example, when translating “Have you tried the Clipboard History window” to Hindi, the “Clipboard History Window” is translated to Hindi instead of being left as it is, as it is a technical term.

I have tried <span class=notranslate> but as it should be, it is valid only in HTML.

1
  • How about removing the respective text from the translation? Or replacing it with something generic? – Alex Dec 2 '13 at 12:18
1

I don't think there's any special tag or character to insert to keep the text untranslated in Google's Translate form.

But since you are willing to manually add tags to keep your text untranslated, a trick is to prefix each word that you want to keep untranslated with a random string, for example "KEEP".

So Have you tried the KEEPClipboard KEEPHistory KEEPwindow will be translated to आप KEEPClipboard KEEPHistory KEEPwindow की कोशिश की and then with a text editor replace all "KEEP" with an empty string.

Definitely not the best work around, but there you go :)

2
  • That will do. I needed this in the program and so managing prefixed words and replacing them back wont be trouble. – Avi Dec 3 '13 at 9:59
  • With Hindi that workaround seems to work, but with other languages Google Translate is being smart and translates each hump separately: OLOLOLOHistory -> OLOLOLOHИстория (russian) – Klesun Mar 2 at 11:41
3

I found the answer, for anyone else who might be looking. You just add the class "notranslate" to your element and Google Translate will leave it untranslated.

Ref: https://cloud.google.com/translate/troubleshooting

2
  • 3
    The page you reference doesn't seem to have that information. Do you have an updated source? – ale Oct 1 '15 at 0:09
  • 1
    Just tried it on my website and adding class="notranslate" is working. Thanks! – Chris Sep 19 '18 at 6:30
0

The "notranslate" trick does not seem to work anymore. What I came up with was such workaround:

  1. In the source text replace all occurrences of the phrase you want to preserve with some big unique number (using any text editor)
  2. Translate
  3. In the result of the translation replace all occurrences of this unique number back to the phrase

Example:

# source text
"Have you tried the Clipboard History window"
# 1. replace "Clipboard History" occurrences with a 0x18482facba482 ->
"Have you tried the 0x18482facba482 window"
# 2. translate -> 
"क्या आपने 0x18482facba482 विंडो की कोशिश की है"
# 3. replace number occurrences back to the phrase ->
"क्या आपने Clipboard History विंडो की कोशिश की है"
0

I replace each unique "tag" that I want to preserve in the text untranslated with a big unique number. After receiving the translation, google keeps it unaltered, so I can look for it and replace it back.

With the catches:

  • for some languages, the number gets commas inserted in them. So on your way back, look for both "20,397,620,437,620,342,340,200" and "20397620437620342340200".
  • If the tag is usually included inside brackets or so, you don't need empty spaces around the number inserted. If it might not, then safer to add spaces around it, to make sure the translator doesn't consider the word before/after as part of the non-lingual alphanumeric and omit translating that word.
  • You will have to assign a big number for each tag you want to preserve. So your dictionary can't be infinite. And it adds cycles to your parser.
0

Failed Attempt

I tried to URL encode my text and slap that as part of a url, for example:

#Source line
When Bluetooth is on, you should see a <i class="fa fa-bluetooth-b" aria-hidden="true"></i> icon in the top bar of your phone. 
#Text to keep untranslated
<i class="fa fa-bluetooth-b" aria-hidden="true"></i>
#Text URL escaped in line with a site attached
When Bluetooth is on, you should see a https://www.donttranslate.com/%3Ci%20class%3D%22fa%20fa-bluetooth-b%22%20aria-hidden%3D%22true%22%3E%3C%2Fi%3E icon in the top bar of your phone.

And use the REGEX of:

\bhttps://www.donttranslate.com/([a-zA-Z0-9%.\-_]*)\b

If the non-replace text includes a % sign, this seems to often fail, and since encoding is about turning symbols into %xx, it's a crap shoot. It can work with some two word combined, such as MyLogo, but I noticed for the above example, aiming for IceCream caused German to do:

When Bluetooth is on, you should see a IceCream icon in the top bar of your phone. 
Wenn Bluetooth eingeschaltet ist, sollten Sie ein https://www.donttranslate.com/IceCream-Symbol in der oberen Leiste Ihres Telefons.

I was really hopeful that URLs would be recognized as a "don't mess with so that the link still works" but this isn't the case.

Solution Used

A different solution I tried was using [§1] similar to how Rafik Mikhael and Klesun did, and having a lookup table for what each number translated to. This ran into the issue that languages would do one of three things to it. Leave it as [§1], move it to [§ 1], or move it to [1.§]. This has been my most consistent solution.

0

to expand on the "replace html with unique placeholders" solution:

i found that google, in rare cases, also translates numbers!
for example google translated [15] to [fünfzehn] in english to german.

my current solution is to encode my index numbers with special characters.
the 12 safe symbols that are never changed by google are []^'*-/_{}<> (can change in future).
(todo: test more combinations of symbols and languages. can be automated.)
note: google can replace -- with -, for example, so i also avoid repetitions.
$ is also preserved, but the order can be changed, e.g. from @$ to $@

const codeNumKey = "^'*-/_{}<>"; // 10 digits = 0123456789
const codeNumBase = codeNumKey.length;

const encodeNumTable = Object.fromEntries(codeNumKey.split('').map((c, i) => [i, c]));
function encodeNum(num) {
  return num.toString(codeNumBase).split('').map(i => encodeNumTable[i]).join('');
}

const decodeNumTable = Object.fromEntries(codeNumKey.split('').map((c, i) => [c, i]));
function decodeNum(str) {
  return parseInt(str.replace(/[\s+\u200B]/sg, '').split('').map(c => decodeNumTable[c]).join(''), codeNumBase);
}

// google can translate -- to -
// so we use "safe" ids without repetition
function getNextSafeId(lastId) {
  for (let id = (lastId + 1); ; id++) {
    let idStr = id.toString();
    let idSafe = true;
    for (let charIdx = 0; charIdx < (idStr.length - 1); charIdx++) {
      if (idStr[charIdx] == idStr[charIdx + 1]) {
        // found repetition
        idSafe = false;
        //if (showDebug) console.log(`skip unsafe id ${id}`);
        break;
      }
    }
    if (idSafe) return id;
  }
}

google will insert spaces (\x20) and zero-width spaces (\u200B) between the characters, so we must parse them too. i remove \u200B completely, cos i dont have it in my input text

i also encode indents, because google will change indent, for example add one space before every line

sample input:

  <div>
    <b>hello</b>
  </div>

encoded:

[}^_]

[}^{]
hello
[}^}]

[}^<]

translated:

[ } ^ _ ]

[ } ^ { ]
こんにちは
[ } ^ } ]

[ } ^ < ]

decoded numbers:

decodeNum(String.raw`[ } ^ _ ]`) == 705
decodeNum(String.raw`[ } ^ { ]`) == 706
decodeNum(String.raw`[ } ^ } ]`) == 707
decodeNum(String.raw`[ } ^ < ]`) == 708

output:

  <div>
    <b>こんにちは</b>
  </div>

my translate script has 700 lines, but also includes code to extract and insert text into my html source files. maybe someone wants to refactor this into a library? : )

im using the official webinterface of google translate, so i can fix translations with the help of google's dictionary. this can be an advantage, compared to other tools like doctranslate

my use case: translate my book of 30 pages html, with bold text, custom xml tags, custom html entities, pre tags, tables, ... working stable so far

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.