MathJax icon indicating copy to clipboard operation
MathJax copied to clipboard

Problems with translator

Open Dacastror opened this issue 7 years ago • 15 comments

Hello everyone, I am trying to read the content of this page: Neural networks and deep learning, but I do not know enough English (I speak Spanish), so I use the translation option that Chrome includes. The problem is that the translator does not ignore the mathematical expressions and these are altered, making them impossible to read.

I downloaded the web page on my computer to see if I could do something about it, I discovered that the problem is solved in an acceptable way if I enclose the mathematical expression with:

<span class="notranslate"> </span>

This prevents the translator from altering the equations.

My question is: How can I add the <span class="notranslate"> </span> tag to the mathjax output? I thank you in advance for your attention.

Note: The option to translate web pages automatically is used by millions of people, It would be great if this problem were solved directly in mathjax.

Dacastror avatar Jan 27 '19 23:01 Dacastror

Well, for pages that you don't control, one way would be to enter

for (const jax of MathJax.Hub.getAllJax()) {jax.SourceElement().previousSibling.classList.add('notranslate')}

in the browser console after MathJax has finished typesetting the page. This will add the notranslate class to all the typeset math. That does seem to prevent Google Translate from affecting the math, though it does sometimes remove spaced before or after the math.

I am marking your post as a feature request for future implementation.

dpvc avatar Jan 28 '19 22:01 dpvc

PS, you could make the command above into a bookmarklet, or make a GreaseMonkey (or TamperMonkey) script to do it automatically on any page that loads MathJax.

dpvc avatar Jan 28 '19 22:01 dpvc

Thank you very much dpvc, wonderful answer, it works very well for me.

dpvc: I am marking your post as a feature request for future implementation.

This is great, this can make it serve many people.

Dacastror avatar Jan 29 '19 19:01 Dacastror

i have a similer problem, when i use google translator the equation just dosent works, even after applying notranslate This is a test text that i want to try Welcome to the <span class='notranslate'> test $$ (x = {-b \pm \sqrt{b^2-4ac} \over 2a}.) $$ </sp an> company website!

iamrahulkumar001 avatar Jan 13 '20 17:01 iamrahulkumar001

@iamrahulkumar001, your example works for me (once the space in </sp an> is removed). Can you link to your test page where it doesn't work for you?

dpvc avatar Jan 13 '20 20:01 dpvc

yes here it is http://ynapsweb.site/pro/test/ i am also trying to figure out the solution in the mean time thankx for helping.

iamrahulkumar001 avatar Jan 14 '20 18:01 iamrahulkumar001

@iamrahulkumar001, try using only class="notranslate" without the math-tex class as well. Maybe Google translate doesn't handle the multiple classes (I don't know, but it is worth a try).

dpvc avatar Jan 14 '20 23:01 dpvc

still the same problem, i appreciate your help though. i tried it with only notranslate class

iamrahulkumar001 avatar Jan 15 '20 05:01 iamrahulkumar001

@iamrahulkumar001, well, it seems, then, that this is an issue with Google Translate more than it is with MathJax. Perhaps this needs to be reported at the php-google-translate site, or to Google itself?

On the other hand, the main issue with the math once it has been "translated" is that there are extra spaces after the backslashes. So perhaps doing a regular expression replacement that converts a backslash followed by a space to just a backslash would be all that is needed?

Alternatively, you might be able to write a routine that takes your string to be translated, removes the math and replaces it with a placeholder (something that won't be translated), calls GoogleTranslate on the modified string, and then puts back the math in the translated string in place of the placeholder. If your string will have multiple math items, you could replace them with something like @@12@@, where 12 is the number of the math in the original string; you would keep an array of the original math strings, and 12 would be the index in that array of the math item that @@12@@ is a placeholder for.

So those are two possible work-arounds for the problem. Perhaps one of those would do the trick.

dpvc avatar Jan 15 '20 14:01 dpvc

@dpvc Thank you so much for helping. i will surely try your methods and will check if i fix it i will post the solution here also. thank you for your help.

iamrahulkumar001 avatar Jan 15 '20 15:01 iamrahulkumar001

$text=str_replace('"', "'",$formula_text)
$results=str_replace("\ ","\\",$text);

iamrahulkumar001 avatar Feb 22 '20 07:02 iamrahulkumar001

Well, for pages that you don't control, one way would be to enter

for (const jax of MathJax.Hub.getAllJax()) {jax.SourceElement().previousSibling.classList.add('notranslate')}

in the browser console after MathJax has finished typesetting the page. This will add the notranslate class to all the typeset math. That does seem to prevent Google Translate from affecting the math, though it does sometimes remove spaced before or after the math.

This solution works fine when doing translations. Is there any reason why we cannot simply add a notranslate class by default similarly as the script from above does? Or is there any sane use case where a translation of a mathematical expression would make sense?

xrayn avatar Apr 07 '20 21:04 xrayn

@xrayn, Well, some expressions include \text{} calls containing text that probably should be translated. It is not clear how this should situation should be handled.

dpvc avatar Apr 07 '20 21:04 dpvc

@dpvc, OK, perhaps this makes sense.

For me, most of the math expressions where messed up and also the parts around it. So even if I had a \text{} in it, the translation of the text would be correct, but the text itself would probably be messed up anyway. :)

xrayn avatar Apr 07 '20 22:04 xrayn

paste this code on your Console

  document.querySelectorAll('.MathJax').forEach(function(element) {
    element.classList.add('notranslate');
  });

then try to translate your web with Google Translate. in my case it works and not translated the Math Symbols and Equations, but the writing becomes a little less neat because sometimes the mathematical symbols are adjacent to the writing

allail-qadrillah avatar Aug 09 '24 15:08 allail-qadrillah