Skip to content

Commit

Permalink
I18N: Rename WP_Translation_Controller::instance() method to `get_i…
Browse files Browse the repository at this point in the history
…nstance()`.

This improves consistency as `get_instance()` is more commonly used in core. 

See #59656.

git-svn-id: https://develop.svn.wordpress.org/trunk@57350 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy committed Jan 25, 2024
1 parent fa441af commit a615250
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-locale-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function change_locale( $locale ) {

$wp_locale = new WP_Locale();

WP_Translation_Controller::instance()->set_locale( $locale );
WP_Translation_Controller::get_instance()->set_locale( $locale );

/**
* Fires when the locale is switched to or restored.
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
$locale = determine_locale();
}

$i18n_controller = WP_Translation_Controller::instance();
$i18n_controller = WP_Translation_Controller::get_instance();

// Ensures the correct locale is set as the current one, in case it was filtered.
$i18n_controller->set_locale( $locale );
Expand Down Expand Up @@ -911,7 +911,7 @@ function unload_textdomain( $domain, $reloadable = false ) {

// Since multiple locales are supported, reloadable text domains don't actually need to be unloaded.
if ( ! $reloadable ) {
WP_Translation_Controller::instance()->unload_textdomain( $domain );
WP_Translation_Controller::get_instance()->unload_textdomain( $domain );
}

if ( isset( $l10n[ $domain ] ) ) {
Expand Down
22 changes: 15 additions & 7 deletions src/wp-includes/l10n/class-wp-translation-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,28 @@ final class WP_Translation_Controller {
protected $loaded_files = array();

/**
* Returns the WP_Translation_Controller singleton.
* Container for the main instance of the class.
*
* @since 6.5.0
* @var WP_Translation_Controller|null
*/
private static $instance = null;

/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 6.5.0
*
* @return WP_Translation_Controller
*/
public static function instance(): WP_Translation_Controller {
static $instance;

if ( ! $instance ) {
$instance = new self();
public static function get_instance(): WP_Translation_Controller {
if ( null === self::$instance ) {
self::$instance = new self();
}

return $instance;
return self::$instance;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@
$GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher();
$GLOBALS['wp_locale_switcher']->init();

WP_Translation_Controller::instance()->set_locale( $locale );
WP_Translation_Controller::get_instance()->set_locale( $locale );

// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
Expand Down
38 changes: 19 additions & 19 deletions tests/phpunit/tests/l10n/wpTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function test_load_textdomain() {

$compat_instance = $l10n['wp-tests-domain'] ?? null;

$is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' );
$headers = WP_Translation_Controller::instance()->get_headers( 'wp-tests-domain' );
$entries = WP_Translation_Controller::instance()->get_entries( 'wp-tests-domain' );
$is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' );
$headers = WP_Translation_Controller::get_instance()->get_headers( 'wp-tests-domain' );
$entries = WP_Translation_Controller::get_instance()->get_entries( 'wp-tests-domain' );

$unload_successful = unload_textdomain( 'wp-tests-domain' );

Expand Down Expand Up @@ -76,7 +76,7 @@ public function test_load_textdomain_existing_override() {

$is_loaded_wp = is_textdomain_loaded( 'wp-tests-domain' );

$is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' );
$is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' );

remove_filter( 'override_load_textdomain', '__return_true' );

Expand All @@ -102,7 +102,7 @@ public function test_load_textdomain_php_files() {
public function test_load_textdomain_prefers_php_files_by_default() {
$load_successful = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );

$instance = WP_Translation_Controller::instance();
$instance = WP_Translation_Controller::get_instance();

$is_loaded = $instance->is_textdomain_loaded( 'wp-tests-domain', 'en_US' );

Expand Down Expand Up @@ -259,9 +259,9 @@ public function test_unload_textdomain() {

$compat_instance = $l10n['wp-tests-domain'] ?? null;

$is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' );
$headers = WP_Translation_Controller::instance()->get_headers( 'wp-tests-domain' );
$entries = WP_Translation_Controller::instance()->get_entries( 'wp-tests-domain' );
$is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' );
$headers = WP_Translation_Controller::get_instance()->get_headers( 'wp-tests-domain' );
$entries = WP_Translation_Controller::get_instance()->get_entries( 'wp-tests-domain' );

$this->assertNull( $compat_instance, 'Compat instance was not removed' );
$this->assertTrue( $unload_successful, 'Text domain not successfully unloaded' );
Expand All @@ -281,13 +281,13 @@ public function test_unload_textdomain_existing_override() {

$unload_successful = unload_textdomain( 'wp-tests-domain' );

$is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' );
$is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' );

remove_filter( 'override_unload_textdomain', '__return_true' );

$unload_successful_after = unload_textdomain( 'wp-tests-domain' );

$is_loaded_after = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' );
$is_loaded_after = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' );

$this->assertTrue( $unload_successful );
$this->assertTrue( $is_loaded );
Expand Down Expand Up @@ -317,14 +317,14 @@ public function test_switch_to_locale_translations_stay_loaded_default_textdomai

$actual = __( 'Invalid parameter.' );

$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded() );
$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded() );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) );

restore_previous_locale();

$actual_2 = __( 'Invalid parameter.' );

$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) );

$this->assertSame( 'Parámetro no válido. ', $actual );
$this->assertSame( 'Invalid parameter.', $actual_2 );
Expand All @@ -336,7 +336,7 @@ public function test_switch_to_locale_translations_stay_loaded_default_textdomai
* @covers ::change_locale
*/
public function test_switch_to_locale_translations_stay_loaded_custom_textdomain() {
$this->assertSame( 'en_US', WP_Translation_Controller::instance()->get_locale() );
$this->assertSame( 'en_US', WP_Translation_Controller::get_instance()->get_locale() );

require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';

Expand All @@ -346,16 +346,16 @@ public function test_switch_to_locale_translations_stay_loaded_custom_textdomain

$actual = i18n_plugin_test();

$this->assertSame( 'es_ES', WP_Translation_Controller::instance()->get_locale() );
$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) );
$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) );
$this->assertFalse( WP_Translation_Controller::instance()->is_textdomain_loaded( 'foo-bar', 'es_ES' ) );
$this->assertSame( 'es_ES', WP_Translation_Controller::get_instance()->get_locale() );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) );
$this->assertFalse( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'foo-bar', 'es_ES' ) );

restore_previous_locale();

$after = i18n_plugin_test();

$this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) );
$this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) );

$this->assertSame( 'This is a dummy plugin', $before );
$this->assertSame( 'Este es un plugin dummy', $actual );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/l10n/wpTranslationsConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class WP_Translation_Controller_Convert_Tests extends WP_UnitTestCase {
* @covers ::instance
*/
public function test_get_instance() {
$instance = WP_Translation_Controller::instance();
$instance2 = WP_Translation_Controller::instance();
$instance = WP_Translation_Controller::get_instance();
$instance2 = WP_Translation_Controller::get_instance();

$this->assertSame( $instance, $instance2 );
}
Expand Down

0 comments on commit a615250

Please sign in to comment.