WP_Textdomain_Registry::get_path_from_lang_dir │ private │ WP 6.1.0

Gets the path to the language directory for the current domain and locale.

Checks the plugins and themes language directories as well as any custom directory set via load_plugin_textdomain() or load_theme_textdomain().

Method of the class: WP_Textdomain_Registry{}

No Hooks.

Returns

string|false. Language directory path or false if there is none available.

Usage

// private - for code of main (parent) class only
$result = $this->get_path_from_lang_dir( $domain, $locale );
$domain(string) (required)
Text domain.
$locale(string) (required)
Locale.

Notes

See: _get_path_to_translation_from_lang_dir()

Changelog

Since 6.1.0 Introduced.

WP_Textdomain_Registry::get_path_from_lang_dir() code WP 7.1.2

private function get_path_from_lang_dir( $domain, $locale ) {
	$locations = $this->get_paths_for_domain( $domain );

	$found_location = false;

	foreach ( $locations as $location ) {
		$files = $this->get_language_files_from_path( $location );

		$mo_path  = "$location/$domain-$locale.mo";
		$php_path = "$location/$domain-$locale.l10n.php";

		foreach ( $files as $file_path ) {
			if (
				! in_array( $domain, $this->domains_with_translations, true ) &&
				str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )
			) {
				$this->domains_with_translations[] = $domain;
			}

			if ( $file_path === $mo_path || $file_path === $php_path ) {
				$found_location = rtrim( $location, '/' ) . '/';
				break 2;
			}
		}
	}

	if ( $found_location ) {
		$this->set( $domain, $locale, $found_location );

		return $found_location;
	}

	/*
	 * If no path is found for the given locale and a custom path has been set
	 * using load_plugin_textdomain/load_theme_textdomain, use that one.
	 */
	if ( isset( $this->custom_paths[ $domain ] ) ) {
		$fallback_location = rtrim( $this->custom_paths[ $domain ], '/' ) . '/';
		$this->set( $domain, $locale, $fallback_location );
		return $fallback_location;
	}

	$this->set( $domain, $locale, false );

	return false;
}