Make WordPress Core


Ignore:
Timestamp:
09/17/2024 08:56:03 PM (5 months ago)
Author:
swissspidy
Message:

I18N: Add a new way to determine whether a translation is available.

A new has_translation() function can be used to determine whether a translation exists for a given string.

Props louiswol94, swissspidy, drzraf, ckanitz, tomhine, mchirag2002, samuelsilvapt.
Fixes #52696.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n/class-wp-translation-controller.php

    r58155 r59029  
    435435        return $this->loaded_translations[ $locale ][ $textdomain ] ?? array();
    436436    }
     437
     438    /**
     439     * Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale.
     440     *
     441     * @since 6.7.0
     442     *
     443     * @param string  $singular   Singular translation to check.
     444     * @param string  $textdomain Optional. Text domain. Default 'default'.
     445     * @param ?string $locale     Optional. Locale. Default current locale.
     446     * @return bool  True if the translation exists, false otherwise.
     447     */
     448    public function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool {
     449        if ( null === $locale ) {
     450            $locale = $this->current_locale;
     451        }
     452
     453        return false !== $this->locate_translation( $singular, $textdomain, $locale );
     454    }
    437455}
Note: See TracChangeset for help on using the changeset viewer.