Make WordPress Core


Ignore:
Timestamp:
02/01/2024 07:03:55 PM (3 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-textdomain-registry.php

    r57344 r57516  
    151151
    152152        /**
    153          * Retrieves .mo files from the specified path.
     153         * Retrieves translation files from the specified path.
    154154         *
    155155         * Allows early retrieval through the {@see 'pre_get_mo_files_from_path'} filter to optimize
    … …  
    158158         * @since 6.5.0
    159159         *
    160          * @param string $path The directory path to search for .mo files.
    161          * @return array Array of .mo file paths.
     160         * @param string $path The directory path to search for translation files.
     161         * @return array Array of translation file paths. Can contain .mo and .l10n.php files.
    162162         */
    163163        public function get_language_files_from_path( $path ) {
    … …  
    165165
    166166                /**
    167                  * Filters the .mo files retrieved from a specified path before the actual lookup.
     167                 * Filters the translation files retrieved from a specified path before the actual lookup.
    168168                 *
    169169                 * Returning a non-null value from the filter will effectively short-circuit
    … …  
    175175                 * @since 6.5.0
    176176                 *
    177                  * @param null|array $mo_files List of .mo files. Default null.
    178                  * @param string $path The path from which .mo files are being fetched.
     177                 * @param null|array $files List of translation files. Default null.
     178                 * @param string $path The path from which translation files are being fetched.
    179179                 **/
    180                 $mo_files = apply_filters( 'pre_get_language_files_from_path', null, $path );
    181 
    182                 if ( null !== $mo_files ) {
    183                         return $mo_files;
     180                $files = apply_filters( 'pre_get_language_files_from_path', null, $path );
     181
     182                if ( null !== $files ) {
     183                        return $files;
    184184                }
    185185
    186186                $cache_key = 'cached_mo_files_' . md5( $path );
    187                 $mo_files  = wp_cache_get( $cache_key, 'translations' );
    188 
    189                 if ( false === $mo_files ) {
    190                         $mo_files = glob( $path . '*.mo' );
    191                         if ( false === $mo_files ) {
    192                                 $mo_files = array();
     187                $files     = wp_cache_get( $cache_key, 'translations' );
     188
     189                if ( false === $files ) {
     190                        $files = glob( $path . '*.mo' );
     191                        if ( false === $files ) {
     192                                $files = array();
    193193                        }
    194                         wp_cache_set( $cache_key, $mo_files, 'translations' );
    195                 }
    196 
    197                 return $mo_files;
     194
     195                        $php_files = glob( $path . '*.l10n.php' );
     196                        if ( is_array( $php_files ) ) {
     197                                $files = array_merge( $files, $php_files );
     198                        }
     199
     200                        wp_cache_set( $cache_key, $files, 'translations' );
     201                }
     202
     203                return $files;
    198204        }
    199205
    … …  
    296302                        $files = $this->get_language_files_from_path( $location );
    297303
    298                         $path = "$location/$domain-$locale.mo";
    299 
    300                         foreach ( $files as $mo_path ) {
     304                        $mo_path  = "$location/$domain-$locale.mo";
     305                        $php_path = "$location/$domain-$locale.l10n.php";
     306
     307                        foreach ( $files as $file_path ) {
    301308                                if (
    302309                                        ! in_array( $domain, $this->domains_with_translations, true ) &&
    303                                         str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" )
     310                                        str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )
    304311                                ) {
    305312                                        $this->domains_with_translations[] = $domain;
    306313                                }
    307314
    308                                 if ( $mo_path === $path ) {
     315                                if ( $file_path === $mo_path || $file_path === $php_path ) {
    309316                                        $found_location = rtrim( $location, '/' ) . '/';
    310317                                }
Note: See TracChangeset for help on using the changeset viewer.