Changeset 7899
- Timestamp:
- 11/26/2018 06:01:12 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php
r6701 r7899 401 401 $translations = $this->get_gp_translations( $slug, $branch, $originals, $translation_set_id ); 402 402 403 // Sort the originals so as to process long strings first. 404 uasort( $originals, function( $a, $b ) { 405 $a_len = strlen( $a->singular ); 406 $b_len = strlen( $b->singular ); 407 408 return $a_len == $b_len ? 0 : ($a_len > $b_len ? -1 : 1); 409 } ); 410 411 // Mark each original for translation 403 412 foreach ( $originals as $original ) { 404 413 if ( ! empty( $original->id ) && array_key_exists( $original->id, $translations ) ) { 405 $content = $this-> translate_gp_original( $original->singular, $translations[ $original->id ], $content );414 $content = $this->mark_gp_original( $original->id, $original->singular, $content ); 406 415 } 407 416 } 408 417 418 // Translate the marked originals. 419 $content = $this->translate_marked_gp_originals( $content, $translations, $originals ); 420 409 421 $this->cache_set( $slug, $branch, $content, $cache_suffix ); 410 422 … … 413 425 414 426 /** 415 * Takes content, searches for $original, and replaces it by $translation.416 * 417 * @param string $original English string.418 * @param string $ translation Translation.427 * Takes content, searches for $original, and replaces it with a marker for later translation. 428 * 429 * @param string $original_id Original English Translation ID. 430 * @param string $original Original English String. 419 431 * @param string $content Content to be searched. 420 432 * @return mixed 421 433 */ 422 public function translate_gp_original( $original, $translation, $content ) { 434 public function mark_gp_original( $original_id, $original, $content ) { 435 $marker = "___TRANSLATION_{$original_id}___"; 436 423 437 if ( $original === $content ) { 424 $content = $ translation;438 $content = $marker; 425 439 } else { 426 440 $original = preg_quote( $original, '/' ); 427 441 428 442 if ( false === strpos( $content, '<' ) ) { 429 // Don't use $translation, it may contain backreference-like characters. 430 $content = preg_replace( "/\b{$original}\b/", '___TRANSLATION___', $content ); 443 $content = preg_replace( "/\b{$original}\b/", $marker, $content ); 431 444 } else { 432 // 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 ); 445 $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\${1}{$marker}\${3}", $content ); 434 446 } 435 436 $content = str_replace( '___TRANSLATION___', $translation, $content );437 447 } 438 448 439 449 return $content; 450 } 451 452 /** 453 * Translates marked translations $content from ::mark_gp_original(). 454 * 455 * @param string $content The Content to be searched. 456 * @param array $translations The Translations. 457 * @param array $originals The Originals. 458 * @return string 459 */ 460 public function translate_marked_gp_originals( $content, $translations, $originals ) { 461 return preg_replace_callback( 462 '/___TRANSLATION_(\d+)___/', 463 function( $m ) use( $translations, $originals ) { 464 $marker = $m[0]; 465 $id = $m[1]; 466 467 // The translation by ID, Original by ID, or the marker if it was never actually marking a original translation. 468 return $translations[ $id ] ?? ( $originals[ $id ] ?? $marker ); 469 }, 470 $content 471 ); 440 472 } 441 473
Note: See TracChangeset
for help on using the changeset viewer.