Make WordPress Core


Ignore:
Timestamp:
01/23/2024 01:32:34 PM (17 months ago)
Author:
swissspidy
Message:

I18N: Introduce a more performant localization library.

This introduces a more lightweight library for loading .mo translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.

For plugins interacting with the $l10n global variable in core, a shim is added to retain backward compatibility with the existing pomo library.

In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an .mo translation file has a corresponding .l10n.php file, the latter will be loaded instead.
This behavior can be adjusted using the new translation_file_format and load_translation_file filters.

PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.

Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.

File:
1 edited

Legend:

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

    r56549 r57337  
    421421}
    422422
     423if ( ! function_exists( 'array_is_list' ) ) {
     424    /**
     425     * Polyfill for `array_is_list()` function added in PHP 8.1.
     426     *
     427     * Determines if the given array is a list.
     428     *
     429     * An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
     430     *
     431     * @see https://github.com/symfony/polyfill-php81/tree/main
     432     *
     433     * @since 6.5.0
     434     *
     435     * @param array<mixed> $arr The array being evaluated.
     436     * @return bool True if array is a list, false otherwise.
     437     */
     438    function array_is_list( $arr ) {
     439        if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
     440            return true;
     441        }
     442
     443        $next_key = -1;
     444
     445        foreach ( $arr as $k => $v ) {
     446            if ( ++$next_key !== $k ) {
     447                return false;
     448            }
     449        }
     450
     451        return true;
     452    }
     453}
     454
    423455if ( ! function_exists( 'str_contains' ) ) {
    424456    /**
Note: See TracChangeset for help on using the changeset viewer.