PluginProbe
Translation with DeepL API / 2.4.3.9
Translation with DeepL API v2.4.3.9
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
wpdeepl / _custom-integration.php

_custom-integration.php in Translation with DeepL API 2.4.3.9, at _custom-integration.php

48 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 *
5 * To add specific data / unique custom meta to the translation, add this code to your theme/functions.php
6 * Sample : the metakey is 'hotelwp_meta' and the value is encoded in json, we need to translate 'tagline' and 'alt_title' from the array of data
7 * */
8
9 /**
10 * First add the original strings to the array to be translated
11 * */
12 add_filter( 'deepl_translate_post_link_strings', 'mytheme_deeplpro_translate_post_link_strings', 80, 4 );
13 function mytheme_deeplpro_translate_post_link_strings( $strings_to_translate, $WP_Post, $target_lang, $source_lang ) {
14
15 $custom_data = json_decode( get_post_meta( $WP_Post->ID, 'hotelwp_meta', true ), true );
16 if( !empty( $custom_data['tagline'] ) ) {
17 $strings_to_translate['mytheme_tagline'] = $custom_data['tagline'];
18 }
19 if( !empty( $custom_data['alt_title'] ) ) {
20 $strings_to_translate['mytheme_alt_title'] = $custom_data['alt_title'];
21 }
22 return $strings_to_translate;
23 }
24
25
26 /**
27 * Then get the translated data after translation and post update
28 */
29 add_action( 'deepl_translate_after_post_update', 'mytheme_deeplpro_translate_post_link_after', 80, 5 );
30 function mytheme_deeplpro_translate_post_link_after( $post_array, $strings_to_translate, $response, $WP_Post, $no_translation ) {
31
32 $new_post_custom_data = json_decode( get_post_meta( $WP_Post->ID, 'hotelwp_meta', true), true );
33
34 $changed = false;
35 if( $response['translations'] ) foreach( $response['translations'] as $key => $translation ) {
36 if( substr( $key, 0, 8 ) == 'mytheme_' ) {
37 $real_key = substr( $key, 8 );
38 $new_post_custom_data[$real_key] = $translation;
39 $changed = true;
40
41 }
42 }
43 if( $changed ) {
44 update_post_meta( $WP_Post->ID, 'hotelwp_meta', json_encode( $new_post_custom_data, JSON_UNESCAPED_UNICODE ) );
45 }
46 }
47
48