PluginProbe
Translation with DeepL API / 1.0
Translation with DeepL API v1.0
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
wpdeepl / client / deeplapi-translate.class.php

deeplapi-translate.class.php in Translation with DeepL API 1.0, at client/deeplapi-translate.class.php

178 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class DeepLApiTranslate extends DeepLApi {
4 protected $endPoint = 'translate';
5
6 protected $langs = array( 'EN', 'DE', 'FR', 'ES', 'IT', 'NL', 'PL' );
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 'text' => array(),
19
20 );
21
22 protected function prepareString( $original_string ) {
23 $string = $original_string;
24
25 $string = preg_replace_callback( '/\\\\u( [0-9a-fA-F]{4} )/', function ( $match ) {
26 return mb_convert_encoding( pack( 'H*', $match[1] ), 'UTF-8', 'UCS-2BE' );
27 }, $string );
28 $string = str_replace( '&nbsp;', ' ', $string );
29 $string = trim( $string );
30
31 //echo "\n ORIG $original_string \n = $string";
32
33 return apply_filters( __METHOD__, $string, $original_string );
34 }
35
36 public function getTranslations( $strings = array() ) {
37 if( !is_array( $strings ) ) {
38 return new WP_Error( "wrong type", "Parameter is not an array" );
39 }
40
41 $response = array();
42 //plouf( $strings , " zeirpejzirjzi" ); //die( 'okokok' );
43
44 $string_indexes = array();
45 $i = 0;
46
47 $cache_id_strings = '';
48
49 foreach( $strings as $string_index => $string ) {
50 $string_indexes[$i] = $string_index;
51 $i++;
52 $string = $this->prepareString( $string );
53 $cache_id_strings += $string;
54 $this->addText( $string );
55 }
56
57 //plouf( $this ); die();
58
59 $cache_id = ( $this->request['source_lang'] ) ? $this->request['source_lang'] : 'AUTO';
60 $cache_id .= ':' . $this->request['target_lang'] . ':' . md5( $cache_id_strings );
61 $this->setCacheID( $cache_id );
62
63 if( !$this->isValidRequest() ) {
64 $return = new WP_Error( "bad request", "plouf" );
65 }
66
67 $response = $this->request();
68 if( is_wp_error( $response ) ) {
69 $error_string = $response->get_error_message();
70 echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
71 return false;
72 }
73
74 if( !array_key_exists( 'translations', $response ) ) {
75 plouf( $response );
76 die( 'monumental error' );
77 $return = new WP_Error( "bad response", $response );
78 }
79
80 $translated = array();
81 foreach( $response['translations'] as $index => $translation ) {
82 $string_index = $string_indexes[$index];
83 $translated[$string_index] = $translation->text;
84 }
85 // plouf( $translated, " TRANSLATED" );
86
87 return $translated;
88 }
89 /*
90 public function resetQuery() {
91 $this->request = array();
92 $this->headers = array();
93 }*/
94
95 public function addText( $string ) {
96 $this->request['text'][] = $string;
97 return strlen( implode( '', $this->request['text'] ) );
98 }
99
100 public function setLangFrom( $source_lang = false ) {
101 if( !$source_lang ) {
102 return true;
103 }
104 elseif( $source_lang && array_key_exists( $source_lang, $this->languages ) ) {
105 $this->request['source_lang'] = $source_lang;
106 return true;
107 }
108 else {
109 return false;
110 }
111 }
112
113 public function setLangTo( $target_lang ) {
114 if( !$target_lang ) {
115 return false;
116 }
117 elseif( $target_lang && array_key_exists( $target_lang, $this->languages ) ) {
118 $this->request['target_lang'] = $target_lang;
119 return true;
120 }
121 else {
122 return false;
123 }
124 }
125
126 public function setTagHandling( $tag_handling = 'xml' ) {
127 if( $tag_handling && in_array( $tag_handling, $this->syntaxes ) ) {
128 $this->request['tag_handling'] = $tag_handling;
129 return true;
130 }
131 }
132
133 public function setSplitSentences( $split_sentences = true ) {
134 if( false === filter_var( $split_sentences, FILTER_VALIDATE_BOOLEAN ) ) {
135 $this->request['split_sentences'] = 0;
136 }
137 else {
138 $this->request['split_sentences'] = 1;
139 }
140 }
141
142 public function setPreserveFormatting( $preserve_formatting = false ) {
143 if( true === filter_var( $split_sentences, FILTER_VALIDATE_BOOLEAN ) ) {
144 $this->request['preserve_formatting'] = 1;
145 }
146 else {
147 $this->request['preserve_formatting'] = 0;
148 }
149 }
150
151 public function getRequestUniqueID() {
152 $this->uniqid = md5( $this->request['text'] );
153 return $this->uniqid;
154 }
155
156 // RESPONSE
157 public function getDetectedLanguage() {
158 if( !isset( $this->result ) || !array_key_exists( 'detected_source_language', $this->result ) ) {
159 return false;
160 }
161 return $this->result['detected_source_language'];
162 }
163
164 public function getMessage() {
165 if( !isset( $this->result ) || !array_key_exists( 'message', $this->result ) ) {
166 return false;
167 }
168 return $this->result['message'];
169 }
170 public function getTranslatedText() {
171 if( !isset( $this->result ) || !array_key_exists( 'text', $this->result ) ) {
172 return false;
173 }
174 return $this->result['text'];
175 }
176 }
177
178