PluginProbe
Translation with DeepL API / 1.7.5
Translation with DeepL API v1.7.5
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-functions.php

deeplapi-functions.php in Translation with DeepL API 1.7.5, at client/deeplapi-functions.php

71 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function deepl_translate( $source_lang, $target_lang, $strings = array(), $cache_prefix = '' ) {
4 $DeepLApiTranslate = new DeepLApiTranslate();
5 $DeepLApiTranslate->setCachePrefix( $cache_prefix );
6
7 if( $source_lang ) {
8 $DeepLApiTranslate->setLangFrom( $source_lang );
9 }
10
11 //var_dump($target_lang); die('ok');
12 if( !$DeepLApiTranslate->setLangTo( $target_lang ) ) {
13 return new WP_Error( sprintf( __( "Target language '%s' not valid", 'wpdeepl' ), $target_lang ) );
14 }
15
16 $DeepLApiTranslate->setTagHandling( 'xml' );
17
18 $DeepLApiTranslate->setFormality( DeepLConfiguration::getFormalityLevel() );
19
20 //plouf($DeepLApiTranslate); die('ok');
21
22 $translations = $DeepLApiTranslate->getTranslations( $strings );
23
24 if( is_wp_error( $translations ) ) {
25 return $translations;
26 }
27 $request = array(
28 'cached' => $DeepLApiTranslate->wasItCached(),
29 'time' => $DeepLApiTranslate->getTimeElapsed(),
30 'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ),
31 'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ),
32 );
33
34
35 $return = compact( 'request', 'translations' );
36 return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix );
37 }
38
39
40 function deepl_show_usage() {
41 ?>
42 <h3><?php _e( 'Usage', 'wpdeepl' ); ?></h3>
43 <?php
44 $DeepLApiUsage = new DeepLApiUsage();
45 $usage = $DeepLApiUsage->request();
46
47 //plouf($usage); plouf($DeepLApiUsage);
48
49 if( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) :
50 $ratio = round( 100 * ( $usage['character_count'] / $usage['character_limit'] ), 3 );
51 $left_chars = $usage['character_limit'] - $usage['character_count'];
52
53 ?>
54 <div class="progress-bar blue">
55 <span style="width: <?php echo round( (100 - $ratio ), 0 ); ?>%"><b><?php printf( __( '%s characters remaining', 'wpdeepl' ), number_format( $left_chars )); ?></b></span>
56 <div class="progress-text"><?php
57 printf( __( '%s / %s characters translated', 'wpdeepl' ), number_format_i18n( $usage['character_count'] ), number_format_i18n( $usage['character_limit'] ) );
58 echo " - " . $ratio; ?> %</div>
59 <small class="request_time"><?php printf( __( 'Request done in: %f milliseconds', 'wpdeepl' ), $DeepLApiUsage->getRequestTime( true )) ?></small>
60 </div>
61 <?php
62 else :
63 _e('No response from the server', 'wpdeepl');
64 echo '<br />';
65 printf(__('Did you select the right server ? If yes, check your plan on <a href="%s">DeepL Pro website</a>. "DeepL API" should be included in it.', 'wpdeepl' ),
66 'https://www.deepl.com/pro-account/plan'
67 );
68
69 endif;
70 }
71