Make WordPress Core


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.

File:
1 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';
Note: See TracChangeset for help on using the changeset viewer.