Making WordPress.org

Changeset 8317


Ignore:
Timestamp:
02/22/2019 06:45:02 AM (5 years ago)
Author:
dd32
Message:

Translate: Add a custom warning to catch translated placeholders.

See https://core.trac.wordpress.org/ticket/46305
Fixes #4196.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-custom-warnings/wporg-gp-custom-warnings.php

    r7314 r8317  
    3030     * This allows for the scheme to change, and for WordPress.org URL's to change to a subdomain.
    3131     *
    32      * @param string $original    The orignal string.
     32     * @param string $original    The original string.
    3333     * @param string $translation The translated string.
    3434     */
     
    9696    }
    9797
     98
     99    /**
     100     * Adds a warning for changing placeholders.
     101     *
     102     * This only supports placeholders in the format of '###[A-Z_]+###'.
     103     *
     104     * @param string $original    The original string.
     105     * @param string $translation The translated string.
     106     */
     107    public function warning_mismatching_placeholders( $original, $translation ) {
     108        $placeholder_regex = '@(###[A-Z_]+###)@';
     109
     110        preg_match_all( $placeholder_regex, $original, $original_placeholders );
     111        $original_placeholders = array_unique( $original_placeholders[0] );
     112
     113        preg_match_all( $placeholder_regex, $translation, $translation_placeholders );
     114        $translation_placeholders = array_unique( $translation_placeholders[0] );
     115
     116        $missing_placeholders = array_diff( $original_placeholders, $translation_placeholders );
     117        $added_placeholders = array_diff( $translation_placeholders, $original_placeholders );
     118        if ( ! $missing_placeholders && ! $added_placeholders ) {
     119            return true;
     120        }
     121
     122        // Error.
     123        $error = '';
     124        if ( $missing_placeholders ) {
     125            $error .= "The translation appears to be missing the following placeholders: " . implode( ', ', $missing_placeholders ) . "\n";
     126        }
     127        if ( $added_placeholders ) {
     128            $error .= "The translation contains the following unexpected placeholders: " . implode( ', ', $added_placeholders );
     129        }
     130
     131        return trim( $error );
     132    }
     133
    98134    /**
    99135     * Registers all methods starting with warning_ with GlotPress.
Note: See TracChangeset for help on using the changeset viewer.