| 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 |
|