Make WordPress Core

Changeset 55279


Ignore:
Timestamp:
02/07/2023 05:26:14 PM (21 months ago)
Author:
audrasjb
Message:

I18N: Introduce word_count_type property to WP_Locale.

This changesets adds a word_count_type property, so that it does not need to be translated separately across multiple projects.

List of changes:

  • New property: WP_Locale::word_count_type.
  • New method: WP_Locale::get_word_count_type().
  • New function: wp_get_word_count_type() as a wrapper for WP_Locale::get_word_count_type().
  • All _x( 'words', 'Word count type. Do not translate!' ) strings have been replaced with a call to wp_get_word_count_type().

Props pedromendonca, desrosj, costdev, mukesh27, johnbillion.
Fixes #56698.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-locale.php

    r55047 r55279  
    112112     */
    113113    public $list_item_separator;
     114
     115    /**
     116     * The word count type of the locale language.
     117     *
     118     * Default is 'words'.
     119     *
     120     * @since 6.2.0
     121     * @var string
     122     */
     123    public $word_count_type;
    114124
    115125    /**
     
    237247            $this->text_direction = 'rtl';
    238248        }
     249
     250        // Set the word count type.
     251        $this->word_count_type = $this->get_word_count_type();
    239252    }
    240253
     
    397410        return $this->list_item_separator;
    398411    }
     412
     413    /**
     414     * Retrieves the localized word count type.
     415     *
     416     * Options are 'characters_excluding_spaces', 'characters_including_spaces or 'words'. Defaults to 'words'.
     417     *
     418     * @since 6.2.0
     419     *
     420     * @return string Localized word count type.
     421     */
     422    public function get_word_count_type() {
     423
     424        /*
     425         * translators: If your word count is based on single characters (e.g. East Asian characters),
     426         * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
     427         * Do not translate into your own language.
     428         */
     429        $word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type;
     430
     431        // Check for valid types.
     432        if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
     433            // Defaults to 'words'.
     434            $word_count_type = 'words';
     435        }
     436
     437        return $word_count_type;
     438    }
    399439}
  • trunk/src/wp-includes/formatting.php

    r55272 r55279  
    39463946    $num_words     = (int) $num_words;
    39473947
    3948     /*
    3949      * translators: If your word count is based on single characters (e.g. East Asian characters),
    3950      * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
    3951      * Do not translate into your own language.
    3952      */
    3953     if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
     3948    if ( str_starts_with( wp_get_word_count_type(), 'characters' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
    39543949        $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
    39553950        preg_match_all( '/./u', $text, $words_array );
  • trunk/src/wp-includes/l10n.php

    r55196 r55279  
    18101810    return $wp_locale->get_list_item_separator();
    18111811}
     1812
     1813/**
     1814 * Retrieves the word count type based on the locale.
     1815 *
     1816 * @since 6.2.0
     1817 *
     1818 * @global WP_Locale $wp_locale WordPress date and time locale object.
     1819 *
     1820 * @return string Locale-specific word count type.
     1821 */
     1822function wp_get_word_count_type() {
     1823    global $wp_locale;
     1824
     1825    return $wp_locale->get_word_count_type();
     1826}
  • trunk/src/wp-includes/script-loader.php

    r55275 r55279  
    18321832        'wordCountL10n',
    18331833        array(
    1834             /*
    1835              * translators: If your word count is based on single characters (e.g. East Asian characters),
    1836              * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
    1837              * Do not translate into your own language.
    1838              */
    1839             'type'       => _x( 'words', 'Word count type. Do not translate!' ),
     1834            'type'       => wp_get_word_count_type(),
    18401835            'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
    18411836        )
  • trunk/tests/phpunit/tests/locale.php

    r55047 r55279  
    174174        $this->assertFalse( $this->locale->is_rtl() );
    175175    }
     176
     177    /**
     178     * Tests that `WP_Locale::get_word_count_type()` returns
     179     * the appropriate value.
     180     *
     181     * @ticket 56698
     182     *
     183     * @covers WP_Locale::get_word_count_type
     184     *
     185     * @dataProvider data_get_word_count_type
     186     *
     187     * @param string $word_count_type The word count type.
     188     * @param string $expected        The expected return value.
     189     */
     190    public function test_get_word_count_type( $word_count_type, $expected ) {
     191        if ( is_string( $word_count_type ) ) {
     192            $this->locale->word_count_type = $word_count_type;
     193
     194        }
     195
     196        $this->assertSame( $expected, $this->locale->get_word_count_type() );
     197    }
     198
     199    /**
     200     * Data provider.
     201     *
     202     * @return array[]
     203     */
     204    public function data_get_word_count_type() {
     205        return array(
     206            'default'                   => array(
     207                'word_count_type' => null,
     208                'expected'        => 'words',
     209            ),
     210            'empty string'              => array(
     211                'word_count_type' => '',
     212                'expected'        => 'words',
     213            ),
     214            'an invalid option - "foo"' => array(
     215                'word_count_type' => 'foo',
     216                'expected'        => 'words',
     217            ),
     218            'a valid option - "words"'  => array(
     219                'word_count_type' => 'words',
     220                'expected'        => 'words',
     221            ),
     222            'a valid option - "characters_excluding_spaces"' => array(
     223                'word_count_type' => 'characters_excluding_spaces',
     224                'expected'        => 'characters_excluding_spaces',
     225            ),
     226            'a valid option - "characters_including_spaces"' => array(
     227                'word_count_type' => 'characters_including_spaces',
     228                'expected'        => 'characters_including_spaces',
     229            ),
     230        );
     231    }
    176232}
    177233
Note: See TracChangeset for help on using the changeset viewer.