Ticket #41498: patch.diff
File patch.diff, 9.4 KB (added by , 6 years ago) |
---|
-
wp-includes/l10n.php
138 138 $determined_locale = get_user_locale(); 139 139 } 140 140 141 if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) &&'wp-login.php' === $GLOBALS['pagenow'] ) {141 if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { 142 142 $determined_locale = sanitize_text_field( $_GET['wp_lang'] ); 143 143 } 144 144 … … 166 166 * Default 'default'. 167 167 * @return string Translated text 168 168 */ 169 function translate( $text, $domain = 'default' ) { 170 $translations = get_translations_for_domain( $domain ); 171 $translation = $translations->translate( $text ); 172 173 /** 174 * Filters text with its translation. 175 * 176 * @since 2.0.11 177 * 178 * @param string $translation Translated text. 179 * @param string $text Text to translate. 180 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 181 */ 182 return apply_filters( 'gettext', $translation, $text, $domain ); 169 if (! function_exists('translate')) { 170 function translate( $text, $domain = 'default' ) { 171 $translations = get_translations_for_domain( $domain ); 172 $translation = $translations->translate( $text ); 173 174 /** 175 * Filters text with its translation. 176 * 177 * @since 2.0.11 178 * 179 * @param string $translation Translated text. 180 * @param string $text Text to translate. 181 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 182 */ 183 return apply_filters( 'gettext', $translation, $text, $domain ); 184 } 183 185 } 184 186 185 187 /** … … 246 248 * Default 'default'. 247 249 * @return string Translated text. 248 250 */ 249 function __( $text, $domain = 'default' ) { 250 return translate( $text, $domain ); 251 if (! function_exists('__')) { 252 function __( $text, $domain = 'default' ) { 253 return translate( $text, $domain ); 254 } 251 255 } 252 256 253 257 /** … … 870 874 } 871 875 872 876 /** 873 * Loads the script translated strings. 874 * 875 * @since 5.0.0 876 * @since 5.0.2 Uses load_script_translations() to load translation data. 877 * Load the script translated strings. 877 878 * 878 879 * @see WP_Scripts::set_translations() 880 * @link https://core.trac.wordpress.org/ticket/45103 881 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 882 * 883 * @since 5.0.0 879 884 * 880 885 * @param string $handle Name of the script to register a translation domain to. 881 * @param string $domain The text 886 * @param string $domain The textdomain. 882 887 * @param string $path Optional. The full file path to the directory containing translation files. 883 888 * 884 889 * @return false|string False if the script textdomain could not be loaded, the translated strings 885 890 * in JSON encoding otherwise. 886 891 */ 887 892 function load_script_textdomain( $handle, $domain, $path = null ) { 888 $wp_scripts = wp_scripts(); 889 890 if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { 891 return false; 892 } 893 global $wp_scripts; 893 894 894 895 $path = untrailingslashit( $path ); 895 896 $locale = determine_locale(); … … 897 898 // If a path was given and the handle file exists simply return it. 898 899 $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; 899 900 $handle_filename = $file_base . '-' . $handle . '.json'; 900 901 if ( $path ) { 902 $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain ); 903 904 if ( $translations ) { 905 return $translations; 906 } 901 if ( $path && file_exists( $path . '/' . $handle_filename ) ) { 902 return file_get_contents( $path . '/' . $handle_filename ); 907 903 } 908 904 909 $ src = $wp_scripts->registered[ $handle ]->src;905 $obj = $wp_scripts->registered[ $handle ]; 910 906 911 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) { 912 $src = $wp_scripts->base_url . $src; 913 } 907 /** This filter is documented in wp-includes/class.wp-scripts.php */ 908 $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) ); 914 909 915 910 $relative = false; 916 911 $languages_path = WP_LANG_DIR; … … 922 917 // If the host is the same or it's a relative URL. 923 918 if ( 924 919 strpos( $src_url['path'], $content_url['path'] ) === 0 && 925 ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )920 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] ) 926 921 ) { 927 922 // Make the src relative the specific plugin or theme. 928 $relative = trim( substr( $src _url['path'], strlen( $content_url['path'] ) ), '/' );923 $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' ); 929 924 $relative = explode( '/', $relative ); 930 925 931 926 $languages_path = WP_LANG_DIR . '/' . $relative[0]; 932 927 933 928 $relative = array_slice( $relative, 2 ); 934 929 $relative = implode( '/', $relative ); 935 } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {930 } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) { 936 931 if ( ! isset( $site_url['path'] ) ) { 937 932 $relative = trim( $src_url['path'], '/' ); 938 } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] )) === 0 ) ) {933 } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) { 939 934 // Make the src relative to the WP root. 940 $relative = substr( $src _url['path'], strlen( $site_url['path'] ) );935 $relative = substr( $src, strlen( $site_url['path'] ) ); 941 936 $relative = trim( $relative, '/' ); 942 937 } 943 938 } 944 939 945 /**946 * Filters the relative path of scripts used for finding translation files.947 *948 * @since 5.0.2949 *950 * @param string $relative The relative path of the script. False if it could not be determined.951 * @param string $src The full source url of the script.952 */953 $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );954 955 940 // If the source is not from WP. 956 941 if ( false === $relative ) { 957 return load_script_translations( false, $handle, $domain );942 return false; 958 943 } 959 944 960 945 // Translations are always based on the unminified filename. … … 963 948 } 964 949 965 950 $md5_filename = $file_base . '-' . md5( $relative ) . '.json'; 966 967 if ( $path ) { 968 $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain ); 969 970 if ( $translations ) { 971 return $translations; 972 } 951 if ( $path && file_exists( $path . '/' . $md5_filename ) ) { 952 return file_get_contents( $path . '/' . $md5_filename ); 973 953 } 974 975 $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain ); 976 977 if ( $translations ) { 978 return $translations; 979 } 980 981 return load_script_translations( false, $handle, $domain ); 982 } 983 984 /** 985 * Loads the translation data for the given script handle and text domain. 986 * 987 * @since 5.0.2 988 * 989 * @param string|false $file Path to the translation file to load. False if there isn't one. 990 * @param string $handle Name of the script to register a translation domain to. 991 * @param string $domain The text domain. 992 * @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none. 993 */ 994 function load_script_translations( $file, $handle, $domain ) { 995 /** 996 * Pre-filters script translations for the given file, script handle and text domain. 997 * 998 * Returning a non-null value allows to override the default logic, effectively short-circuiting the function. 999 * 1000 * @since 5.0.2 1001 * 1002 * @param string|false $translations JSON-encoded translation data. Default null. 1003 * @param string|false $file Path to the translation file to load. False if there isn't one. 1004 * @param string $handle Name of the script to register a translation domain to. 1005 * @param string $domain The text domain. 1006 */ 1007 $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain ); 1008 1009 if ( null !== $translations ) { 1010 return $translations; 954 if ( file_exists( $languages_path . '/' . $md5_filename ) ) { 955 return file_get_contents( $languages_path . '/' . $md5_filename ); 1011 956 } 1012 957 1013 /** 1014 * Filters the file path for loading script translations for the given script handle and text domain. 1015 * 1016 * @since 5.0.2 1017 * 1018 * @param string|false $file Path to the translation file to load. False if there isn't one. 1019 * @param string $handle Name of the script to register a translation domain to. 1020 * @param string $domain The text domain. 1021 */ 1022 $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain ); 1023 1024 if ( ! $file || ! is_readable( $file ) ) { 1025 return false; 1026 } 1027 1028 $translations = file_get_contents( $file ); 1029 1030 /** 1031 * Filters script translations for the given file, script handle and text domain. 1032 * 1033 * @since 5.0.2 1034 * 1035 * @param string $translations JSON-encoded translation data. 1036 * @param string $file Path to the translation file that was loaded. 1037 * @param string $handle Name of the script to register a translation domain to. 1038 * @param string $domain The text domain. 1039 */ 1040 return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain ); 958 return false; 1041 959 } 1042 960 1043 961 /** … … 1472 1390 1473 1391 /** 1474 1392 * Determines whether the current locale is right-to-left (RTL). 1475 * 1393 * 1476 1394 * For more information on this and similar theme functions, check out 1477 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1395 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1478 1396 * Conditional Tags} article in the Theme Developer Handbook. 1479 1397 * 1480 1398 * @since 3.0.0