Skip to content

Commit

Permalink
I18N: Add a new way to determine whether a translation is available.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
swissspidy committed Sep 17, 2024
1 parent 4dafd58 commit 70c7962
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
18 changes: 18 additions & 0 deletions src/wp-includes/l10n/class-wp-translation-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
47 changes: 47 additions & 0 deletions tests/phpunit/tests/l10n/wpTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}

0 comments on commit 70c7962

Please sign in to comment.