| @@ -1,28 +1,75 @@ | ||
| 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( $target_lang, __( "Target language '$target_lang' not valid", 'wpdeepl' )); | |
| 13 | - | |
| 14 | - $DeepLApiTranslate->setTagHandling( 'xml' ); | |
| 15 | - | |
| 16 | - $translations = $DeepLApiTranslate->getTranslations( $strings ); | |
| 17 | - $request = array( | |
| 18 | - 'cached' => $DeepLApiTranslate->wasItCached(), | |
| 19 | - 'time' => $DeepLApiTranslate->getTimeElapsed(), | |
| 20 | - 'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ), | |
| 21 | - 'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ), | |
| 22 | - ); | |
| 23 | - | |
| 24 | - $return = compact( 'request', 'translations' ); | |
| 25 | - return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix ); | |
| 26 | -} | |
| 27 | - | |
| 28 | - | |
| 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 | +} | |