Changeset 53874 for trunk/src/wp-includes/l10n.php
- Timestamp:
- 08/11/2022 12:37:05 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/l10n.php
r53702 r53874 705 705 * 706 706 * @since 1.5.0 707 * 708 * @global MO[] $l10n An array of all currently loaded text domains. 709 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. 707 * @since 6.1.0 Added the `$locale` parameter. 708 * 709 * @global MO[] $l10n An array of all currently loaded text domains. 710 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. 711 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 710 712 * 711 713 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 712 714 * @param string $mofile Path to the .mo file. 715 * @param string $locale Optional. Locale. Default is the current locale. 713 716 * @return bool True on success, false on failure. 714 717 */ 715 function load_textdomain( $domain, $mofile ) { 716 global $l10n, $l10n_unloaded; 718 function load_textdomain( $domain, $mofile, $locale = null ) { 719 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 720 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 717 721 718 722 $l10n_unloaded = (array) $l10n_unloaded; … … 759 763 } 760 764 765 if ( ! $locale ) { 766 $locale = determine_locale(); 767 } 768 761 769 $mo = new MO(); 762 770 if ( ! $mo->import_from_file( $mofile ) ) { 771 $wp_textdomain_registry->set( $domain, $locale, false ); 772 763 773 return false; 764 774 } … … 772 782 $l10n[ $domain ] = &$mo; 773 783 784 $wp_textdomain_registry->set( $domain, $locale, dirname( $mofile ) ); 785 774 786 return true; 775 787 } … … 779 791 * 780 792 * @since 3.0.0 793 * @since 6.1.0 Added the `$reloadable` parameter. 781 794 * 782 795 * @global MO[] $l10n An array of all currently loaded text domains. 783 796 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. 784 797 * 785 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 798 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 799 * @param bool $reloadable Whether the text domain can be loaded just-in-time again. 786 800 * @return bool Whether textdomain was unloaded. 787 801 */ 788 function unload_textdomain( $domain ) {802 function unload_textdomain( $domain, $reloadable = false ) { 789 803 global $l10n, $l10n_unloaded; 790 804 … … 795 809 * 796 810 * @since 3.0.0 797 * 798 * @param bool $override Whether to override the text domain unloading. Default false. 799 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 800 */ 801 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); 811 * @since 6.1.0 Added the `$reloadable` parameter. 812 * 813 * @param bool $override Whether to override the text domain unloading. Default false. 814 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 815 * @param bool $reloadable Whether the text domain can be loaded just-in-time again. 816 */ 817 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain, $reloadable ); 802 818 803 819 if ( $plugin_override ) { 804 $l10n_unloaded[ $domain ] = true; 820 if ( ! $reloadable ) { 821 $l10n_unloaded[ $domain ] = true; 822 } 805 823 806 824 return true; … … 811 829 * 812 830 * @since 3.0.0 813 * 814 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 815 */ 816 do_action( 'unload_textdomain', $domain ); 831 * @since 6.1.0 Added the `$reloadable` parameter. 832 * 833 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 834 * @param bool $reloadable Whether the text domain can be loaded just-in-time again. 835 */ 836 do_action( 'unload_textdomain', $domain, $reloadable ); 817 837 818 838 if ( isset( $l10n[ $domain ] ) ) { 819 839 unset( $l10n[ $domain ] ); 820 840 821 $l10n_unloaded[ $domain ] = true; 841 if ( ! $reloadable ) { 842 $l10n_unloaded[ $domain ] = true; 843 } 822 844 823 845 return true; … … 848 870 unload_textdomain( 'default' ); 849 871 850 $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );872 $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo", $locale ); 851 873 852 874 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { 853 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );875 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo", $locale ); 854 876 return $return; 855 877 } 856 878 857 879 if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { 858 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );880 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale ); 859 881 } 860 882 861 883 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) { 862 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );884 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo", $locale ); 863 885 } 864 886 … … 884 906 */ 885 907 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { 908 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 909 global $wp_textdomain_registry; 910 886 911 /** 887 912 * Filters a plugin's locale. … … 897 922 898 923 // Try to load from the languages directory first. 899 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {924 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) { 900 925 return true; 901 926 } … … 910 935 } 911 936 912 return load_textdomain( $domain, $path . '/' . $mofile ); 937 $wp_textdomain_registry->set( $domain, $locale, $path ); 938 939 return load_textdomain( $domain, $path . '/' . $mofile, $locale ); 913 940 } 914 941 … … 918 945 * @since 3.0.0 919 946 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. 947 * 948 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 920 949 * 921 950 * @param string $domain Text domain. Unique identifier for retrieving translated strings. … … 925 954 */ 926 955 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 956 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 957 global $wp_textdomain_registry; 958 927 959 /** This filter is documented in wp-includes/l10n.php */ 928 960 $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); … … 931 963 932 964 // Try to load from the languages directory first. 933 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {965 if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) { 934 966 return true; 935 967 } … … 937 969 $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); 938 970 939 return load_textdomain( $domain, $path . '/' . $mofile ); 971 $wp_textdomain_registry->set( $domain, $locale, $path ); 972 973 return load_textdomain( $domain, $path . '/' . $mofile, $locale ); 940 974 } 941 975 … … 950 984 * @since 1.5.0 951 985 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. 986 * 987 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 952 988 * 953 989 * @param string $domain Text domain. Unique identifier for retrieving translated strings. … … 957 993 */ 958 994 function load_theme_textdomain( $domain, $path = false ) { 995 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 996 global $wp_textdomain_registry; 997 959 998 /** 960 999 * Filters a theme's locale. … … 970 1009 971 1010 // Try to load from the languages directory first. 972 if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) {1011 if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) { 973 1012 return true; 974 1013 } … … 978 1017 } 979 1018 980 return load_textdomain( $domain, $path . '/' . $locale . '.mo' ); 1019 $wp_textdomain_registry->set( $domain, $locale, $path ); 1020 1021 return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale ); 981 1022 } 982 1023 … … 1199 1240 1200 1241 /** 1201 * Loads plugin and theme text domains just-in-time.1242 * Loads plugin and theme text domains just-in-time. 1202 1243 * 1203 1244 * When a textdomain is encountered for the first time, we try to load … … 1208 1249 * @access private 1209 1250 * 1210 * @ see get_translations_for_domain()1211 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.1251 * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. 1252 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 1212 1253 * 1213 1254 * @param string $domain Text domain. Unique identifier for retrieving translated strings. … … 1215 1256 */ 1216 1257 function _load_textdomain_just_in_time( $domain ) { 1217 global $l10n_unloaded; 1258 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 1259 global $l10n_unloaded, $wp_textdomain_registry; 1218 1260 1219 1261 $l10n_unloaded = (array) $l10n_unloaded; … … 1224 1266 } 1225 1267 1226 $translation_path = _get_path_to_translation( $domain ); 1227 if ( false === $translation_path ) { 1268 if ( $wp_textdomain_registry->has( $domain ) && ! $wp_textdomain_registry->get_current( $domain ) ) { 1228 1269 return false; 1229 1270 } 1230 1271 1231 return load_textdomain( $domain, $translation_path );1232 }1233 1234 /**1235 * Gets the path to a translation file for loading a textdomain just in time.1236 *1237 * Caches the retrieved results internally.1238 *1239 * @since 4.7.01240 * @access private1241 *1242 * @see _load_textdomain_just_in_time()1243 *1244 * @param string $domain Text domain. Unique identifier for retrieving translated strings.1245 * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality.1246 * @return string|false The path to the translation file or false if no translation file was found.1247 */1248 function _get_path_to_translation( $domain, $reset = false ) {1249 static $available_translations = array();1250 1251 if ( true === $reset ) {1252 $available_translations = array();1253 }1254 1255 if ( ! isset( $available_translations[ $domain ] ) ) {1256 $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );1257 }1258 1259 return $available_translations[ $domain ];1260 }1261 1262 /**1263 * Gets the path to a translation file in the languages directory for the current locale.1264 *1265 * Holds a cached list of available .mo files to improve performance.1266 *1267 * @since 4.7.01268 * @access private1269 *1270 * @see _get_path_to_translation()1271 *1272 * @param string $domain Text domain. Unique identifier for retrieving translated strings.1273 * @return string|false The path to the translation file or false if no translation file was found.1274 */1275 function _get_path_to_translation_from_lang_dir( $domain ) {1276 static $cached_mofiles = null;1277 1278 if ( null === $cached_mofiles ) {1279 $cached_mofiles = array();1280 1281 $locations = array(1282 WP_LANG_DIR . '/plugins',1283 WP_LANG_DIR . '/themes',1284 );1285 1286 foreach ( $locations as $location ) {1287 $mofiles = glob( $location . '/*.mo' );1288 if ( $mofiles ) {1289 $cached_mofiles = array_merge( $cached_mofiles, $mofiles );1290 }1291 }1292 }1293 1294 1272 $locale = determine_locale(); 1295 $mofile = "{$domain}-{$locale}.mo"; 1296 1297 $path = WP_LANG_DIR . '/plugins/' . $mofile; 1298 if ( in_array( $path, $cached_mofiles, true ) ) { 1299 return $path; 1300 } 1301 1302 $path = WP_LANG_DIR . '/themes/' . $mofile; 1303 if ( in_array( $path, $cached_mofiles, true ) ) { 1304 return $path; 1305 } 1306 1307 return false; 1273 $path = $wp_textdomain_registry->get( $domain, $locale ); 1274 if ( ! $path ) { 1275 return false; 1276 } 1277 // Themes with their language directory outside of WP_LANG_DIR have a different file name. 1278 $template_directory = trailingslashit( get_template_directory() ); 1279 $stylesheet_directory = trailingslashit( get_stylesheet_directory() ); 1280 if ( str_starts_with( $path, $template_directory ) || str_starts_with( $path, $stylesheet_directory ) ) { 1281 $mofile = "{$path}{$locale}.mo"; 1282 } else { 1283 $mofile = "{$path}{$domain}-{$locale}.mo"; 1284 } 1285 1286 return load_textdomain( $domain, $mofile, $locale ); 1308 1287 } 1309 1288 … … 1315 1294 * @since 2.8.0 1316 1295 * 1317 * @global MO[] $l10n 1296 * @global MO[] $l10n An array of all currently loaded text domains. 1318 1297 * 1319 1298 * @param string $domain Text domain. Unique identifier for retrieving translated strings. … … 1339 1318 * @since 3.0.0 1340 1319 * 1341 * @global MO[] $l10n 1320 * @global MO[] $l10n An array of all currently loaded text domains. 1342 1321 * 1343 1322 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
Note: See TracChangeset
for help on using the changeset viewer.