Making WordPress.org

Changeset 7899


Ignore:
Timestamp:
11/26/2018 06:01:12 AM (6 years ago)
Author:
dd32
Message:

Plugin Directory: i18n: When translating the content of a plugins readme, don't apply the translations to other translations.

Fixes #3929.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php

    r6701 r7899  
    401401        $translations = $this->get_gp_translations( $slug, $branch, $originals, $translation_set_id );
    402402
     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
    403412        foreach ( $originals as $original ) {
    404413            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 );
    406415            }
    407416        }
    408417
     418        // Translate the marked originals.
     419        $content = $this->translate_marked_gp_originals( $content, $translations, $originals );
     420
    409421        $this->cache_set( $slug, $branch, $content, $cache_suffix );
    410422
     
    413425
    414426    /**
    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.
    419431     * @param string $content     Content to be searched.
    420432     * @return mixed
    421433     */
    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
    423437        if ( $original === $content ) {
    424             $content = $translation;
     438            $content = $marker;
    425439        } else {
    426440            $original = preg_quote( $original, '/' );
    427441
    428442            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 );
    431444            } 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 );
    434446            }
    435 
    436             $content = str_replace( '___TRANSLATION___', $translation, $content );
    437447        }
    438448
    439449        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        );
    440472    }
    441473
Note: See TracChangeset for help on using the changeset viewer.