WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/21/2016 04:06:38 PM (4 years ago)
Author:
ocean90
Message:

I18N: Add an additional caching layer for _load_textdomain_just_in_time().

Previously, if no translation files exist for a text domain, _load_textdomain_just_in_time() went through the entire process each time it was called. This results in an increased call to get_locale() and its locale filter.
This change splits the logic into _get_path_to_translation() and _get_path_to_translation_from_lang_dir(). The former, which is used by _load_textdomain_just_in_time(), caches the result of the latter. It also removes some non-working code from WP_Locale_Switcher::load_translations().

Props jrf, swissspidy, sharkomatic, ocean90.
Fixes #37997.

File:
1 edited

Legend:

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

    r39244 r39330  
    828828 * to call load_plugin_texdomain() or load_theme_texdomain().
    829829 *
    830  * Holds a cached list of available .mo files to improve performance.
    831  *
    832830 * @since 4.6.0
    833831 * @access private
     
    843841
    844842    $l10n_unloaded = (array) $l10n_unloaded;
    845 
    846     static $cached_mofiles = null;
    847843
    848844    // Short-circuit if domain is 'default' which is reserved for core.
     
    850846        return false;
    851847    }
     848
     849    $translation_path = _get_path_to_translation( $domain );
     850    if ( false === $translation_path ) {
     851        return false;
     852    }
     853
     854    return load_textdomain( $domain, $translation_path );
     855}
     856
     857/**
     858 * Gets the path to a translation file for loading a textdomain just in time.
     859 *
     860 * Caches the retrieved results internally.
     861 *
     862 * @since 4.7.0
     863 * @access private
     864 *
     865 * @see _load_textdomain_just_in_time()
     866 *
     867 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     868 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     869 * @return string|false The path to the translation file or false if no translation file was found.
     870 */
     871function _get_path_to_translation( $domain, $reset = false ) {
     872    static $available_translations = array();
     873
     874    if ( true === $reset ) {
     875        $available_translations = array();
     876    }
     877
     878    if ( ! isset( $available_translations[ $domain ] ) ) {
     879        $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
     880    }
     881
     882    return $available_translations[ $domain ];
     883}
     884
     885/**
     886 * Gets the path to a translation file in the languages directory for the current locale.
     887 *
     888 * Holds a cached list of available .mo files to improve performance.
     889 *
     890 * @since 4.7.0
     891 * @access private
     892 *
     893 * @see _get_path_to_translation()
     894 *
     895 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     896 * @return string|false The path to the translation file or false if no translation file was found.
     897 */
     898function _get_path_to_translation_from_lang_dir( $domain ) {
     899    static $cached_mofiles = null;
    852900
    853901    if ( null === $cached_mofiles ) {
     
    870918    $mofile = "{$domain}-{$locale}.mo";
    871919
    872     if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
    873         return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
    874     }
    875 
    876     if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
    877         return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
     920    $path = WP_LANG_DIR . '/plugins/' . $mofile;
     921    if ( in_array( $path, $cached_mofiles ) ) {
     922        return $path;
     923    }
     924
     925    $path = WP_LANG_DIR . '/themes/' . $mofile;
     926    if ( in_array( $path, $cached_mofiles ) ) {
     927        return $path;
    878928    }
    879929
Note: See TracChangeset for help on using the changeset viewer.