| @@ -1,29 +1,37 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -function deepl_translate( $source_lang, $target_lang, $strings = array(), $cache_prefix = '' ) { | |
| 2 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 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 ) ) { | |
| 14 | + /* translators: target language asked */ | |
| 13 | 15 | return new WP_Error( sprintf( __( "Target language '%s' not valid", 'wpdeepl' ), $target_lang ) ); |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | - $DeepLApiTranslate->setTagHandling( 'xml' ); | |
| 18 | + $DeepLApiTranslate->setTagHandling( 'html' ); | |
| 17 | 19 | |
| 18 | - $DeepLApiTranslate->setFormality( DeepLConfiguration::getFormalityLevel() ); | |
| 20 | + $DeepLApiTranslate->setFormality( DeepLConfiguration::getFormalityLevel( $target_lang) ); | |
| 19 | 21 | |
| 20 | - //plouf($DeepLApiTranslate); die('ok'); | |
| 22 | + $DeepLApiTranslate = apply_filters('deepl_translate_DeepLApiTranslate_before', $DeepLApiTranslate ); | |
| 21 | 23 | |
| 24 | + //echo "\n pour lang $target_lang , formality = " . DeepLConfiguration::getFormalityLevel( $target_lang) ; | |
| 25 | +// wpdeepl_debug_display($DeepLApiTranslate); die('oazeaz+e48az4ea698k'); | |
| 26 | + | |
| 22 | 27 | $translations = $DeepLApiTranslate->getTranslations( $strings ); |
| 23 | 28 | |
| 24 | - if( is_wp_error( $translations ) ) { | |
| 25 | - return $translations; | |
| 29 | + if ( is_wp_error( $translations ) ) { | |
| 30 | + return array( | |
| 31 | + 'success' => false, | |
| 32 | + 'errors' => $translations | |
| 33 | + ); | |
| 26 | 34 | } |
| 27 | 35 | $request = array( |
| 28 | 36 | 'cached' => $DeepLApiTranslate->wasItCached(), |
| 29 | 37 | 'time' => $DeepLApiTranslate->getTimeElapsed(), |
| @@ -29,42 +37,61 @@ | ||
| 29 | 37 | 'time' => $DeepLApiTranslate->getTimeElapsed(), |
| 30 | 38 | 'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ), |
| 31 | 39 | 'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ), |
| 32 | 40 | ); |
| 33 | - | |
| 34 | - | |
| 35 | - $return = compact( 'request', 'translations' ); | |
| 41 | + $success = true; | |
| 42 | + $return = compact( 'success', 'request', 'translations' ); | |
| 36 | 43 | return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix ); |
| 37 | 44 | } |
| 38 | 45 | |
| 39 | - | |
| 46 | + | |
| 40 | 47 | function deepl_show_usage() { |
| 41 | 48 | ?> |
| 42 | - <h3><?php _e( 'Usage', 'wpdeepl' ); ?></h3> | |
| 49 | + <h3><?php esc_html_e( 'Usage', 'wpdeepl' ); ?></h3> | |
| 43 | 50 | <?php |
| 44 | 51 | $DeepLApiUsage = new DeepLApiUsage(); |
| 45 | 52 | $usage = $DeepLApiUsage->request(); |
| 46 | 53 | |
| 47 | - //plouf($usage); plouf($DeepLApiUsage); | |
| 54 | + //wpdeepl_debug_display($usage); wpdeepl_debug_display($DeepLApiUsage); | |
| 48 | 55 | |
| 49 | - if( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) : | |
| 56 | + if ( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) : | |
| 50 | 57 | $ratio = round( 100 * ( $usage['character_count'] / $usage['character_limit'] ), 3 ); |
| 51 | 58 | $left_chars = $usage['character_limit'] - $usage['character_count']; |
| 52 | 59 | |
| 53 | 60 | ?> |
| 54 | 61 | <div class="progress-bar blue"> |
| 55 | - <span style="width: <?php echo round( (100 - $ratio ), 0 ); ?>%"><b><?php printf( __( '%s characters remaining', 'wpdeepl' ), number_format( $left_chars )); ?></b></span> | |
| 62 | + <span style="width: <?php echo esc_attr( round( (100 - absint( $ratio ) ), 0 ) ); ?>%"><b><?php | |
| 63 | + echo esc_html( | |
| 64 | + sprintf( | |
| 65 | + /* translators: characters remaining */ | |
| 66 | + __( '%s characters remaining', 'wpdeepl' ), | |
| 67 | + // L'argument est déjà sécurisé par esc_html(), on le laisse tel quel. | |
| 68 | + number_format( $left_chars ) | |
| 69 | + ) | |
| 70 | + ); ?></b></span> | |
| 56 | 71 | <div class="progress-text"><?php |
| 57 | - printf( __( '%s / %s characters translated', 'wpdeepl' ), number_format_i18n( $usage['character_count'] ), number_format_i18n( $usage['character_limit'] ) ); | |
| 58 | - echo " - " . $ratio; ?> %</div> | |
| 59 | - <small class="request_time"><?php printf( __( 'Request done in: %f milliseconds', 'wpdeepl' ), $DeepLApiUsage->getRequestTime( true )) ?></small> | |
| 72 | + echo esc_html( | |
| 73 | + sprintf( | |
| 74 | + /* translators: characters translated / limit */ | |
| 75 | + __( '%1$s / %2$s characters translated', 'wpdeepl' ), | |
| 76 | + esc_html( number_format_i18n( $usage['character_count'] ) ), | |
| 77 | + esc_html( number_format_i18n( $usage['character_limit'] ) ) | |
| 78 | + )); | |
| 79 | + echo " - " . esc_html( $ratio ); ?> %</div> | |
| 80 | + <small class="request_time"><?php | |
| 81 | + echo esc_html( sprintf( | |
| 82 | + /* translators: time for the request */ | |
| 83 | + __( 'Request done in: %f milliseconds', 'wpdeepl' ), | |
| 84 | + esc_html( $DeepLApiUsage->getRequestTime( true ) ) | |
| 85 | + )); ?></small> | |
| 60 | 86 | </div> |
| 61 | 87 | <?php |
| 62 | 88 | else : |
| 63 | - _e('No response from the server', 'wpdeepl'); | |
| 64 | - 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' ), | |
| 89 | + esc_html_e('No response from the server', 'wpdeepl'); | |
| 90 | + ?><br /><?php | |
| 91 | + /* translators: link to the DeepL website */ | |
| 92 | + echo wp_kses_post( sprintf(__('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 | 93 | 'https://www.deepl.com/pro-account/plan' |
| 67 | - ); | |
| 94 | + )); | |
| 68 | 95 | |
| 69 | 96 | endif; |
| 70 | -} | |
| 97 | +} | |