Last active
November 4, 2015 09:44
-
-
Save Darklg/8680b4f6befd5f440d22 to your computer and use it in GitHub Desktop.
Text Expander - Translate FR to EN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$fromLang = 'fr'; | |
$toLang = 'en'; | |
$text = <<<'CLIP' | |
%clipboard | |
CLIP; | |
function wd_remove_accents($str, $charset = 'utf-8') { | |
$str = htmlentities($str, ENT_NOQUOTES, $charset); | |
$str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str); | |
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); | |
$str = preg_replace('#&[^;]+;#', '', $str); | |
return $str; | |
} | |
$text = wd_remove_accents($text); | |
$url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s'; | |
$curlUrl = sprintf($url, $fromLang, $toLang, urlencode(trim($text))); | |
$handle = curl_init($curlUrl); | |
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($handle); | |
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | |
curl_close($handle); | |
if ($responseCode != 200) { | |
return; | |
} | |
$responseFixed = str_replace(array( | |
',,,', | |
',,' | |
) , array( | |
',"","",', | |
',"",' | |
) , $response); | |
$responseJson = json_decode($responseFixed); | |
if (isset($responseJson[0], $responseJson[0][0], $responseJson[0][0][0])) { | |
echo ($responseJson[0][0][0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment