Make WordPress Core

Changeset 52809


Ignore:
Timestamp:
02/28/2022 03:40:15 PM (3 years ago)
Author:
SergeyBiryukov
Message:

I18N: Add a $locale parameter for remove_accents().

This highlights the fact that remove_accents() is locale-aware and makes it easier to utilize the function with different locales without having to use switch_to_locale() or the locale filter.

Additionally, this commit relaxes the check for character replacements in German locales to include formal and informal variants of any de_* locale, even if WordPress does not have a native translation for some of them yet.

Props malthert, johnbillion, knutsp, ocean90, SergeyBiryukov.
Fixes #54415.

Location:
trunk
Files:
2 edited

Legend:

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

    r52789 r52809  
    15841584 * @since 4.8.0 Added locale support for `bs_BA`.
    15851585 * @since 5.7.0 Added locale support for `de_AT`.
    1586  *
    1587  * @param string $string Text that might have accent characters
     1586 * @since 6.0.0 Added the `$locale` parameter.
     1587 *
     1588 * @param string $string Text that might have accent characters.
     1589 * @param string $locale Optional. The locale to use for accent removal. Some character
     1590 *                       replacements depend on the locale being used (e.g. 'de_DE').
     1591 *                       Defaults to the current locale.
    15881592 * @return string Filtered string with replaced "nice" characters.
    15891593 */
    1590 function remove_accents( $string ) {
     1594function remove_accents( $string, $locale = '' ) {
    15911595    if ( ! preg_match( '/[\x80-\xff]/', $string ) ) {
    15921596        return $string;
     
    19241928
    19251929        // Used for locale-specific rules.
    1926         $locale = get_locale();
    1927 
    1928         if ( in_array( $locale, array( 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'de_AT' ), true ) ) {
     1930        if ( empty( $locale ) ) {
     1931            $locale = get_locale();
     1932        }
     1933
     1934        /*
     1935         * German has various locales (de_DE, de_CH, de_AT, ...) with formal and informal variants.
     1936         * There is no 3-letter locale like 'def', so checking for 'de' instead of 'de_' is safe,
     1937         * since 'de' itself would be a valid locale too).
     1938         */
     1939        if ( str_starts_with( $locale, 'de' ) ) {
    19291940            $chars['Ä'] = 'Ae';
    19301941            $chars['ä'] = 'ae';
  • trunk/tests/phpunit/tests/formatting/removeAccents.php

    r52010 r52809  
    8181    }
    8282
    83     public function remove_accents_germanic_umlauts_cb() {
    84         return 'de_DE';
    85     }
    86 
    8783    /**
    8884     * @ticket 3782
    8985     */
    9086    public function test_remove_accents_germanic_umlauts() {
    91         add_filter( 'locale', array( $this, 'remove_accents_germanic_umlauts_cb' ) );
    92 
    93         $this->assertSame( 'AeOeUeaeoeuess', remove_accents( 'ÄÖÜäöüß' ) );
    94 
    95         remove_filter( 'locale', array( $this, 'remove_accents_germanic_umlauts_cb' ) );
    96     }
    97 
    98     public function set_locale_to_danish() {
    99         return 'da_DK';
     87        $this->assertSame( 'AeOeUeaeoeuess', remove_accents( 'ÄÖÜäöüß', 'de_DE' ) );
    10088    }
    10189
     
    10492     */
    10593    public function test_remove_danish_accents() {
    106         add_filter( 'locale', array( $this, 'set_locale_to_danish' ) );
    107 
    108         $this->assertSame( 'AeOeAaaeoeaa', remove_accents( 'ÆØÅæøå' ) );
    109 
    110         remove_filter( 'locale', array( $this, 'set_locale_to_danish' ) );
    111     }
    112 
    113     public function set_locale_to_catalan() {
    114         return 'ca';
     94        $this->assertSame( 'AeOeAaaeoeaa', remove_accents( 'ÆØÅæøå', 'da_DK' ) );
    11595    }
    11696
     
    11999     */
    120100    public function test_remove_catalan_middot() {
    121         add_filter( 'locale', array( $this, 'set_locale_to_catalan' ) );
    122 
    123         $this->assertSame( 'allallalla', remove_accents( 'al·lallaŀla' ) );
    124 
    125         remove_filter( 'locale', array( $this, 'set_locale_to_catalan' ) );
    126 
     101        $this->assertSame( 'allallalla', remove_accents( 'al·lallaŀla', 'ca' ) );
    127102        $this->assertSame( 'al·lallalla', remove_accents( 'al·lallaŀla' ) );
    128     }
    129 
    130     public function set_locale_to_serbian() {
    131         return 'sr_RS';
    132103    }
    133104
     
    136107     */
    137108    public function test_transcribe_serbian_crossed_d() {
    138         add_filter( 'locale', array( $this, 'set_locale_to_serbian' ) );
    139 
    140         $this->assertSame( 'DJdj', remove_accents( 'Đđ' ) );
    141 
    142         remove_filter( 'locale', array( $this, 'set_locale_to_serbian' ) );
    143 
     109        $this->assertSame( 'DJdj', remove_accents( 'Đđ', 'sr_RS' ) );
    144110        $this->assertSame( 'Dd', remove_accents( 'Đđ' ) );
    145111    }
Note: See TracChangeset for help on using the changeset viewer.