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

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