WP_Textdomain_Registry::get_language_files_from_path( string $path ): array

Retrieves translation files from the specified path.

Description

Allows early retrieval through the ‘pre_get_mo_files_from_path’ filter to optimize performance, especially in directories with many files.

Parameters

$pathstringrequired
The directory path to search for translation files.

Return

array Array of translation file paths. Can contain .mo and .l10n.php files.

Source

public function get_language_files_from_path( $path ) {
	$path = rtrim( $path, '/' ) . '/';

	/**
	 * Filters the translation files retrieved from a specified path before the actual lookup.
	 *
	 * Returning a non-null value from the filter will effectively short-circuit
	 * the MO files lookup, returning that value instead.
	 *
	 * This can be useful in situations where the directory contains a large number of files
	 * and the default glob() function becomes expensive in terms of performance.
	 *
	 * @since 6.5.0
	 *
	 * @param null|array $files List of translation files. Default null.
	 * @param string     $path  The path from which translation files are being fetched.
	 */
	$files = apply_filters( 'pre_get_language_files_from_path', null, $path );

	if ( null !== $files ) {
		return $files;
	}

	$cache_key = md5( $path );
	$files     = wp_cache_get( $cache_key, 'translation_files' );

	if ( false === $files ) {
		$files = glob( $path . '*.mo' );
		if ( false === $files ) {
			$files = array();
		}

		$php_files = glob( $path . '*.l10n.php' );
		if ( is_array( $php_files ) ) {
			$files = array_merge( $files, $php_files );
		}

		wp_cache_set( $cache_key, $files, 'translation_files', HOUR_IN_SECONDS );
	}

	return $files;
}

Hooks

apply_filters( ‘pre_get_language_files_from_path’, null|array $files, string $path )

Filters the translation files retrieved from a specified path before the actual lookup.

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.