From 39f91adcd53c761137ae0cfbdba2a29b4a947187 Mon Sep 17 00:00:00 2001
Date: Sat, 12 Nov 2016 01:55:57 +0100
Subject: [PATCH] Proof of concept for improving the performance of the
`_load_textdomain_just_in_time()` function.
---
src/wp-includes/class-wp-locale-switcher.php | 3 ++
src/wp-includes/l10n.php | 67 +++++++++++++++++++++++-----
2 files changed, 60 insertions(+), 10 deletions(-)
diff --git a/src/wp-includes/class-wp-locale-switcher.php b/src/wp-includes/class-wp-locale-switcher.php
index 3d288c8..4ccffab 100644
|
a
|
b
|
class WP_Locale_Switcher {
|
| 228 | 228 | * @param string $locale The locale to change to. |
| 229 | 229 | */ |
| 230 | 230 | private function change_locale( $locale ) { |
| | 231 | // Reset translation availability information. |
| | 232 | _get_path_to_translation( null, true ); |
| | 233 | |
| 231 | 234 | $this->load_translations( $locale ); |
| 232 | 235 | |
| 233 | 236 | $GLOBALS['wp_locale'] = new WP_Locale(); |
diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
index 39346c4..cbd7985 100644
|
a
|
b
|
function load_child_theme_textdomain( $domain, $path = false ) {
|
| 827 | 827 | * the translation file from `wp-content/languages`, removing the need |
| 828 | 828 | * to call load_plugin_texdomain() or load_theme_texdomain(). |
| 829 | 829 | * |
| 830 | | * Holds a cached list of available .mo files to improve performance. |
| 831 | | * |
| 832 | 830 | * @since 4.6.0 |
| 833 | 831 | * @access private |
| 834 | 832 | * |
| … |
… |
function load_child_theme_textdomain( $domain, $path = false ) {
|
| 841 | 839 | function _load_textdomain_just_in_time( $domain ) { |
| 842 | 840 | global $l10n_unloaded; |
| 843 | 841 | |
| 844 | | $l10n_unloaded = (array) $l10n_unloaded; |
| 845 | | |
| 846 | | static $cached_mofiles = null; |
| | 842 | $l10n_unloaded = (array) $l10n_unloaded; |
| | 843 | $translation_path = _get_path_to_translation( $domain ); |
| 847 | 844 | |
| 848 | 845 | // Short-circuit if domain is 'default' which is reserved for core. |
| 849 | | if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) { |
| | 846 | if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) || false === $translation_path ) { |
| 850 | 847 | return false; |
| 851 | 848 | } |
| 852 | 849 | |
| | 850 | return load_textdomain( $domain, $translation_path ); |
| | 851 | } |
| | 852 | |
| | 853 | /** |
| | 854 | * Get the path to a translation file in the `wp-content/languages` directory. |
| | 855 | * |
| | 856 | * Caches the retrieved results internally. |
| | 857 | * |
| | 858 | * @since 4.7.0 |
| | 859 | * @access private |
| | 860 | * |
| | 861 | * @see _load_textdomain_just_in_time() |
| | 862 | * |
| | 863 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| | 864 | * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. |
| | 865 | * @return string|false The path to the translation file or false if no translation file was found. |
| | 866 | */ |
| | 867 | function _get_path_to_translation( $domain, $reset = false ) { |
| | 868 | static $available_translations = array(); |
| | 869 | |
| | 870 | if ( true === $reset ) { |
| | 871 | $available_translations = array(); |
| | 872 | } |
| | 873 | |
| | 874 | if ( ! isset( $available_translations[ $domain ] ) ) { |
| | 875 | $available_translations[ $domain ] = _get_path_to_glotpress_translation( $domain ); |
| | 876 | } |
| | 877 | |
| | 878 | return $available_translations[ $domain ]; |
| | 879 | } |
| | 880 | |
| | 881 | /** |
| | 882 | * Get the path to a translation file in the languages directory for loading |
| | 883 | * a textdomain just in time. |
| | 884 | * |
| | 885 | * Holds a cached list of available .mo files to improve performance. |
| | 886 | * |
| | 887 | * @since 4.7.0 |
| | 888 | * @access private |
| | 889 | * |
| | 890 | * @see _get_path_to_translation() |
| | 891 | * |
| | 892 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| | 893 | * @return string|false The path to the translation file or false if no translation file was found. |
| | 894 | */ |
| | 895 | function _get_path_to_glotpress_translation( $domain ) { |
| | 896 | static $cached_mofiles = null; |
| | 897 | |
| 853 | 898 | if ( null === $cached_mofiles ) { |
| 854 | 899 | $cached_mofiles = array(); |
| 855 | 900 | |
| … |
… |
function _load_textdomain_just_in_time( $domain ) {
|
| 868 | 913 | $locale = is_admin() ? get_user_locale() : get_locale(); |
| 869 | 914 | $mofile = "{$domain}-{$locale}.mo"; |
| 870 | 915 | |
| 871 | | if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) { |
| 872 | | return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ); |
| | 916 | $path = WP_LANG_DIR . '/plugins/' . $mofile; |
| | 917 | if ( in_array( $path, $cached_mofiles ) ) { |
| | 918 | return $path; |
| 873 | 919 | } |
| 874 | 920 | |
| 875 | | if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) { |
| 876 | | return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ); |
| | 921 | $path = WP_LANG_DIR . '/themes/' . $mofile; |
| | 922 | if ( in_array( $path, $cached_mofiles ) ) { |
| | 923 | return $path; |
| 877 | 924 | } |
| 878 | 925 | |
| 879 | 926 | return false; |