WordPress.org

Make WordPress Core

Ticket #42725: 42725.diff

File 42725.diff, 2.1 KB (added by danieltj, 3 years ago)

Alternate implementation for the _g function

  • wp-includes/l10n.php

    diff --git wp-includes/l10n.php wp-includes/l10n.php
    index a273b0c..638091f 100644
    function esc_html_x( $text, $context, $domain = 'default' ) { 
    340340        return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
    341341}
    342342
     343/**
     344 * Translates a string based on the gender pronouns passed to it.
     345 *
     346 * This function takes a string with an expected a placeholder to be
     347 * swapped out with a gender specific pronoun which would allow for
     348 * strings to change to cater towards the user's gender.
     349 *
     350 * Examples of using `_g` to use correct gender based pronouns.
     351 *
     352 * Male: He was editing the post.
     353 * Female: She is creating a new taxonomy.
     354 * Other: They have activated a plugin.
     355 *
     356 * @since 4.9
     357 *
     358 * @param string $text    The text to translate.
     359 * @param string $male    Male string version.
     360 * @param string $female  Female string version.
     361 * @param string $other   Non-specific gender version.
     362 * @param int    $user_id The user's id.
     363 * @param string $domain  The text domain.
     364 *
     365 * @return string The translated string with gender.
     366 */
     367function _g( $text, $male, $female, $other, $user_id = 0, $domain = 'default' ) {
     368
     369        // Check if the user id was set.
     370        if ( 0 == $user_id ) {
     371                $user_id = get_current_user_id();
     372        }
     373
     374        // Get the users gender preference.
     375        $get_gender = get_user_meta($user_id, 'gender', true);
     376
     377        // Translate each string that is provided.
     378        $translate_text = translate( $text, $domain );
     379        $translate_male = translate( $male, $domain );
     380        $translate_female = translate( $female, $domain );
     381        $translate_other = translate( $other, $domain );
     382
     383        // Check which gender is selected.
     384        if ( 'male' == $get_gender ) {
     385                $the_gender = $translate_male;
     386        } elseif ( 'female' == $user_gender ) {
     387                $the_gender = $translate_female;
     388        } else {
     389                $the_gender = $translate_other;
     390        }
     391
     392        // Add the gender string to the main string.
     393        $string = sprintf( $translate_text, $the_gender );
     394
     395        // Return string for output.
     396        return $string
     397
     398}
     399
    343400/**
    344401 * Translates and retrieves the singular or plural form based on the supplied number.
    345402 *