Make WordPress Core


Ignore:
Timestamp:
04/30/2024 08:37:25 AM (7 months ago)
Author:
swissspidy
Message:

I18N: Actually add all the files for [58061], not just the test fixtures.

Improve support for using only PHP translation files.

This builds on top of the PHP translation file support added in WordPress 6.5, improving the behavior for projects using solely .l10n.php translation files and no .mo. and .po files.

Updates wp_get_installed_translations(), which is used when updating language packs and when uninstalling plugins/themes (to remove the translations again), to look for PHP translation files and read metadata from them. Additionally, the file lookup is now cached thanks to using WP_Textdomain_Registry.

Updates Language_Pack_Upgrader::check_package() to allow language packs that only contain PHP translation files. While WordPress.org continues to serve .mo and .po files, third-party services might want to only use the PHP file format.

See #60554.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r57925 r58062  
    15061506 * @since 3.7.0
    15071507 *
     1508 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
     1509 *
    15081510 * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
    15091511 * @return array Array of language data.
    15101512 */
    15111513function wp_get_installed_translations( $type ) {
     1514    global $wp_textdomain_registry;
     1515
    15121516    if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
    15131517        return array();
    15141518    }
    15151519
    1516     $dir = 'core' === $type ? '' : "/$type";
    1517 
    1518     if ( ! is_dir( WP_LANG_DIR ) ) {
     1520    $dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . "/$type";
     1521
     1522    if ( ! is_dir( $dir ) ) {
    15191523        return array();
    15201524    }
    15211525
    1522     if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) {
    1523         return array();
    1524     }
    1525 
    1526     $files = scandir( WP_LANG_DIR . $dir );
     1526    $files = $wp_textdomain_registry->get_language_files_from_path( $dir );
    15271527    if ( ! $files ) {
    15281528        return array();
     
    15321532
    15331533    foreach ( $files as $file ) {
    1534         if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) {
    1535             continue;
    1536         }
    1537         if ( ! str_ends_with( $file, '.po' ) ) {
    1538             continue;
    1539         }
    1540         if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
    1541             continue;
    1542         }
    1543         if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files, true ) ) {
     1534        if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/', basename( $file ), $match ) ) {
    15441535            continue;
    15451536        }
     
    15491540            $textdomain = 'default';
    15501541        }
    1551         $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
     1542
     1543        if ( str_ends_with( $file, '.mo' ) ) {
     1544            $pofile = substr_replace( $file, '.po', - strlen( '.mo' ) );
     1545
     1546            if ( ! file_exists( $pofile ) ) {
     1547                continue;
     1548            }
     1549
     1550            $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( $pofile );
     1551        } else {
     1552            $pofile = substr_replace( $file, '.po', - strlen( '.l10n.php' ) );
     1553
     1554            // If both a PO and a PHP file exist, prefer the PO file.
     1555            if ( file_exists( $pofile ) ) {
     1556                continue;
     1557            }
     1558
     1559            $language_data[ $textdomain ][ $language ] = wp_get_l10n_php_file_data( $file );
     1560        }
    15521561    }
    15531562    return $language_data;
     
    15771586    }
    15781587    return $headers;
     1588}
     1589
     1590/**
     1591 * Extracts headers from a PHP translation file.
     1592 *
     1593 * @since 6.6.0
     1594 *
     1595 * @param string $php_file Path to a `.l10n.php` file.
     1596 * @return string[] Array of file header values keyed by header name.
     1597 */
     1598function wp_get_l10n_php_file_data( $php_file ) {
     1599    $data = (array) include $php_file;
     1600
     1601    unset( $data['messages'] );
     1602    $headers = array(
     1603        'POT-Creation-Date'  => 'pot-creation-date',
     1604        'PO-Revision-Date'   => 'po-revision-date',
     1605        'Project-Id-Version' => 'project-id-version',
     1606        'X-Generator'        => 'x-generator',
     1607    );
     1608
     1609    $result = array(
     1610        'POT-Creation-Date'  => '',
     1611        'PO-Revision-Date'   => '',
     1612        'Project-Id-Version' => '',
     1613        'X-Generator'        => '',
     1614    );
     1615
     1616    foreach ( $headers as $po_header => $php_header ) {
     1617        if ( isset( $data[ $php_header ] ) ) {
     1618            $result[ $po_header ] = $data[ $php_header ];
     1619        }
     1620    }
     1621
     1622    return $result;
    15791623}
    15801624
Note: See TracChangeset for help on using the changeset viewer.