Search code examples
google-translatemathjaxgoogle-translation-api

google translate API and mathjax latex translation issue


hello here is my code you may understand the whole issue from this

    <?php

require_once ('vendor/autoload.php');
use \Statickidz\GoogleTranslate;

$source = 'en';
$target = 'hi';
$text = "<p>This is a test equation <span translate='no' class='math-tex notranslate'> test me \(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>to check something<span class='math-tex notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p><p>test</p><p>test</p><p>test</p>";

$trans = new GoogleTranslate();
$result = $trans->translate($source, $target, $text);

echo $result;
?>
<html>
<p>
  <br/>
=============== THIS IS THE ORIGINAL TEXT =================
<br/><br/>
  This is a test equation <span class='math-tex notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>to check something<span class='math-tex notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p><p>test</p><p>test</p><p>test</p>


</html>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=default'></script>

when i translate the text from english hindi all works good but my math equation gets issue. it dosent works like it should. i have added the live version here http://ynapsweb.site/pro/test/ please do let me know where i am doing wrong.

i am also looking to solve this if i get the answer i will post here.


Solution

  • There are two issues with the HTML that prevent rendering

    There's also extra space inside the span tag declarations (< span> etc) which you probably want to fix -- but those do not influence MathJax.

    With those fixed, everything renders fine.

    <p> यह एक परीक्षण समीकरण है <span Translate = 'no' class = 'notranslate'> परीक्षण me \ (x = {-b \ pm \ sqrt {b ^ 2-4ac} \ over 2a} \) </ span > कुछ जाँचने के लिए <span class = 'notranslate'> \ _ (x = {-b \ pm \ sqrt {b ^ 2-4ac} \ over 2a} \) </ span> </ p> <p> परीक्षण </ p> <p> परीक्षण </ p> <p> परीक्षण </ p><html>
    <p>
      <br/>
    =============== THIS IS THE ORIGINAL TEXT =================
    <br/><br/>
      This is a test equation <span class='notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>to check something<span class='notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p><p>test</p><p>test</p><p>test</p>
    
    
      <br/>
    =============== THIS IS THE ORIGINAL TEXT in codeing =================
    <br/><br/>
    
    This is a test equation <span class='notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>to check something <span class='notranslate'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p><p>test</p><p>test</p><p>test</p>
    
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=default'></script>