| 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( '002', sprintf( __( "Target language '%s' not valid", 'wpdeepl' ), $target_lang ) ); |
| 13 |
} |
| 14 |
|
| 15 |
$DeepLApiTranslate->setTagHandling( 'xml' ); |
| 16 |
|
| 17 |
$translations = $DeepLApiTranslate->getTranslations( $strings ); |
| 18 |
$request = array( |
| 19 |
'cached' => $DeepLApiTranslate->wasItCached(), |
| 20 |
'time' => $DeepLApiTranslate->getTimeElapsed(), |
| 21 |
'cache_file_request' => $DeepLApiTranslate->getCacheFile( 'request' ), |
| 22 |
'cache_file_response' => $DeepLApiTranslate->getCacheFile( 'response' ), |
| 23 |
); |
| 24 |
|
| 25 |
$return = compact( 'request', 'translations' ); |
| 26 |
return apply_filters( 'deepl_translate', $return, $source_lang, $target_lang, $strings, $cache_prefix ); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
function deepl_show_usage() { |
| 31 |
?> |
| 32 |
<h3><?php _e( 'Usage', 'wpdeepl' ); ?></h3> |
| 33 |
<?php |
| 34 |
$DeepLApiUsage = new DeepLApiUsage(); |
| 35 |
$usage = $DeepLApiUsage->request(); |
| 36 |
|
| 37 |
//plouf($usage); plouf($DeepLApiUsage); |
| 38 |
|
| 39 |
if( $usage && is_array( $usage ) && array_key_exists( 'character_count', $usage ) && array_key_exists( 'character_limit', $usage )) : |
| 40 |
$ratio = round( 100 * ( $usage['character_count'] / $usage['character_limit'] ), 3 ); |
| 41 |
$left_chars = $usage['character_limit'] - $usage['character_count']; |
| 42 |
|
| 43 |
?> |
| 44 |
<div class="progress-bar blue"> |
| 45 |
<span style="width: <?php echo round( (100 - $ratio ), 0 ); ?>%"><b><?php printf( __( '%s characters remaining', 'wpdeepl' ), number_format( $left_chars )); ?></b></span> |
| 46 |
<div class="progress-text"><?php |
| 47 |
printf( __( '%s / %s characters translated', 'wpdeepl' ), number_format_i18n( $usage['character_count'] ), number_format_i18n( $usage['character_limit'] ) ); |
| 48 |
echo " - " . $ratio; ?> %</div> |
| 49 |
<small class="request_time"><?php printf( __( 'Request done in: %f milliseconds', 'wpdeepl' ), $DeepLApiUsage->getRequestTime( true )) ?></small> |
| 50 |
</div> |
| 51 |
<?php |
| 52 |
endif; |
| 53 |
} |
| 54 |
|