Google translate - Disable translating of a part of my text

被刻印的时光 ゝ 提交于 2021-02-07 05:07:50

问题


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


回答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>

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




回答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



来源:https://stackoverflow.com/questions/14862291/google-translate-disable-translating-of-a-part-of-my-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!