| 1 |
<?php |
| 2 |
|
| 3 |
class DeepLApiTranslate extends DeepLApi { |
| 4 |
protected $endPoint = 'translate'; |
| 5 |
|
| 6 |
protected $langs = array( 'EN', 'DE', 'FR', 'ES', 'IT', 'NL', 'PL', 'RU' ); |
| 7 |
protected $syntaxes = array( 'xml' ); |
| 8 |
|
| 9 |
public $allow_cache = true; |
| 10 |
protected $http_mode = 'POST'; |
| 11 |
|
| 12 |
protected $request = array( |
| 13 |
'source_lang' => null, |
| 14 |
'target_lang' => false, |
| 15 |
'tag_handling' => null, |
| 16 |
'split_sentences' => 1, |
| 17 |
'preserve_formatting' => 1, |
| 18 |
'formality' => 'default', |
| 19 |
'text' => array(), |
| 20 |
|
| 21 |
); |
| 22 |
|
| 23 |
protected function prepareString( $original_string ) { |
| 24 |
$string = $original_string; |
| 25 |
|
| 26 |
$string = preg_replace_callback( '/\\\\u( [0-9a-fA-F]{4} )/', function ( $match ) { |
| 27 |
return mb_convert_encoding( pack( 'H*', $match[1] ), 'UTF-8', 'UCS-2BE' ); |
| 28 |
}, $string ); |
| 29 |
$string = str_replace( ' ', ' ', $string ); |
| 30 |
// mandatory for POST requests |
| 31 |
$string = urlencode($string); |
| 32 |
//$string = htmlspecialchars($string); |
| 33 |
$string = trim( $string ); |
| 34 |
|
| 35 |
//echo "\n ORIG $original_string \n = $string"; |
| 36 |
|
| 37 |
return apply_filters( __METHOD__, $string, $original_string ); |
| 38 |
} |
| 39 |
|
| 40 |
public function getTranslations( $strings = array(), $return_originals = false ) { |
| 41 |
if( !is_array( $strings ) ) { |
| 42 |
return new WP_Error( "wrong type", "Parameter is not an array" ); |
| 43 |
} |
| 44 |
|
| 45 |
$this->finalPrepareRequest(); |
| 46 |
|
| 47 |
$response = array(); |
| 48 |
$translated = array(); |
| 49 |
//plouf( $strings , " zeirpejzirjzi" ); //die( 'okokok' ); |
| 50 |
|
| 51 |
$string_indexes = array(); |
| 52 |
$i = 0; |
| 53 |
|
| 54 |
$cache_id_strings = ''; |
| 55 |
|
| 56 |
foreach( $strings as $string_index => $string ) { |
| 57 |
$string_indexes[$i] = $string_index; |
| 58 |
$i++; |
| 59 |
$string = $this->prepareString( $string ); |
| 60 |
$cache_id_strings .= $string; |
| 61 |
$this->addText( $string ); |
| 62 |
|
| 63 |
if( $return_originals ) { |
| 64 |
$translated['_original_' . $string_index] = urldecode($string); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
//plouf($translated); die('ok aziejzaiej'); |
| 69 |
//plouf( $this ); die("zpijzaijeiaeij"); |
| 70 |
|
| 71 |
$cache_id = ( $this->request['source_lang'] ) ? $this->request['source_lang'] : 'AUTO'; |
| 72 |
$cache_id .= ':' . $this->request['target_lang'] . ':' . md5( $cache_id_strings ); |
| 73 |
$this->setCacheID( $cache_id ); |
| 74 |
|
| 75 |
//plouf($this); die('okaziejaiej'); |
| 76 |
if( !$this->isValidRequest() ) { |
| 77 |
$return = new WP_Error( "bad request", "Bof" ); |
| 78 |
return $return; |
| 79 |
} |
| 80 |
|
| 81 |
//plouf($this);die('eizjrizj'); |
| 82 |
$response = $this->request(); |
| 83 |
|
| 84 |
if( is_wp_error( $response ) ) { |
| 85 |
return $response; |
| 86 |
//$error_string = $response->get_error_message(); echo '<div id="message" class="error"><p>' . $error_string . '</p></div>'; return false; |
| 87 |
} |
| 88 |
|
| 89 |
if( !array_key_exists( 'translations', $response ) ) { |
| 90 |
plouf( $response ); |
| 91 |
die( 'big mistake' ); |
| 92 |
$return = new WP_Error( "bad response", $response ); |
| 93 |
} |
| 94 |
|
| 95 |
foreach( $response['translations'] as $index => $translation ) { |
| 96 |
$string_index = $string_indexes[$index]; |
| 97 |
|
| 98 |
$translated_text = $translation->text; |
| 99 |
//var_dump($translated_text); |
| 100 |
|
| 101 |
// 2021 04 14 |
| 102 |
$translated_text = str_replace( '<!--', '<!--', $translated_text ); |
| 103 |
$translated_text = str_replace( '-->', '-->', $translated_text ); |
| 104 |
$translated_text = str_replace( '-->', '-->', $translated_text ); |
| 105 |
|
| 106 |
//var_dump($translated_text); |
| 107 |
|
| 108 |
$translated[$string_index] = $translated_text; |
| 109 |
} |
| 110 |
// plouf( $translated, " TRANSLATED" ); |
| 111 |
|
| 112 |
return $translated; |
| 113 |
} |
| 114 |
/* |
| 115 |
public function resetQuery() { |
| 116 |
$this->request = array(); |
| 117 |
$this->headers = array(); |
| 118 |
}*/ |
| 119 |
|
| 120 |
private function finalPrepareRequest() { |
| 121 |
|
| 122 |
/* |
| 123 |
Sets whether the translated text should lean towards formal or informal language. This feature currently only works for target languages "DE" (German), "FR" (French), "IT" (Italian), "ES" (Spanish), "NL" (Dutch), "PL" (Polish), "PT-PT", "PT-BR" (Portuguese) and "RU" (Russian). |
| 124 |
*/ |
| 125 |
$target_lang = $this->request['target_lang']; |
| 126 |
if( !in_array( $target_lang, array('DE', 'FR', 'IT', 'ES', 'NL', 'PL', 'PT-PT', 'PT-BR', 'RU' ) ) ) { |
| 127 |
unset( $this->request['formality'] ); |
| 128 |
} |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
public function addText( $string ) { |
| 134 |
$this->request['text'][] = $string; |
| 135 |
return strlen( implode( '', $this->request['text'] ) ); |
| 136 |
} |
| 137 |
|
| 138 |
public function setLangFrom( $source_lang = false ) { |
| 139 |
if( !$source_lang ) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
$source = DeeplConfiguration::validateLang( $source_lang, 'assource' ); |
| 144 |
if( $source ) { |
| 145 |
$this->request['source_lang'] = $source; |
| 146 |
return true; |
| 147 |
} |
| 148 |
else { |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public function setLangTo( $target_lang ) { |
| 154 |
if( !$target_lang ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
$target = DeeplConfiguration::validateLang( $target_lang, 'astarget' ); |
| 159 |
if( !$target ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
else { |
| 163 |
$this->request['target_lang'] = $target; |
| 164 |
return true; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public function setTagHandling( $tag_handling = 'xml' ) { |
| 169 |
if( $tag_handling && in_array( $tag_handling, $this->syntaxes ) ) { |
| 170 |
$this->request['tag_handling'] = $tag_handling; |
| 171 |
return true; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public function setSplitSentences( $split_sentences = true ) { |
| 176 |
if( false === filter_var( $split_sentences, FILTER_VALIDATE_BOOLEAN ) ) { |
| 177 |
$this->request['split_sentences'] = 0; |
| 178 |
} |
| 179 |
else { |
| 180 |
$this->request['split_sentences'] = 1; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
public function setFormality( $formality = 'default' ) { |
| 185 |
if( in_array( $formality, array('more', 'less' ) ) ) { |
| 186 |
$this->request['formality'] = $formality; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public function setPreserveFormatting( $preserve_formatting = false ) { |
| 191 |
if( true === filter_var( $split_sentences, FILTER_VALIDATE_BOOLEAN ) ) { |
| 192 |
$this->request['preserve_formatting'] = 1; |
| 193 |
} |
| 194 |
else { |
| 195 |
$this->request['preserve_formatting'] = 0; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public function getRequestUniqueID() { |
| 200 |
$this->uniqid = md5( implode( '',$this->request['text'] ) ); |
| 201 |
return $this->uniqid; |
| 202 |
} |
| 203 |
|
| 204 |
// RESPONSE |
| 205 |
public function getDetectedLanguage() { |
| 206 |
if( !isset( $this->result ) || !array_key_exists( 'detected_source_language', $this->result ) ) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
return $this->result['detected_source_language']; |
| 210 |
} |
| 211 |
|
| 212 |
public function getMessage() { |
| 213 |
if( !isset( $this->result ) || !array_key_exists( 'message', $this->result ) ) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
return $this->result['message']; |
| 217 |
} |
| 218 |
public function getTranslatedText() { |
| 219 |
if( !isset( $this->result ) || !array_key_exists( 'text', $this->result ) ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
return $this->result['text']; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|