I18N: Support loading `.l10n.php` translation files on their own.

Adjusts the translation file lookup in `WP_Textdomain_Registry` so that just-in-time translation loading
works even if there is only a `.l10n.php` translation file without a corresponding `.mo` file.

While language packs continue to contain both file types, this makes it easier to use translations in a project
without having to deal with `.mo` or `.po` files.

Props Chrystl.
See #59656.
Built from https://develop.svn.wordpress.org/trunk@57516


git-svn-id: http://core.svn.wordpress.org/trunk@57017 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-02-01 19:05:13 +00:00
parent 3664cc3698
commit 5b1d220d1e
3 changed files with 34 additions and 31 deletions

View File

@ -150,21 +150,21 @@ class WP_Textdomain_Registry {
}
/**
* Retrieves .mo files from the specified path.
* Retrieves translation files from the specified path.
*
* Allows early retrieval through the {@see 'pre_get_mo_files_from_path'} filter to optimize
* performance, especially in directories with many files.
*
* @since 6.5.0
*
* @param string $path The directory path to search for .mo files.
* @return array Array of .mo file paths.
* @param string $path The directory path to search for translation files.
* @return array Array of translation file paths. Can contain .mo and .l10n.php files.
*/
public function get_language_files_from_path( $path ) {
$path = rtrim( $path, '/' ) . '/';
/**
* Filters the .mo files retrieved from a specified path before the actual lookup.
* 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.
@ -174,27 +174,33 @@ class WP_Textdomain_Registry {
*
* @since 6.5.0
*
* @param null|array $mo_files List of .mo files. Default null.
* @param string $path The path from which .mo files are being fetched.
* @param null|array $files List of translation files. Default null.
* @param string $path The path from which translation files are being fetched.
**/
$mo_files = apply_filters( 'pre_get_language_files_from_path', null, $path );
$files = apply_filters( 'pre_get_language_files_from_path', null, $path );
if ( null !== $mo_files ) {
return $mo_files;
if ( null !== $files ) {
return $files;
}
$cache_key = 'cached_mo_files_' . md5( $path );
$mo_files = wp_cache_get( $cache_key, 'translations' );
$files = wp_cache_get( $cache_key, 'translations' );
if ( false === $mo_files ) {
$mo_files = glob( $path . '*.mo' );
if ( false === $mo_files ) {
$mo_files = array();
}
wp_cache_set( $cache_key, $mo_files, 'translations' );
if ( false === $files ) {
$files = glob( $path . '*.mo' );
if ( false === $files ) {
$files = array();
}
return $mo_files;
$php_files = glob( $path . '*.l10n.php' );
if ( is_array( $php_files ) ) {
$files = array_merge( $files, $php_files );
}
wp_cache_set( $cache_key, $files, 'translations' );
}
return $files;
}
/**
@ -295,17 +301,18 @@ class WP_Textdomain_Registry {
foreach ( $locations as $location ) {
$files = $this->get_language_files_from_path( $location );
$path = "$location/$domain-$locale.mo";
$mo_path = "$location/$domain-$locale.mo";
$php_path = "$location/$domain-$locale.l10n.php";
foreach ( $files as $mo_path ) {
foreach ( $files as $file_path ) {
if (
! in_array( $domain, $this->domains_with_translations, true ) &&
str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" )
str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )
) {
$this->domains_with_translations[] = $domain;
}
if ( $mo_path === $path ) {
if ( $file_path === $mo_path || $file_path === $php_path ) {
$found_location = rtrim( $location, '/' ) . '/';
}
}

View File

@ -789,10 +789,6 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
*/
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
if ( ! is_readable( $mofile ) ) {
return false;
}
if ( ! $locale ) {
$locale = determine_locale();
}
@ -817,14 +813,14 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
$preferred_format = 'php';
}
$translation_files = array( $mofile );
$translation_files = array();
if ( 'mo' !== $preferred_format ) {
array_unshift(
$translation_files,
substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) )
);
$translation_files[] = substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) );
}
$translation_files[] = $mofile;
foreach ( $translation_files as $file ) {
/**
* Filters the file path for loading translations for the given text domain.
@ -857,7 +853,7 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
}
}
return true;
return false;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57515';
$wp_version = '6.5-alpha-57516';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.