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/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.