PluginProbe
Translation with DeepL API / 1.2.5.1
Translation with DeepL API v1.2.5.1
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.2.5.1, at client/deeplapi-functions.php

54 lines 2.2 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 = false, $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 if( !$DeepLApiTranslate->setLangTo( $target_lang ) ) {
12 return new WP_Error( '002', sprintf( __( "Target language '%s' not valid", 'wpdeepl' ), $target_lang ) );
13 }
14
15 $DeepLApiTranslate->setTagHandling( 'xml' );
16
17 $translations = $DeepLApiTranslate->getTranslations( $strings );
18 $request = array(
19 'cached' => $DeepLApiTranslate->wasItCached(),
20 'time' => $DeepLApiTranslate->getTimeElapsed(),
21 'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ),
22 'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ),
23 );
24
25 $return = compact( 'request', 'translations' );
26 return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix );
27 }
28
29
30 function deepl_show_usage() {
31 ?>
32 <h3><?php _e( 'Usage', 'wpdeepl' ); ?></h3>
33 <?php
34 $DeepLApiUsage = new DeepLApiUsage();
35 $usage = $DeepLApiUsage->request();
36
37 //plouf($usage); plouf($DeepLApiUsage);
38
39 if( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) :
40 $ratio = round( 100 * ( $usage['character_count'] / $usage['character_limit'] ), 3 );
41 $left_chars = $usage['character_limit'] - $usage['character_count'];
42
43 ?>
44 <div class="progress-bar blue">
45 <span style="width: <?php echo round( (100 - $ratio ), 0 ); ?>%"><b><?php printf( __( '%s characters remaining', 'wpdeepl' ), number_format( $left_chars )); ?></b></span>
46 <div class="progress-text"><?php
47 printf( __( '%s / %s characters translated', 'wpdeepl' ), number_format_i18n( $usage['character_count'] ), number_format_i18n( $usage['character_limit'] ) );
48 echo " - " . $ratio; ?> %</div>
49 <small class="request_time"><?php printf( __( 'Request done in: %f milliseconds', 'wpdeepl' ), $DeepLApiUsage->getRequestTime( true )) ?></small>
50 </div>
51 <?php
52 endif;
53 }
54