| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* wpLingua translate : Get translated JSON |
| 11 |
* |
| 12 |
* @param string $json |
| 13 |
* @param array $args |
| 14 |
* @return string |
| 15 |
*/ |
| 16 |
function wplng_translate_json( $json, $args = array() ) { |
| 17 |
|
| 18 |
if ( empty( $json ) ) { |
| 19 |
return $json; |
| 20 |
} |
| 21 |
|
| 22 |
$json_translated = ''; |
| 23 |
$json_decoded = json_decode( $json, true ); |
| 24 |
|
| 25 |
if ( empty( $json_decoded ) || ! is_array( $json_decoded ) ) { |
| 26 |
return $json; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Update args |
| 31 |
*/ |
| 32 |
|
| 33 |
wplng_args_setup( $args ); |
| 34 |
|
| 35 |
if ( empty( $args['translations'] ) && empty( $args['texts_unknow'] ) ) { |
| 36 |
|
| 37 |
$texts = wplng_parse_json_array( |
| 38 |
$json_decoded, |
| 39 |
$args['parents'] |
| 40 |
); |
| 41 |
|
| 42 |
wplng_args_update_from_texts( |
| 43 |
$args, |
| 44 |
$texts |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Translate JSON |
| 50 |
*/ |
| 51 |
|
| 52 |
$json_translated = wp_json_encode( |
| 53 |
wplng_translate_json_array( |
| 54 |
$json_decoded, |
| 55 |
$args |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
if ( empty( $json_translated ) ) { |
| 60 |
return $json; |
| 61 |
} |
| 62 |
|
| 63 |
return $json_translated; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* wpLingua translate : Get translated array from decoded JSON |
| 69 |
* |
| 70 |
* @param array $json_decoded |
| 71 |
* @param array $translations |
| 72 |
* @param array $parents |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
function wplng_translate_json_array( $json_decoded, $args = array() ) { |
| 76 |
|
| 77 |
$array_translated = $json_decoded; |
| 78 |
|
| 79 |
/** |
| 80 |
* Update args |
| 81 |
*/ |
| 82 |
|
| 83 |
wplng_args_setup( $args ); |
| 84 |
|
| 85 |
if ( empty( $args['translations'] ) && empty( $args['texts_unknow'] ) ) { |
| 86 |
|
| 87 |
$texts = wplng_parse_json_array( |
| 88 |
$json_decoded, |
| 89 |
$args['parents'] |
| 90 |
); |
| 91 |
|
| 92 |
wplng_args_update_from_texts( |
| 93 |
$args, |
| 94 |
$texts |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Don't parse JSON if it's exclude |
| 100 |
*/ |
| 101 |
|
| 102 |
if ( wplng_json_element_is_excluded( $json_decoded, $args['parents'] ) ) { |
| 103 |
|
| 104 |
if ( WPLNG_DEBUG_JSON === true ) { |
| 105 |
|
| 106 |
$debug = array( |
| 107 |
'title' => 'wpLingua JSON debug - Excluded parent', |
| 108 |
'parents' => $args['parents'], |
| 109 |
'value' => $json_decoded, |
| 110 |
); |
| 111 |
|
| 112 |
error_log( |
| 113 |
var_export( |
| 114 |
$debug, |
| 115 |
true |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
return $json_decoded; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Parse each JSON elements |
| 125 |
*/ |
| 126 |
|
| 127 |
foreach ( $json_decoded as $key => $value ) { |
| 128 |
|
| 129 |
$current_parents = array_merge( $args['parents'], array( $key ) ); |
| 130 |
|
| 131 |
/** |
| 132 |
* Don't parse element if it's exclude |
| 133 |
*/ |
| 134 |
|
| 135 |
if ( wplng_json_element_is_excluded( $value, $current_parents ) ) { |
| 136 |
|
| 137 |
if ( true === WPLNG_DEBUG_JSON ) { |
| 138 |
|
| 139 |
$debug = array( |
| 140 |
'title' => 'wpLingua JSON debug - Excluded element', |
| 141 |
'parents' => $current_parents, |
| 142 |
'value' => $value, |
| 143 |
); |
| 144 |
|
| 145 |
error_log( |
| 146 |
var_export( |
| 147 |
$debug, |
| 148 |
true |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
if ( is_array( $value ) ) { |
| 157 |
|
| 158 |
/** |
| 159 |
* If element is an array, parse it |
| 160 |
*/ |
| 161 |
|
| 162 |
$args['parents'] = $current_parents; |
| 163 |
|
| 164 |
$array_translated[ $key ] = wplng_translate_json_array( |
| 165 |
$value, |
| 166 |
$args |
| 167 |
); |
| 168 |
|
| 169 |
$args['parents'] = array_splice( $args['parents'], 0, -1 ); |
| 170 |
|
| 171 |
} elseif ( is_string( $value ) ) { |
| 172 |
|
| 173 |
$debug_type = ''; |
| 174 |
|
| 175 |
/** |
| 176 |
* If element is a string |
| 177 |
*/ |
| 178 |
|
| 179 |
if ( wplng_str_is_locale_id( $value ) ) { |
| 180 |
|
| 181 |
/** |
| 182 |
* If is a local ID (fr_FR, fr, FR, ...), replace by current |
| 183 |
*/ |
| 184 |
|
| 185 |
$debug_type = 'String - Local ID'; |
| 186 |
$array_translated[ $key ] = $args['language_target']; |
| 187 |
|
| 188 |
} elseif ( wplng_str_is_json( $value ) ) { |
| 189 |
|
| 190 |
/** |
| 191 |
* If element is a JSON, parse and translate it |
| 192 |
*/ |
| 193 |
|
| 194 |
$debug_type = 'String - JSON'; |
| 195 |
$args['parents'] = $current_parents; |
| 196 |
|
| 197 |
$array_translated[ $key ] = wplng_translate_json( |
| 198 |
$value, |
| 199 |
$args |
| 200 |
); |
| 201 |
|
| 202 |
} elseif ( wplng_str_is_html( $value ) ) { |
| 203 |
|
| 204 |
/** |
| 205 |
* If element is a HTML, parse and translate it |
| 206 |
*/ |
| 207 |
|
| 208 |
$debug_type = 'String - HTML'; |
| 209 |
$array_translated[ $key ] = wplng_translate_html( |
| 210 |
$value, |
| 211 |
$args |
| 212 |
); |
| 213 |
|
| 214 |
} elseif ( wplng_str_is_url( $value ) ) { |
| 215 |
|
| 216 |
/** |
| 217 |
* If element is an URL, replace by translated URL |
| 218 |
*/ |
| 219 |
|
| 220 |
$debug_type = 'String - URL'; |
| 221 |
$array_translated[ $key ] = wplng_url_translate( $value ); |
| 222 |
|
| 223 |
} elseif ( wplng_str_is_script_i18n( $value ) ) { |
| 224 |
|
| 225 |
/** |
| 226 |
* If element is a i18n script, replace by translated script |
| 227 |
*/ |
| 228 |
|
| 229 |
// Voluntarily don't pass $current_parents |
| 230 |
$debug_type = 'String - Script i18n'; |
| 231 |
$array_translated[ $key ] = wplng_translate_js_json_in_i18n_script( $value ); |
| 232 |
|
| 233 |
} else { |
| 234 |
|
| 235 |
/** |
| 236 |
* Element is a unknow string, |
| 237 |
* - check if it's translatable |
| 238 |
* - Check if is an included element |
| 239 |
*/ |
| 240 |
|
| 241 |
if ( ! wplng_text_is_translatable( $value ) ) { |
| 242 |
|
| 243 |
$debug_type = 'String - Untranslatable'; |
| 244 |
|
| 245 |
} elseif ( ! wplng_json_element_is_included( $value, $current_parents ) ) { |
| 246 |
|
| 247 |
$debug_type = 'String - Not included'; |
| 248 |
|
| 249 |
} else { |
| 250 |
|
| 251 |
$debug_type = 'String - Translatable'; |
| 252 |
$array_translated[ $key ] = wplng_get_translated_text_from_translations( |
| 253 |
$value, |
| 254 |
$args['translations'] |
| 255 |
); |
| 256 |
|
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Print debug data in debug.log file |
| 262 |
*/ |
| 263 |
|
| 264 |
if ( true === WPLNG_DEBUG_JSON |
| 265 |
&& isset( $json_decoded[ $key ] ) |
| 266 |
&& isset( $array_translated[ $key ] ) |
| 267 |
// && $debug_type === 'String - Not included' |
| 268 |
) { |
| 269 |
|
| 270 |
$debug = array( |
| 271 |
'title' => 'wpLingua JSON debug', |
| 272 |
'parents' => $current_parents, |
| 273 |
'type' => $debug_type, |
| 274 |
'value' => $json_decoded[ $key ], |
| 275 |
); |
| 276 |
|
| 277 |
if ( $json_decoded[ $key ] !== $array_translated[ $key ] ) { |
| 278 |
$debug['translated'] = $array_translated[ $key ]; |
| 279 |
} |
| 280 |
|
| 281 |
error_log( var_export( $debug, true ) ); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
return $array_translated; |
| 287 |
} |
| 288 |
|