Search code examples
javascriptgoogle-translatemathjaxckeditor4.xgoogle-translation-api

CK editor 4, google Translate also translate my MATHJAX formulas, even after no translate


Hello all i have a big issue here which is not getting resolved from a long time.

i am using ck editor and Mathjax plugin to add maths formula there like this

<script src="https://cdn.ckeditor.com/4.13.1/standard-all/ckeditor.js"></script>

<textarea name="desc" class="form-control" id="richtext" placeholder="About this category."></textarea>

<script>

var richtext = document.getElementById('richtext');
CKEDITOR.replace('richtext', {
      on : {
            change: function ( evt )  {
            $(richtext).html(evt.editor.getData().replace(/(\r\n|\n|\r)/gm,"") ) ;
            }
        },
      extraPlugins: 'mathjax,colorbutton,font,justify,print,tableresize,uploadimage,uploadfile,pastefromword,liststyle,pagebreak',
      mathJaxLib: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML',
      height: 320
    });

    if (CKEDITOR.env.ie && CKEDITOR.env.version == 8) {
      document.getElementById('ie8-warning').className = 'tip alert';
    }
                </script>

everything works great till here.

now when i publish the data and use google translate to translate the data from english to hindi like this

require_once ('assets/vendor/autoload.php');
use \Statickidz\GoogleTranslate;
$source = 'en';
$target = 'hi';
$text = "<p>This is a test question. please do not translate this <span class="math-tex">\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>equation.</p>";  
// i know there is an issue of double quotes in the class math-tex, but this is what i get from ckeditor. even if i replace the double quotes with single it does not does the job.

$tex4444t=str_replace(array("\r\n", "\r", "\n"), " ", $text);
$trans232 = new GoogleTranslate();
$results3 = $trans232->translate($source, $target, $tex4444t);

i know there is an issue of double quotes in the class math-tex, but this is what i get from ckeditor. even if i replace the double quotes with single it does not does the job. i have also tried using notranslate class with math-tex it still does not work.

i have tried amny other alternatives but none works please help me solve this issue.


Solution

  • This has come up in the MathJax issue tracker. It appears that php google translate doesn't seem to handle the notranslate class. You might file a bug with them.

    In the meantime, 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?

    There is also another option suggested in the MathJax issue tracker that is more complicated, but would probably be the safest approach.