Make WordPress Core

Changeset 59029


Ignore:
Timestamp:
09/17/2024 08:56:03 PM (4 weeks 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.

Location:
trunk
Files:
3 edited

Legend:

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

    r58095 r59029  
    19891989    return $wp_locale->get_word_count_type();
    19901990}
     1991
     1992/**
     1993 * Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale.
     1994 *
     1995 * @since 6.7.0
     1996 *
     1997 * @param string  $singular   Singular translation to check.
     1998 * @param string  $textdomain Optional. Text domain. Default 'default'.
     1999 * @param ?string $locale     Optional. Locale. Default current locale.
     2000 * @return bool  True if the translation exists, false otherwise.
     2001 */
     2002function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool {
     2003    return WP_Translation_Controller::get_instance()->has_translation( $singular, $textdomain, $locale );
     2004}
  • 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}
  • trunk/tests/phpunit/tests/l10n/wpTranslationController.php

    r57350 r59029  
    362362        $this->assertSame( 'This is a dummy plugin', $after );
    363363    }
     364
     365    /**
     366     * @ticket 52696
     367     * @covers ::has_translation
     368     */
     369    public function test_has_translation_with_existing_translation() {
     370        load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );
     371        $this->assertTrue( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain', 'en_US' ) );
     372    }
     373
     374    /**
     375     * @ticket 52696
     376     * @covers ::has_translation
     377     */
     378    public function test_has_translation_with_no_translation() {
     379        $this->assertFalse( WP_Translation_Controller::get_instance()->has_translation( 'Goodbye', 'wp-tests-domain', 'en_US' ) );
     380    }
     381
     382    /**
     383     * @ticket 52696
     384     * @covers ::has_translation
     385     */
     386    public function test_has_translation_with_different_textdomain() {
     387        load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );
     388        $this->assertFalse( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'custom-domain', 'en_US' ) );
     389    }
     390
     391    /**
     392     * @ticket 52696
     393     * @covers ::has_translation
     394     */
     395    public function test_has_translation_with_different_locale() {
     396        switch_to_locale( 'es_ES' );
     397        load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );
     398        $actual = WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain', 'es_ES' );
     399        restore_previous_locale();
     400        $this->assertTrue( $actual );
     401    }
     402
     403    /**
     404     * @ticket 52696
     405     * @covers ::has_translation
     406     */
     407    public function test_has_translation_with_no_locale_provided() {
     408        load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );
     409        $this->assertTrue( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain' ) );
     410    }
    364411}
Note: See TracChangeset for help on using the changeset viewer.