diff options
Diffstat (limited to 'wp-includes/l10n.php')
-rw-r--r-- | wp-includes/l10n.php | 86 |
1 files changed, 66 insertions, 20 deletions
diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 9a8ff84..038b916 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -833,11 +833,13 @@ function load_textdomain( $domain, $mofile, $locale = null ) { * the file path could be for an MO or PHP file. * * @since 6.5.0 + * @since 6.6.0 Added the `$locale` parameter. * * @param string $file Path to the translation file to load. * @param string $domain The text domain. + * @param string $locale The locale. */ - $file = (string) apply_filters( 'load_translation_file', $file, $domain ); + $file = (string) apply_filters( 'load_translation_file', $file, $domain, $locale ); $success = $i18n_controller->load_file( $file, $domain, $locale ); @@ -1205,7 +1207,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) { $relative = trim( $relative, '/' ); $relative = explode( '/', $relative ); - $languages_path = WP_LANG_DIR . '/' . $relative[0]; + $languages_path = WP_LANG_DIR . '/plugins'; $relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>. $relative = implode( '/', $relative ); @@ -1505,25 +1507,25 @@ function get_available_languages( $dir = null ) { * * @since 3.7.0 * + * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. + * * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'. * @return array Array of language data. */ function wp_get_installed_translations( $type ) { + global $wp_textdomain_registry; + if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) { return array(); } - $dir = 'core' === $type ? '' : "/$type"; + $dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . "/$type"; - if ( ! is_dir( WP_LANG_DIR ) ) { + if ( ! is_dir( $dir ) ) { return array(); } - if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) { - return array(); - } - - $files = scandir( WP_LANG_DIR . $dir ); + $files = $wp_textdomain_registry->get_language_files_from_path( $dir ); if ( ! $files ) { return array(); } @@ -1531,16 +1533,7 @@ function wp_get_installed_translations( $type ) { $language_data = array(); foreach ( $files as $file ) { - if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) { - continue; - } - if ( ! str_ends_with( $file, '.po' ) ) { - continue; - } - if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) { - continue; - } - if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files, true ) ) { + if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/', basename( $file ), $match ) ) { continue; } @@ -1548,7 +1541,25 @@ function wp_get_installed_translations( $type ) { if ( '' === $textdomain ) { $textdomain = 'default'; } - $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" ); + + if ( str_ends_with( $file, '.mo' ) ) { + $pofile = substr_replace( $file, '.po', - strlen( '.mo' ) ); + + if ( ! file_exists( $pofile ) ) { + continue; + } + + $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( $pofile ); + } else { + $pofile = substr_replace( $file, '.po', - strlen( '.l10n.php' ) ); + + // If both a PO and a PHP file exist, prefer the PO file. + if ( file_exists( $pofile ) ) { + continue; + } + + $language_data[ $textdomain ][ $language ] = wp_get_l10n_php_file_data( $file ); + } } return $language_data; } @@ -1579,6 +1590,41 @@ function wp_get_pomo_file_data( $po_file ) { } /** + * Extracts headers from a PHP translation file. + * + * @since 6.6.0 + * + * @param string $php_file Path to a `.l10n.php` file. + * @return string[] Array of file header values keyed by header name. + */ +function wp_get_l10n_php_file_data( $php_file ) { + $data = (array) include $php_file; + + unset( $data['messages'] ); + $headers = array( + 'POT-Creation-Date' => 'pot-creation-date', + 'PO-Revision-Date' => 'po-revision-date', + 'Project-Id-Version' => 'project-id-version', + 'X-Generator' => 'x-generator', + ); + + $result = array( + 'POT-Creation-Date' => '', + 'PO-Revision-Date' => '', + 'Project-Id-Version' => '', + 'X-Generator' => '', + ); + + foreach ( $headers as $po_header => $php_header ) { + if ( isset( $data[ $php_header ] ) ) { + $result[ $po_header ] = $data[ $php_header ]; + } + } + + return $result; +} + +/** * Displays or returns a Language selector. * * @since 4.0.0 |