From 70c7962fee55cd10ffb2ab35c19c7d78359bef07 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Sep 2024 20:56:03 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@59029 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/l10n.php | 14 ++++++ .../l10n/class-wp-translation-controller.php | 18 +++++++ .../tests/l10n/wpTranslationController.php | 47 +++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 038b9160499ce..1f3532a40ded5 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1988,3 +1988,17 @@ function wp_get_word_count_type() { return $wp_locale->get_word_count_type(); } + +/** + * Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale. + * + * @since 6.7.0 + * + * @param string $singular Singular translation to check. + * @param string $textdomain Optional. Text domain. Default 'default'. + * @param ?string $locale Optional. Locale. Default current locale. + * @return bool True if the translation exists, false otherwise. + */ +function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool { + return WP_Translation_Controller::get_instance()->has_translation( $singular, $textdomain, $locale ); +} diff --git a/src/wp-includes/l10n/class-wp-translation-controller.php b/src/wp-includes/l10n/class-wp-translation-controller.php index 295e3fcac79a2..c68cf32add6d6 100644 --- a/src/wp-includes/l10n/class-wp-translation-controller.php +++ b/src/wp-includes/l10n/class-wp-translation-controller.php @@ -434,4 +434,22 @@ protected function get_files( string $textdomain = 'default', ?string $locale = return $this->loaded_translations[ $locale ][ $textdomain ] ?? array(); } + + /** + * Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale. + * + * @since 6.7.0 + * + * @param string $singular Singular translation to check. + * @param string $textdomain Optional. Text domain. Default 'default'. + * @param ?string $locale Optional. Locale. Default current locale. + * @return bool True if the translation exists, false otherwise. + */ + public function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool { + if ( null === $locale ) { + $locale = $this->current_locale; + } + + return false !== $this->locate_translation( $singular, $textdomain, $locale ); + } } diff --git a/tests/phpunit/tests/l10n/wpTranslationController.php b/tests/phpunit/tests/l10n/wpTranslationController.php index 6c8fb6dc9d300..00afeaa095cea 100644 --- a/tests/phpunit/tests/l10n/wpTranslationController.php +++ b/tests/phpunit/tests/l10n/wpTranslationController.php @@ -361,4 +361,51 @@ public function test_switch_to_locale_translations_stay_loaded_custom_textdomain $this->assertSame( 'Este es un plugin dummy', $actual ); $this->assertSame( 'This is a dummy plugin', $after ); } + + /** + * @ticket 52696 + * @covers ::has_translation + */ + public function test_has_translation_with_existing_translation() { + load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' ); + $this->assertTrue( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain', 'en_US' ) ); + } + + /** + * @ticket 52696 + * @covers ::has_translation + */ + public function test_has_translation_with_no_translation() { + $this->assertFalse( WP_Translation_Controller::get_instance()->has_translation( 'Goodbye', 'wp-tests-domain', 'en_US' ) ); + } + + /** + * @ticket 52696 + * @covers ::has_translation + */ + public function test_has_translation_with_different_textdomain() { + load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' ); + $this->assertFalse( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'custom-domain', 'en_US' ) ); + } + + /** + * @ticket 52696 + * @covers ::has_translation + */ + public function test_has_translation_with_different_locale() { + switch_to_locale( 'es_ES' ); + load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' ); + $actual = WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain', 'es_ES' ); + restore_previous_locale(); + $this->assertTrue( $actual ); + } + + /** + * @ticket 52696 + * @covers ::has_translation + */ + public function test_has_translation_with_no_locale_provided() { + load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' ); + $this->assertTrue( WP_Translation_Controller::get_instance()->has_translation( 'baba', 'wp-tests-domain' ) ); + } }