From 71ae6e2787f642dfa1bee1fb04c8dee01013ab86 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 16 Feb 2024 10:28:12 +0000 Subject: [PATCH] I18N: Prevent incorrect language dropdown entries when there are `.l10n.php` files. In [57516], the just-in-time translation loading logic was enhanced to support cases where only `.l10n.php` translation exist but no `.mo` or `.po` files. This caused a slight regression in `get_available_languages()`, which uses the list of files to populate the language dropdown list on the settings page. To address this, the new file extension is now properly stripped off, and the resulting file list is de-duplicated. New test files are added to allow the existing tests to cover this new scenario. See #59656. Fixes #60553. Built from https://develop.svn.wordpress.org/trunk@57639 git-svn-id: http://core.svn.wordpress.org/trunk@57140 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-textdomain-registry.php | 1 + wp-includes/l10n.php | 9 ++++++--- wp-includes/version.php | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/wp-includes/class-wp-textdomain-registry.php b/wp-includes/class-wp-textdomain-registry.php index b2cbd72fa5..113ef3bd65 100644 --- a/wp-includes/class-wp-textdomain-registry.php +++ b/wp-includes/class-wp-textdomain-registry.php @@ -314,6 +314,7 @@ class WP_Textdomain_Registry { if ( $file_path === $mo_path || $file_path === $php_path ) { $found_location = rtrim( $location, '/' ) . '/'; + break 2; } } } diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 8191b88cee..38c30b84e8 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -1435,19 +1435,20 @@ function translate_user_role( $name, $domain = 'default' ) { } /** - * Gets all available languages based on the presence of *.mo files in a given directory. + * Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory. * * The default directory is WP_LANG_DIR. * * @since 3.0.0 * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter. + * @since 6.5.0 The initial file list is now cached and also takes into account *.l10n.php files. * * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. * * @param string $dir A directory to search for language files. * Default WP_LANG_DIR. * @return string[] An array of language codes or an empty array if no languages are present. - * Language codes are formed by stripping the .mo extension from the language file names. + * Language codes are formed by stripping the file extension from the language file names. */ function get_available_languages( $dir = null ) { global $wp_textdomain_registry; @@ -1460,6 +1461,8 @@ function get_available_languages( $dir = null ) { if ( $lang_files ) { foreach ( $lang_files as $lang_file ) { $lang_file = basename( $lang_file, '.mo' ); + $lang_file = basename( $lang_file, '.l10n.php' ); + if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) && ! str_starts_with( $lang_file, 'admin-' ) ) { $languages[] = $lang_file; @@ -1475,7 +1478,7 @@ function get_available_languages( $dir = null ) { * @param string[] $languages An array of available language codes. * @param string $dir The directory where the language files were found. */ - return apply_filters( 'get_available_languages', $languages, $dir ); + return apply_filters( 'get_available_languages', array_unique( $languages ), $dir ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 18981e3495..3ee45a5b55 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-beta1-57638'; +$wp_version = '6.5-beta1-57639'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.