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