google translate API and mathjax latex translation issue - google-translate

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.

There are two issues with the HTML that prevent rendering
extra space in delimiter: \ ( instead of \(
pre and code elements: by default, MathJax ignores these elements, cf. skipTags in http://docs.mathjax.org/en/v2.7-latest/options/tex2jax.html
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>

Related

Loop in XQuery repeats results several times

I have a function which returns paragraphs from the text. So I'm comparing the <anchor> tag's attribute number(#n) to the <notes> tag's attribute number, if it's the same I want to print it with the tooltip if not I just want to print out the paragraph.
declare function letter:text_trans($node as node(), $model as map(*))
{
let $resource := collection('/db/apps/Tobi-oshki/data')
for $ab in $resource//tei:div[#type="translation"]/tei:ab
for $note in $resource//tei:div[#type="notes"]/tei:note
return
if (data($note/#n) eq data($ab/tei:anchor/#n))
then
<div class="tooltip">{$ab}
<span class="tooltiptext"> {data($note/#n)}.{$note}</span>
</div>
else
<p> {$ab} </p>
};
In <notes> I have three notes and when it loops over the notes, each paragraph is returned three times.
How can I change it so it returns the paragraphs only one time?
I am using xquery version "3.1";
Inside the for loop on the $ab, let a variable for the $note and select notes that have #n attribute values that match the $ab, then if there is a matching $note, use it, else return the <p> using just the $ab:
let $resource := collection('/db/apps/Tobi-oshki/data')
for $ab in $resource//tei:div[#type="translation"]/tei:ab
let $note := $resource//tei:div[#type="notes"]/tei:note[#n = $ab/tei:anchor/#n]
return
if ($note)
then
<div class="tooltip">{$ab}
<span class="tooltiptext"> {data($note/#n)}.{$note}</span>
</div>
else
<p> {$ab} </p>
With this input:
<tei:doc>
<tei:div type="notes">
<tei:note n="1">note1</tei:note>
<tei:note n="2">note2</tei:note>
</tei:div>
<tei:div type="translation">
<tei:ab><tei:anchor n="1">translated note1</tei:anchor></tei:ab>
<tei:ab><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
</tei:div>
</tei:doc>
the code above produces this output:
<div class="tooltip">
<tei:ab xmlns:tei="tei">
<tei:anchor n="1">translated note1</tei:anchor>
</tei:ab>
<span class="tooltiptext">1.<tei:note n="1" xmlns:tei="tei">note1</tei:note>
</span>
</div>
<p>
<tei:ab xmlns:tei="tei"><tei:anchor n="3">translated note3</tei:anchor></tei:ab>
</p>

DomCrawler filterXpath for emails

In my project I am trying to use filterXPath for emails. So I get an E-Mail via IMAP and put the mail body into my DomCrawler.
$crawler = new Crawler();
$crawler->addHtmlContent($mail->textHtml); //mail html content utf8
Now to my issue. I only want the plain text of the mail body, but still remain all new lines spaces etc - the exact same as the mail looks just in plain text without html (still with \n\r etc).
For that reason I tried using $crawler->filterXPath('//body/descendant-or-self::*/text()') to get every text node inside the mail.
However my test-mail containts html like:
<p>
<u>
<span>
<a href="mailto:mail#example.com">
<span style="color:#0563C1">mail#example.com</span>
</a>
</span>
</u>
<span>
</span>
<span>·</span>
<span>
<b>
<a href="http://www.example.com">
<span style="color:#0563C1">www.example.com</span>
</a>
</b>
<p/>
</span>
</p>
In my mail this looks like mail#example.com · www.example.com (in one single line).
With my filterXPath I get multiple nodes which result in following (multiple lines):
mail#example.com
· wwww.example.com
I know that probably the 
 might be the problem, which is a \r, but since I can't change the html in the mail, I need another solution - as mentioned before in the mail it is only a single line.
Please keep in mind, that my solution has to work for every mail - I do not know how the mail html looks like - it can change every time. So I need a generic solution.
I already tried using strip_tags too - this does not change the result at all.
My current approach:
$crawler = new Crawler();
$crawler->addHtmlContent($mail->textHtml);
$text = "";
foreach ($crawler->filterXPath('//body/descendant-or-self::*/text()') as $element) {
$part = trim($element->textContent);
if($part) {
$text .= "|".$part."|\n"; //to see whitespaces etc
}
}
echo $text;
//OUTPUT
|mail#example.com|
|·|
| |
|www.example.com|
| |
I believe something like this should work:
$xpath = new DOMXpath($crawler);
$result = $xpath->query('(//span[not(descendant::*)])');
$text = "";
foreach ($result as $element) {
$part = trim($element->textContent);
if($part) {
$text .= "|".$part."|"; //to see whitespaces etc
}
}
echo $text;
Output:
|mail#example.com||·||www.example.com|
Do note that you are dealing with two different ways to treat whitespace only text nodes: HTML has its own rules about if those are rendered (the difference are mainly between block elements and inline elements and also includes normalization) and XPATH works over a document tree provided by a parser (or DOM API) which has its own configuration about preserving or not those whitespace only text nodes. Taking this into account, one solution could be to use the string() function to get the string value of the element containing the email:
For this input:
<root>
<p>
<u>
<span>
<a href="mailto:mail#example.com">
<span style="color:#0563C1">mail#example.com</span>
</a>
</span>
</u>
<span>
</span>
<span>·</span>
<span>
<b>
<a href="http://www.example.com">
<span style="color:#0563C1">www.example.com</span>
</a>
</b>
<p/>
</span>
</p>
</root>
This XPath expresion:
string(/root)
Outputs:
mail#example.com
·
www.example.com
Check in here

Translating a theme

I am tryign to make my theme translatable so I am using the following command to output text. But its not outputting a default value i thought the __ did that automartically.
<?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?>
I am using the above to display a translatable string to wordpress buts its empty.
<div class="skill-title">
<h3><?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?></h3>
</div>
<div class="col-md-5 col-sm-12">
<div class="kids-dashboard-skill">
<div class="skill-show">
<div class="points"><h3><span><?php echo $player->display( 'points' ); ?></span>POINTS</h3></div>
<div class="circle-skill"><div id="circle" data-size="<?php echo $player->display( 'points' ); ?>" data-thickness="35"></div></div>
</div>
<div class="skill-button">
<center>
<button><?php __('VIEW MY TEAM MATES.','gogreensoccer');?></button>
<button><?php __('Player ID','gogreensoccer');?><?php echo $playerId;?></button>
</center>
</div>
</div>
But im not getting any text outputed obv I want a default value here if no translation exists i though __(string,themename) would achieve this.
You don't use the right function: __() returns the text or the translation if it exists. If you want to display it, you must use echo like this: echo __('some text', 'textdomain').
As an alternative, if you just want to print the text, you can use the _e() function which will echo the text (the one in parentheses or the translation if it exists). You can use it like this for example: _('some text', 'textdomain').
Use esc_html_e() function to escape your translatable text and output it.
_e() is a shorthand notation for echo __(), while __() just returns the translated string (not echoing it out).
Anyways, the best practice is to escape all things.

Experiencing tags on screen

I am calling title, body and few other things from database and using echo to display it on screen. Everything is working fine except body, where p tags are printed at start and end of body. My code is
echo "<div class='db'>
<h2 style='background-color:#ffffff;color:#000000;'>".$row['title']."</h2>
<p style='background-color:#ffffff'>" .
(strlen($row['body'])>500?
substr( $row['body'],0,500)."...<a href='blog.php?blogId=" . $row['blogs_id'] . "' style='font-size:13px;color:black;background-color:#ffffff;text-decoration:none;'>Read more</a>":
$row['body']).
"</p>
<img src='$profilephoto_info' height='60' style='background-color:#ffffff;border-radius:20px;'>
By:<a href='$posted_by' style='background-color:#ffffff;text-decoration:none;color:#626769;font-size:13px;'>$posted_by</a>
<p style='background-color:#ffffff;text-decoration:none;color:#626769;font-size:13px;''>".$row['bio']."</p>
</div>";
No h2 tags appear for title nor for any other elements only body is showing p tags around it on screen.
Format your code to know where things get missed up
consider heredoc
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
also loss the inline styles!
<?php
$title = $row['title'];
$shortBody = strlen($row['body']) >500 ? substr( $row['body'],0,500) : $row['body'];
$url = "blog.php?blogId=" . $row[' blogs_id '];
$bio = $row['bio '];
echo <<<dbElement
<div class='db'>
<h2 style='background-color:#FFF;color:#000;'>
$title
</h2>
<p style='background-color:#FFF'>
$shortBody ...
<a href="$url"
style="font-size:13px;color:black;background-color:#FFF;text-decoration:none;">
Read more
</a>
</p>
<img src='$profilephoto_info' height='60'
style='background-color:#FFF;border-radius:20px;'>
By:
<a href='$posted_by'
style='background-color:#FFF;text-decoration:none;color:#626769;font-size:13px;'>
$posted_by
</a>
<p
style='background-color:#FFF;text-decoration:none;color:#626769;font-size:13px;'>
$bio
</p>
</div>
dbElement;
?>
*make sure to not include my php opening and closing tags (<?php and ?>)

Convert text to paragraph in Liquid Template

I'm trying to convert a raw string to html with the Liquid templating language. What I want to do is convert this:
Hello. This
is a text.
Please convert me.
into this:
<p>Hello. This<br />
is a text.</p>
<p>Please convert me.</p>
What I tried
{{ mytextvariable | newline_to_br | replace: "<br />\n<br />", "</p><p>" | replace: "<p></p>", "" | prepend: '<p>' | append: '</p>' }}
but this gives me (notice double <br />):
<p>Hello. This<br />
is a text.<br />
<br />
Please convert me.</p>
Site note
This will be used for a Shopify site.
You're replacing new lines with break tags when using newline_to_br, so the first replacement won't replace anything, newlines are gone.
From what I can see, Liquid does not support regular expressions, so the replace: "<br />\n<br />" will probably be interpreted literally.
Might be easier to implement as a new filter?
... something like this:
def newline_to_p_or_br(input)
input.to_s.gsub(/(.*?)\n\n/, "<p>\1</p>").gsub(/\n/, "<br />\n")
end
Here is the definition of newline_to_br for reference:
http://rubydoc.info/gems/liquid/2.2.2/Liquid/StandardFilters:newline_to_br

Resources