Google translate - Disable translating of a part of my text
Solution 1
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>
Also, Google Translate API will provide you with more flexibility if you don't mind paying a small fee ($20 per 1 M characters).
Solution 2
Just add notranslate class class="notranslate"
where you need and google translator do not touch it..
https://cloud.google.com/translate/v2/faq#technical
Solution 3
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.
Related videos on Youtube
pmrotule
I am a passionate self-taught developer, originally from Quebec city, Canada. I work as a full stack developer (NodeJS, Rails) but actually prefer front end (VueJS, SASS).
Updated on September 21, 2021Comments
-
pmrotule almost 3 years
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 ?"
-
theblang over 8 yearsI 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. -
Adam about 7 yearsIt 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?
-
4lberto about 5 yearsThe 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 2019
-
The Rahul Jha almost 4 yearstranslate.google.com/… Check this, for some reasons it dosent works for me either.
-
The Rahul Jha almost 4 yearstranslate.google.com/… Check this, for some reasons it dosent works for me either.