Ticket #3929: 3929.diff
File 3929.diff, 4.2 KB (added by , 6 years ago) |
---|
-
class-plugin-i18n.php
class Plugin_I18n { 368 368 $post->stable_tag = get_post_meta( $post->ID, 'stable_tag', true ) ?: 'trunk'; 369 369 370 370 if ( empty( $slug ) ) { 371 371 return $content; 372 372 } 373 373 374 374 $branch = ( empty( $post->stable_tag ) || 'trunk' === $post->stable_tag ) ? 'dev' : 'stable'; 375 375 376 376 if ( empty( $args['code_i18n'] ) || true !== $args['code_i18n'] ) { 377 377 $branch .= '-readme'; 378 378 } 379 379 380 380 $cache_suffix = "{$locale}:{$key}"; 381 381 382 382 // Try the cache. 383 if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) { 383 // TODO reenable 384 /* if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) { 384 385 // DEBUG 385 //var_dump( array( $slug, $branch, $cache_suffix, $cache ) );386 var_dump( array( $slug, $branch, $cache_suffix, $cache ) ); 386 387 return $cache; 387 } 388 }*/ 388 389 389 390 $originals = $this->get_gp_originals( $slug, $branch, $key, $content ); 390 391 391 392 if ( empty( $originals ) ) { 392 393 return $content; 393 394 } 394 395 395 396 $translation_set_id = $this->get_gp_translation_set_id( $slug, $branch, $locale ); 396 397 397 398 if ( empty( $translation_set_id ) ) { 398 399 return $content; 399 400 } 400 401 401 402 $translations = $this->get_gp_translations( $slug, $branch, $originals, $translation_set_id ); 402 403 404 // Sort the originals so that we match the longer originals first. 405 uasort( $originals, function( $a, $b ) { 406 $a_len = strlen( $a->singular ); 407 $b_len = strlen( $b->singular ); 408 409 return $a_len == $b_len ? 0 : ($a_len > $b_len ? -1 : 1); 410 } ); 411 412 // Mark each original for translation 403 413 foreach ( $originals as $original ) { 404 if ( ! empty( $original->id ) && array_key_exists( $original->id, $translations )) {405 $content = $this-> translate_gp_original( $original->singular, $translations[ $original->id ], $content );414 if ( ! empty( $original->id ) ) { 415 $content = $this->mark_gp_original( $original, $content ); 406 416 } 407 417 } 408 418 409 $this->cache_set( $slug, $branch, $content, $cache_suffix ); 419 // Translate each original marked 420 $content = preg_replace_callback( '!___TRANSLATION_(\d+)___!', function( $m ) use( $originals, $translations ) { 421 return $translations[ $m[1] ] ?? $originals[ $m[1] ]; 422 }, $content ); 423 424 return $content; 425 // TODO reenable 426 //$this->cache_set( $slug, $branch, $content, $cache_suffix ); 410 427 411 428 return $content; 412 429 } 413 430 414 431 /** 415 432 * Takes content, searches for $original, and replaces it by $translation. 416 433 * 417 434 * @param string $original English string. 418 435 * @param string $translation Translation. 419 436 * @param string $content Content to be searched. 420 437 * @return mixed 421 438 */ 422 public function translate_gp_original( $original, $translation, $content ) { 439 public function mark_gp_original( $original_obj, $content ) { 440 $marker = "___TRANSLATION_{$original_obj->id}___"; 441 $original = $original_obj->singular; 442 423 443 if ( $original === $content ) { 424 $content = $ translation;444 $content = $marker; 425 445 } else { 426 446 $original = preg_quote( $original, '/' ); 427 447 428 448 if ( false === strpos( $content, '<' ) ) { 429 449 // Don't use $translation, it may contain backreference-like characters. 430 $content = preg_replace( "/\b{$original}\b/", '___TRANSLATION___', $content );450 $content = preg_replace( "/\b{$original}\b/", $marker, $content ); 431 451 } else { 432 452 // Don't use $translation, it may contain backreference-like characters. 433 $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", '${1}___TRANSLATION___${3}', $content );453 $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\${1}{$marker}\${3}", $content ); 434 454 } 435 436 $content = str_replace( '___TRANSLATION___', $translation, $content );437 455 } 438 456 439 457 return $content; 440 458 } 441 459 442 460 /** 443 461 * Returns a list of translation locales for a given plugin slug and branch. 444 462 * 445 463 * @param string $slug Plugin slug. 446 464 * @param string $branch Branch - 'stable-readme' for example. 447 465 * @param int $min_percent Optional. Only return locales where percent_translated is >= this value. 448 466 * @return array 449 467 */ 450 468 public function find_all_translations_for_plugin( $slug, $branch, $min_percent = 0 ) { 451 469 $post = Plugin_Directory::get_plugin_post( $slug );