WP_Translation_Controller::locate_translation()protectedWP 6.5.0

Locates translation for a given string and text domain.

Method of the class: WP_Translation_Controller{}

No Hooks.

Returns

Array{source:. WP_Translation_File, entries: string[]}|false {

Translations on success, false otherwise.
@type WP_Translation_File $source Translation file instance.
@type string[]            $entries Array of translation entries.

}

Usage

// protected - for code of main (parent) or child class
$result = $this->locate_translation( $singular, $textdomain, ?string $locale );
$singular(string) (required)
Singular translation.
$textdomain(string)
Text domain.
Default: 'default'
?string $locale **
-
Default: null

Changelog

Since 6.5.0 Introduced.

WP_Translation_Controller::locate_translation() code WP 6.8.1

protected function locate_translation( string $singular, string $textdomain = 'default', ?string $locale = null ) {
	if ( array() === $this->loaded_translations ) {
		return false;
	}

	// Find the translation in all loaded files for this text domain.
	foreach ( $this->get_files( $textdomain, $locale ) as $moe ) {
		$translation = $moe->translate( $singular );
		if ( false !== $translation ) {
			return array(
				'entries' => explode( "\0", $translation ),
				'source'  => $moe,
			);
		}
		if ( null !== $moe->error() ) {
			// Unload this file, something is wrong.
			$this->unload_file( $moe, $textdomain, $locale );
		}
	}

	// Nothing could be found.
	return false;
}