PluginProbe
Translation with DeepL API / 2.4.3.12
Translation with DeepL API v2.4.3.12
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 2.4.3.12, at client/deeplapi-functions.php

75 lines 3.0 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 = false, $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('okzerzererrzer');
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( 'html' );
17
18 $DeepLApiTranslate->setFormality( DeepLConfiguration::getFormalityLevel( $target_lang) );
19
20 $DeepLApiTranslate = apply_filters('deepl_translate_DeepLApiTranslate_before', $DeepLApiTranslate );
21
22 //echo "\n pour lang $target_lang , formality = " . DeepLConfiguration::getFormalityLevel( $target_lang) ;
23 // plouf($DeepLApiTranslate); die('oazeaz+e48az4ea698k');
24
25 $translations = $DeepLApiTranslate->getTranslations( $strings );
26
27 if ( is_wp_error( $translations ) ) {
28 return array(
29 'success' => false,
30 'errors' => $translations
31 );
32 }
33 $request = array(
34 'cached' => $DeepLApiTranslate->wasItCached(),
35 'time' => $DeepLApiTranslate->getTimeElapsed(),
36 'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ),
37 'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ),
38 );
39 $success = true;
40 $return = compact( 'success', 'request', 'translations' );
41 return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix );
42 }
43
44
45 function deepl_show_usage() {
46 ?>
47 <h3><?php _e( 'Usage', 'wpdeepl' ); ?></h3>
48 <?php
49 $DeepLApiUsage = new DeepLApiUsage();
50 $usage = $DeepLApiUsage->request();
51
52 //plouf($usage); plouf($DeepLApiUsage);
53
54 if ( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) :
55 $ratio = round( 100 * ( $usage['character_count'] / $usage['character_limit'] ), 3 );
56 $left_chars = $usage['character_limit'] - $usage['character_count'];
57
58 ?>
59 <div class="progress-bar blue">
60 <span style="width: <?php echo round( (100 - $ratio ), 0 ); ?>%"><b><?php printf( __( '%s characters remaining', 'wpdeepl' ), number_format( $left_chars )); ?></b></span>
61 <div class="progress-text"><?php
62 printf( __( '%s / %s characters translated', 'wpdeepl' ), number_format_i18n( $usage['character_count'] ), number_format_i18n( $usage['character_limit'] ) );
63 echo " - " . ( $ratio ); ?> %</div>
64 <small class="request_time"><?php printf( __( 'Request done in: %f milliseconds', 'wpdeepl' ), $DeepLApiUsage->getRequestTime( true )) ?></small>
65 </div>
66 <?php
67 else :
68 _e('No response from the server', 'wpdeepl');
69 echo '<br />';
70 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' ),
71 'https://www.deepl.com/pro-account/plan'
72 );
73
74 endif;
75 }