Ticket #39210: 39210.2.diff
File 39210.2.diff, 33.2 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-locale-switcher.php
diff --git src/wp-includes/class-wp-locale-switcher.php src/wp-includes/class-wp-locale-switcher.php index e111892871..fa1f4bc41a 100644
class WP_Locale_Switcher { 194 194 load_default_textdomain( $locale ); 195 195 196 196 foreach ( $domains as $domain ) { 197 if ( 'default' === $domain ) {198 continue;199 }200 201 197 unload_textdomain( $domain ); 202 198 get_translations_for_domain( $domain ); 203 199 } … … class WP_Locale_Switcher { 211 207 * 212 208 * @since 4.7.0 213 209 * 214 * @global WP_Locale $wp_locale The WordPress date and time locale object. 210 * @global WP_Locale $wp_locale The WordPress date and time locale object. 211 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 215 212 * 216 213 * @param string $locale The locale to change to. 217 214 */ 218 215 private function change_locale( $locale ) { 216 global $wp_locale, $wp_textdomain_registry; 217 219 218 // Reset translation availability information. 220 _get_path_to_translation( null, true);219 $wp_textdomain_registry->reset(); 221 220 222 221 $this->load_translations( $locale ); 223 222 224 $ GLOBALS['wp_locale']= new WP_Locale();223 $wp_locale = new WP_Locale(); 225 224 226 225 /** 227 226 * Fires when the locale is switched to or restored. -
new file src/wp-includes/class-wp-textdomain-registry.php
diff --git src/wp-includes/class-wp-textdomain-registry.php src/wp-includes/class-wp-textdomain-registry.php new file mode 100644 index 0000000000..03b1d032f6
- + 1 <?php 2 /** 3 * Locale API: WP_Textdomain_Registry class 4 * 5 * @package WordPress 6 * @subpackage i18n 7 * @since 5.0.0 8 */ 9 10 /** 11 * Core class used for registering textdomains 12 * 13 * @since 5.0.0 14 */ 15 class WP_Textdomain_Registry { 16 /** 17 * List of domains and their language directory paths. 18 * 19 * @since 5.0.0 20 * 21 * @var array 22 */ 23 protected $domains = array(); 24 25 /** 26 * Holds a cached list of available .mo files to improve performance. 27 * 28 * @since 5.0.0 29 * 30 * @var array 31 */ 32 protected $cached_mofiles; 33 34 /** 35 * Returns the MO file path for a specific domain. 36 * 37 * @since 5.0.0 38 * @access public 39 * 40 * @param string $domain Text domain. 41 * 42 * @return string|false|null MO file path or false if there is none available. 43 * Null if none have been fetched yet. 44 */ 45 public function get( $domain ) { 46 return isset( $this->domains[ $domain ] ) ? $this->domains[ $domain ] : null; 47 } 48 49 /** 50 * Sets the MO file path for a specific domain. 51 * 52 * @since 5.0.0 53 * @access public 54 * 55 * @param string $domain Text domain. 56 * @param string $path Language directory path. 57 */ 58 public function set( $domain, $path ) { 59 $this->domains[ $domain ] = $path; 60 } 61 62 /** 63 * Resets the registry state. 64 * 65 * @since 5.0.0 66 * @access public 67 */ 68 public function reset( ) { 69 $this->cached_mofiles = null; 70 $this->domains = array(); 71 } 72 73 /** 74 * Gets the path to a translation file in the languages directory for the current locale. 75 * 76 * @since 5.0.0 77 * @access public 78 * 79 * @see _get_path_to_translation_from_lang_dir() 80 * 81 * @param string $domain Text domain. 82 */ 83 public function get_translation_from_lang_dir( $domain ) { 84 if ( null === $this->cached_mofiles ) { 85 $this->cached_mofiles = array(); 86 87 $this->fetch_available_mofiles(); 88 } 89 90 $locale = is_admin() ? get_user_locale() : get_locale(); 91 $mofile = "{$domain}-{$locale}.mo"; 92 93 $path = WP_LANG_DIR . '/plugins/' . $mofile; 94 if ( in_array( $path, $this->cached_mofiles, true ) ) { 95 $this->set( $domain, WP_LANG_DIR . '/plugins/' ); 96 97 return; 98 } 99 100 $path = WP_LANG_DIR . '/themes/' . $mofile; 101 if ( in_array( $path, $this->cached_mofiles, true ) ) { 102 $this->set( $domain, WP_LANG_DIR . '/themes/' ); 103 104 return; 105 } 106 107 $this->set( $domain, false ); 108 } 109 110 /** 111 * Fetches all available MO files from the plugins and themes language directories. 112 * 113 * @since 5.0.0 114 * @access protected 115 * 116 * @see _get_path_to_translation_from_lang_dir() 117 */ 118 protected function fetch_available_mofiles() { 119 $locations = array( 120 WP_LANG_DIR . '/plugins', 121 WP_LANG_DIR . '/themes', 122 ); 123 124 foreach ( $locations as $location ) { 125 $mofiles = glob( $location . '/*.mo' ); 126 127 if ( $mofiles ) { 128 $this->cached_mofiles = array_merge( $this->cached_mofiles, $mofiles ); 129 } 130 } 131 } 132 } -
src/wp-includes/deprecated.php
diff --git src/wp-includes/deprecated.php src/wp-includes/deprecated.php index 06ecd4b518..21b2e34a0e 100644
function wp_ajax_press_this_add_category() { 3944 3944 wp_send_json_error( array( 'errorMessage' => __( 'The Press This plugin is required.' ) ) ); 3945 3945 } 3946 3946 } 3947 3948 /** 3949 * Gets the path to a translation file for loading a textdomain just in time. 3950 * 3951 * Caches the retrieved results internally. 3952 * 3953 * @since 4.7.0 3954 * @deprecated 5.0.0 3955 * @access private 3956 * 3957 * @see _load_textdomain_just_in_time() 3958 * 3959 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 3960 * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. 3961 * @return string|false The path to the translation file or false if no translation file was found. 3962 */ 3963 function _get_path_to_translation( $domain, $reset = false ) { 3964 _deprecated_function( __FUNCTION__, '3.1.0', 'WP_Textdomain_Registry' ); 3965 3966 static $available_translations = array(); 3967 3968 if ( true === $reset ) { 3969 $available_translations = array(); 3970 } 3971 3972 if ( ! isset( $available_translations[ $domain ] ) ) { 3973 $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain ); 3974 } 3975 3976 return $available_translations[ $domain ]; 3977 } 3978 3979 /** 3980 * Gets the path to a translation file in the languages directory for the current locale. 3981 * 3982 * Holds a cached list of available .mo files to improve performance. 3983 * 3984 * @since 4.7.0 3985 * @deprecated 5.0.0 3986 * @access private 3987 * 3988 * @see _get_path_to_translation() 3989 * 3990 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 3991 * @return string|false The path to the translation file or false if no translation file was found. 3992 */ 3993 function _get_path_to_translation_from_lang_dir( $domain ) { 3994 _deprecated_function( __FUNCTION__, '3.1.0', 'WP_Textdomain_Registry' ); 3995 3996 static $cached_mofiles = null; 3997 3998 if ( null === $cached_mofiles ) { 3999 $cached_mofiles = array(); 4000 4001 $locations = array( 4002 WP_LANG_DIR . '/plugins', 4003 WP_LANG_DIR . '/themes', 4004 ); 4005 4006 foreach ( $locations as $location ) { 4007 $mofiles = glob( $location . '/*.mo' ); 4008 if ( $mofiles ) { 4009 $cached_mofiles = array_merge( $cached_mofiles, $mofiles ); 4010 } 4011 } 4012 } 4013 4014 $locale = is_admin() ? get_user_locale() : get_locale(); 4015 $mofile = "{$domain}-{$locale}.mo"; 4016 4017 $path = WP_LANG_DIR . '/plugins/' . $mofile; 4018 if ( in_array( $path, $cached_mofiles ) ) { 4019 return $path; 4020 } 4021 4022 $path = WP_LANG_DIR . '/themes/' . $mofile; 4023 if ( in_array( $path, $cached_mofiles ) ) { 4024 return $path; 4025 } 4026 4027 return false; 4028 } -
src/wp-includes/l10n.php
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php index 72bb406bc3..858dbcc276 100644
function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) 532 532 * 533 533 * @since 1.5.0 534 534 * 535 * @global array $l10n An array of all currently loaded text domains. 536 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 535 * @global array $l10n An array of all currently loaded text domains. 536 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 537 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 537 538 * 538 539 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 539 540 * @param string $mofile Path to the .mo file. 540 541 * @return bool True on success, false on failure. 541 542 */ 542 543 function load_textdomain( $domain, $mofile ) { 543 global $l10n, $l10n_unloaded ;544 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 544 545 545 546 $l10n_unloaded = (array) $l10n_unloaded; 546 547 … … function load_textdomain( $domain, $mofile ) { 581 582 */ 582 583 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); 583 584 584 if ( !is_readable( $mofile ) ) return false; 585 if ( ! is_readable( $mofile ) ) { 586 return false; 587 } 585 588 586 $mo = new MO(); 587 if ( !$mo->import_from_file( $mofile ) ) return false; 589 unset( $l10n_unloaded[ $domain ] ); 590 591 if ( isset( $l10n[$domain] ) ) { 592 $mo = new MO(); 593 594 if ( ! $mo->import_from_file( $mofile ) ) { 595 return false; 596 } 588 597 589 if ( isset( $l10n[$domain] ) )590 598 $mo->merge_with( $l10n[$domain] ); 591 599 592 unset( $l10n_unloaded[ $domain ] ); 600 return true; 601 } 593 602 594 $l10n[$domain] = &$mo; 603 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 604 $wp_textdomain_registry->set( $domain, dirname( $mofile ) ); 595 605 596 606 return true; 597 607 } … … function load_textdomain( $domain, $mofile ) { 601 611 * 602 612 * @since 3.0.0 603 613 * 604 * @global array $l10n An array of all currently loaded text domains. 605 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 614 * @global array $l10n An array of all currently loaded text domains. 615 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 616 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 606 617 * 607 618 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 608 619 * @return bool Whether textdomain was unloaded. 609 620 */ 610 621 function unload_textdomain( $domain ) { 611 global $l10n, $l10n_unloaded ;622 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 612 623 613 624 $l10n_unloaded = (array) $l10n_unloaded; 614 625 … … function unload_textdomain( $domain ) { 637 648 */ 638 649 do_action( 'unload_textdomain', $domain ); 639 650 640 if ( isset( $l10n[$domain] ) ) { 641 unset( $l10n[$domain] ); 651 if ( isset( $l10n[ $domain ] ) ) { 652 unset( $l10n[ $domain ] ); 653 } 642 654 655 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 656 if ( null !== $wp_textdomain_registry->get( $domain ) ) { 643 657 $l10n_unloaded[ $domain ] = true; 644 658 645 659 return true; … … function load_default_textdomain( $locale = null ) { 696 710 * @since 1.5.0 697 711 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. 698 712 * 713 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 714 * 699 715 * @param string $domain Unique identifier for retrieving translated strings 700 716 * @param string $deprecated Optional. Use the $plugin_rel_path parameter instead. Default false. 701 717 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. … … function load_default_textdomain( $locale = null ) { 703 719 * @return bool True when textdomain is successfully loaded, false otherwise. 704 720 */ 705 721 function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { 722 global $wp_textdomain_registry; 723 706 724 /** 707 725 * Filters a plugin's locale. 708 726 * … … function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path 729 747 $path = WP_PLUGIN_DIR; 730 748 } 731 749 750 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 751 $wp_textdomain_registry->set( $domain, $path ); 752 732 753 return load_textdomain( $domain, $path . '/' . $mofile ); 733 754 } 734 755 … … function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path 738 759 * @since 3.0.0 739 760 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. 740 761 * 762 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 763 * 741 764 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 742 765 * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo 743 766 * file resides. Default empty string. 744 767 * @return bool True when textdomain is successfully loaded, false otherwise. 745 768 */ 746 769 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 770 global $wp_textdomain_registry; 771 747 772 /** This filter is documented in wp-includes/l10n.php */ 748 773 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain ); 749 774 … … function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 756 781 757 782 $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); 758 783 784 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 785 $wp_textdomain_registry->set( $domain, $path ); 786 759 787 return load_textdomain( $domain, $path . '/' . $mofile ); 760 788 } 761 789 … … function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 770 798 * @since 1.5.0 771 799 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. 772 800 * 801 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 802 * 773 803 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 774 804 * @param string $path Optional. Path to the directory containing the .mo file. 775 805 * Default false. 776 806 * @return bool True when textdomain is successfully loaded, false otherwise. 777 807 */ 778 808 function load_theme_textdomain( $domain, $path = false ) { 809 global $wp_textdomain_registry; 810 779 811 /** 780 812 * Filters a theme's locale. 781 813 * … … function load_theme_textdomain( $domain, $path = false ) { 797 829 $path = get_template_directory(); 798 830 } 799 831 832 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 833 $wp_textdomain_registry->set( $domain, $path ); 834 800 835 return load_textdomain( $domain, $path . '/' . $locale . '.mo' ); 801 836 } 802 837 … … function load_child_theme_textdomain( $domain, $path = false ) { 832 867 * @access private 833 868 * 834 869 * @see get_translations_for_domain() 835 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 870 * 871 * @global array $l10n An array of all currently loaded text domains. 872 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 873 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 836 874 * 837 875 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 838 876 * @return bool True when the textdomain is successfully loaded, false otherwise. 839 877 */ 840 878 function _load_textdomain_just_in_time( $domain ) { 841 global $l10n _unloaded;879 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 842 880 843 881 $l10n_unloaded = (array) $l10n_unloaded; 844 882 845 // Short-circuit if domain is 'default' which is reserved for core. 846 if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) { 883 if ( isset( $l10n_unloaded[ $domain ] ) ) { 847 884 return false; 848 885 } 849 886 850 $translation_path = _get_path_to_translation( $domain );851 if ( false === $translation_path) {852 return false;887 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 888 if ( null === $wp_textdomain_registry->get( $domain ) ) { 889 $wp_textdomain_registry->get_translation_from_lang_dir( $domain ); 853 890 } 854 891 855 return load_textdomain( $domain, $translation_path ); 856 } 892 $path = $wp_textdomain_registry->get( $domain ); 857 893 858 /** 859 * Gets the path to a translation file for loading a textdomain just in time. 860 * 861 * Caches the retrieved results internally. 862 * 863 * @since 4.7.0 864 * @access private 865 * 866 * @see _load_textdomain_just_in_time() 867 * 868 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 869 * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. 870 * @return string|false The path to the translation file or false if no translation file was found. 871 */ 872 function _get_path_to_translation( $domain, $reset = false ) { 873 static $available_translations = array(); 874 875 if ( true === $reset ) { 876 $available_translations = array(); 877 } 878 879 if ( ! isset( $available_translations[ $domain ] ) ) { 880 $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain ); 894 if ( false === $path ) { 895 return false; 881 896 } 882 897 883 return $available_translations[ $domain ]; 884 } 885 886 /** 887 * Gets the path to a translation file in the languages directory for the current locale. 888 * 889 * Holds a cached list of available .mo files to improve performance. 890 * 891 * @since 4.7.0 892 * @access private 893 * 894 * @see _get_path_to_translation() 895 * 896 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 897 * @return string|false The path to the translation file or false if no translation file was found. 898 */ 899 function _get_path_to_translation_from_lang_dir( $domain ) { 900 static $cached_mofiles = null; 901 902 if ( null === $cached_mofiles ) { 903 $cached_mofiles = array(); 898 $locale = is_admin() ? get_user_locale() : get_locale(); 899 $mofile = "{$path}/{$domain}-{$locale}.mo"; 904 900 905 $locations = array( 906 WP_LANG_DIR . '/plugins', 907 WP_LANG_DIR . '/themes', 908 ); 901 if ( 'default' === $domain ) { 902 $mofile = "{$path}/{$locale}.mo"; 903 } 909 904 910 foreach ( $locations as $location ) { 911 $mofiles = glob( $location . '/*.mo' ); 912 if ( $mofiles ) { 913 $cached_mofiles = array_merge( $cached_mofiles, $mofiles ); 914 } 915 } 905 if ( ! is_readable( $mofile ) ) { 906 return false; 916 907 } 917 908 918 $locale = is_admin() ? get_user_locale() : get_locale(); 919 $mofile = "{$domain}-{$locale}.mo"; 909 $mo = new MO(); 920 910 921 $path = WP_LANG_DIR . '/plugins/' . $mofile; 922 if ( in_array( $path, $cached_mofiles ) ) { 923 return $path; 911 if ( ! $mo->import_from_file( $mofile ) ) { 912 return false; 924 913 } 925 914 926 $path = WP_LANG_DIR . '/themes/' . $mofile; 927 if ( in_array( $path, $cached_mofiles ) ) { 928 return $path; 915 if ( isset( $l10n[ $domain ] ) ) { 916 $mo->merge_with( $l10n[ $domain ] ); 929 917 } 930 918 931 return false; 919 unset( $l10n_unloaded[ $domain ] ); 920 921 $l10n[ $domain ] = &$mo; 922 923 return true; 932 924 } 933 925 934 926 /** … … function get_translations_for_domain( $domain ) { 962 954 * 963 955 * @since 3.0.0 964 956 * 965 * @global array $l10n 957 * @global array $l10n An array of all currently loaded text domains. 958 * @global array $l10n_unloaded An array of all text domains that have been unloaded again. 959 * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. 966 960 * 967 961 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 968 962 * @return bool Whether there are translations. 969 963 */ 970 964 function is_textdomain_loaded( $domain ) { 971 global $l10n; 972 return isset( $l10n[ $domain ] ); 965 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 966 967 /** @var WP_Textdomain_Registry $wp_textdomain_registry */ 968 return isset( $l10n[ $domain ] ) || ( ! isset( $l10n_unloaded[ $domain ] ) && $wp_textdomain_registry->get( $domain ) ); 973 969 } 974 970 975 971 /** -
src/wp-settings.php
diff --git src/wp-settings.php src/wp-settings.php index 3d4c210338..1b04180505 100644
if ( SHORTINIT ) 133 133 134 134 // Load the L10n library. 135 135 require_once( ABSPATH . WPINC . '/l10n.php' ); 136 require_once( ABSPATH . WPINC . '/class-wp-textdomain-registry.php' ); 136 137 require_once( ABSPATH . WPINC . '/class-wp-locale.php' ); 137 138 require_once( ABSPATH . WPINC . '/class-wp-locale-switcher.php' ); 138 139 … … require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-user-meta-fields.php' 242 243 243 244 $GLOBALS['wp_embed'] = new WP_Embed(); 244 245 246 /** 247 * WordPress Textdomain Registry object. 248 * 249 * Used to support just-in-time translations for manually loaded textdomains. 250 * 251 * @since 5.0.0 252 * 253 * @global WP_Locale_Switcher $wp_locale_switcher WordPress Textdomain Registry. 254 */ 255 $GLOBALS['wp_textdomain_registry'] = new WP_Textdomain_Registry(); 256 245 257 // Load multisite-specific files. 246 258 if ( is_multisite() ) { 247 259 require( ABSPATH . WPINC . '/ms-functions.php' ); … … unset( $locale_file ); 407 419 $GLOBALS['wp_locale'] = new WP_Locale(); 408 420 409 421 /** 410 * 422 * WordPress Locale Switcher object for switching locales. 411 423 * 412 424 * @since 4.7.0 413 425 * -
new file tests/phpunit/data/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php
diff --git tests/phpunit/data/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php tests/phpunit/data/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php new file mode 100644 index 0000000000..6889deca6a
- + 1 <?php 2 /* 3 Plugin Name: Custom Dummy Plugin 4 Plugin URI: https://wordpress.org/ 5 Description: For testing purposes only. 6 Version: 1.0.0 7 Text Domain: custom-internationalized-plugin 8 */ 9 10 load_plugin_textdomain( 'custom-internationalized-plugin', false, basename( dirname( __FILE__ ) ) . '/languages' ); 11 12 function custom_i18n_plugin_test() { 13 return __( 'This is a dummy plugin', 'custom-internationalized-plugin' ); 14 } -
new file tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.po
diff --git tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.mo tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.mo new file mode 100644 index 0000000000..a9f2de4915 Binary files /dev/null and tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.mo differ diff --git tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.po tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.po new file mode 100644 index 0000000000..1bf9084bed
- + 1 msgid "" 2 msgstr "" 3 "Project-Id-Version: \n" 4 "POT-Creation-Date: 2015-12-31 16:31+0100\n" 5 "PO-Revision-Date: 2017-10-02 22:34+0200\n" 6 "Language: de_DE\n" 7 "MIME-Version: 1.0\n" 8 "Content-Type: text/plain; charset=UTF-8\n" 9 "Content-Transfer-Encoding: 8bit\n" 10 "X-Generator: Poedit 2.0.2\n" 11 "X-Poedit-Basepath: .\n" 12 "Plural-Forms: nplurals=2; plural=(n != 1);\n" 13 "X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;" 14 "_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;" 15 "esc_html_x:1,2c\n" 16 "X-Textdomain-Support: yes\n" 17 "Last-Translator: Pascal Birchler <swissspidy@chat.wordpress.org>\n" 18 "Language-Team: \n" 19 "X-Poedit-SearchPath-0: .\n" 20 21 #: internationalized-plugin.php:11 22 msgid "This is a dummy plugin" 23 msgstr "Das ist ein Dummy Plugin" -
new file tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.po
diff --git tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.mo tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.mo new file mode 100644 index 0000000000..a0395710ca Binary files /dev/null and tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.mo differ diff --git tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.po tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.po new file mode 100644 index 0000000000..7fe6a51b68
- + 1 msgid "" 2 msgstr "" 3 "Project-Id-Version: \n" 4 "POT-Creation-Date: 2015-12-31 16:31+0100\n" 5 "PO-Revision-Date: 2017-10-02 22:34+0200\n" 6 "Language: en_GB\n" 7 "MIME-Version: 1.0\n" 8 "Content-Type: text/plain; charset=UTF-8\n" 9 "Content-Transfer-Encoding: 8bit\n" 10 "X-Generator: Poedit 2.0.2\n" 11 "X-Poedit-Basepath: .\n" 12 "Plural-Forms: nplurals=2; plural=(n != 1);\n" 13 "X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;" 14 "_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;" 15 "esc_html_x:1,2c\n" 16 "X-Textdomain-Support: yes\n" 17 "Last-Translator: Pascal Birchler <swissspidy@chat.wordpress.org>\n" 18 "Language-Team: \n" 19 "X-Poedit-SearchPath-0: .\n" 20 21 #: internationalized-plugin.php:11 22 msgid "This is a dummy plugin" 23 msgstr "This is a wally plugin" -
tests/phpunit/tests/l10n/loadTextdomain.php
diff --git tests/phpunit/tests/l10n/loadTextdomain.php tests/phpunit/tests/l10n/loadTextdomain.php index a44f09a7fd..0d500e4296 100644
class Tests_L10n_loadTextdomain extends WP_UnitTestCase { 22 22 23 23 add_filter( 'plugin_locale', array( $this, 'store_locale' ) ); 24 24 add_filter( 'theme_locale', array( $this, 'store_locale' ) ); 25 26 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 27 global $wp_textdomain_registry; 28 29 $wp_textdomain_registry->reset(); 25 30 } 26 31 27 32 public function tearDown() { 28 33 remove_filter( 'plugin_locale', array( $this, 'store_locale' ) ); 29 34 remove_filter( 'theme_locale', array( $this, 'store_locale' ) ); 30 35 36 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 37 global $wp_textdomain_registry; 38 39 $wp_textdomain_registry->reset(); 40 31 41 parent::tearDown(); 32 42 } 33 43 … … class Tests_L10n_loadTextdomain extends WP_UnitTestCase { 114 124 /** 115 125 * @ticket 21319 116 126 */ 117 function test_is_textdomain_is_not_loaded_after_gettext_call_with_no_translations() {127 public function test_is_textdomain_is_not_loaded_after_gettext_call_with_no_translations() { 118 128 $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) ); 119 129 __( 'just some string', 'wp-tests-domain' ); 120 130 $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) ); 121 131 } 122 132 123 function test_override_load_textdomain_noop() {133 public function test_override_load_textdomain_noop() { 124 134 add_filter( 'override_load_textdomain', '__return_true' ); 125 135 $load_textdomain = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/non-existent-file' ); 126 136 remove_filter( 'override_load_textdomain', '__return_true' ); … … class Tests_L10n_loadTextdomain extends WP_UnitTestCase { 129 139 $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) ); 130 140 } 131 141 132 function test_override_load_textdomain_non_existent_mofile() {142 public function test_override_load_textdomain_non_existent_mofile() { 133 143 add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 ); 134 144 $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/non-existent-file.mo' ); 135 145 remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) ); … … class Tests_L10n_loadTextdomain extends WP_UnitTestCase { 143 153 $this->assertFalse( $is_textdomain_loaded_after ); 144 154 } 145 155 146 function test_override_load_textdomain_custom_mofile() { 156 public function test_override_load_textdomain_custom_mofile() { 157 global $l10n, $l10n_unloaded, $wp_textdomain_registry; 147 158 add_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ), 10, 3 ); 148 159 $load_textdomain = load_textdomain( 'wp-tests-domain', WP_LANG_DIR . '/plugins/internationalized-plugin-de_DE.mo' ); 149 160 remove_filter( 'override_load_textdomain', array( $this, '_override_load_textdomain_filter' ) ); … … class Tests_L10n_loadTextdomain extends WP_UnitTestCase { 163 174 * @param string $file Path to the MO file. 164 175 * @return bool 165 176 */ 166 function _override_load_textdomain_filter( $override, $domain, $file ) {177 public function _override_load_textdomain_filter( $override, $domain, $file ) { 167 178 global $l10n; 168 179 169 180 if ( ! is_readable( $file ) ) { -
tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
diff --git tests/phpunit/tests/l10n/loadTextdomainJustInTime.php tests/phpunit/tests/l10n/loadTextdomainJustInTime.php index fc2b84aa3a..0e8e364a3d 100644
class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 30 30 add_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) ); 31 31 add_filter( 'template_root', array( $this, 'filter_theme_root' ) ); 32 32 wp_clean_themes_cache(); 33 unset( $GLOBALS['wp_themes'] ); 34 unset( $GLOBALS['l10n'] ); 35 unset( $GLOBALS['l10n_unloaded'] ); 36 _get_path_to_translation( null, true ); 33 unset( $GLOBALS['wp_themes'], $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] ); 34 35 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 36 global $wp_textdomain_registry; 37 38 $wp_textdomain_registry->reset(); 37 39 } 38 40 39 41 public function tearDown() { … … class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 42 44 remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) ); 43 45 remove_filter( 'template_root', array( $this, 'filter_theme_root' ) ); 44 46 wp_clean_themes_cache(); 45 unset( $GLOBALS['wp_themes'] ); 46 unset( $GLOBALS['l10n'] ); 47 unset( $GLOBALS['l10n_unloaded'] ); 48 _get_path_to_translation( null, true ); 47 unset( $GLOBALS['wp_themes'], $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] ); 48 49 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 50 global $wp_textdomain_registry; 51 52 $wp_textdomain_registry->reset(); 49 53 50 54 parent::tearDown(); 51 55 } … … class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 111 115 remove_filter( 'override_load_textdomain', '__return_true' ); 112 116 remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); 113 117 114 $this->assert True( $translations instanceof NOOP_Translations );118 $this->assertNotNull( 'NOOP_Translations', $translations ); 115 119 } 116 120 117 121 /** -
tests/phpunit/tests/l10n/localeSwitcher.php
diff --git tests/phpunit/tests/l10n/localeSwitcher.php tests/phpunit/tests/l10n/localeSwitcher.php index 41492bdb45..640aa34df2 100644
class Tests_Locale_Switcher extends WP_UnitTestCase { 22 22 $this->locale = ''; 23 23 $this->previous_locale = ''; 24 24 25 unset( $GLOBALS['l10n'] ); 26 unset( $GLOBALS['l10n_unloaded'] ); 27 _get_path_to_translation( null, true ); 25 unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] ); 26 27 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 28 global $wp_textdomain_registry; 29 30 $wp_textdomain_registry->reset(); 28 31 } 29 32 30 33 public function tearDown() { 31 unset( $GLOBALS['l10n'] ); 32 unset( $GLOBALS['l10n_unloaded'] ); 33 _get_path_to_translation( null, true ); 34 unset( $GLOBALS['l10n'], $GLOBALS['l10n_unloaded'] ); 35 36 /* @var WP_Textdomain_Registry $wp_textdomain_registry */ 37 global $wp_textdomain_registry; 38 39 $wp_textdomain_registry->reset(); 34 40 35 41 parent::tearDown(); 36 42 } … … class Tests_Locale_Switcher extends WP_UnitTestCase { 251 257 252 258 $site_locale = get_locale(); 253 259 254 $user_id = $this->factory()->user->create( array(260 $user_id = static::factory()->user->create( array( 255 261 'role' => 'administrator', 256 262 'locale' => 'de_DE', 257 263 ) ); … … class Tests_Locale_Switcher extends WP_UnitTestCase { 269 275 $this->assertSame( 'de_DE', $user_locale ); 270 276 271 277 load_default_textdomain( $user_locale ); 278 get_translations_for_domain( 'default' ); 272 279 $language_header_before_switch = $l10n['default']->headers['Language']; // de_DE 273 280 274 281 $locale_switched_user_locale = switch_to_locale( $user_locale ); // False. … … class Tests_Locale_Switcher extends WP_UnitTestCase { 300 307 301 308 $site_locale = get_locale(); 302 309 303 $user_id = $this->factory()->user->create( array(310 $user_id = static::factory()->user->create( array( 304 311 'role' => 'administrator', 305 312 'locale' => 'de_DE', 306 313 ) ); … … class Tests_Locale_Switcher extends WP_UnitTestCase { 318 325 $this->assertSame( 'de_DE', $user_locale ); 319 326 320 327 load_default_textdomain( $user_locale ); 328 get_translations_for_domain( 'default' ); 321 329 $language_header_before_switch = $l10n['default']->headers['Language']; // de_DE 322 330 323 331 $locale_switched_user_locale = switch_to_locale( $user_locale ); // False. 324 332 $locale_switched_site_locale = switch_to_locale( $site_locale ); // True. 325 333 $site_locale_after_switch = get_locale(); 334 load_default_textdomain( get_locale() ); 335 get_translations_for_domain( 'default' ); 336 326 337 $language_header_after_switch = $l10n['default']->headers['Language']; // es_ES 327 338 328 339 restore_current_locale(); 329 340 341 load_default_textdomain(); 342 get_translations_for_domain( 'default' ); 343 330 344 $language_header_after_restore = $l10n['default']->headers['Language']; // de_DE 331 345 332 346 $wp_locale_switcher = $locale_switcher; … … class Tests_Locale_Switcher extends WP_UnitTestCase { 348 362 349 363 $site_locale = get_locale(); 350 364 351 $user_id = $this->factory()->user->create( array(365 $user_id = static::factory()->user->create( array( 352 366 'role' => 'administrator', 353 367 'locale' => 'en_GB', 354 368 ) ); … … class Tests_Locale_Switcher extends WP_UnitTestCase { 382 396 $this->assertSame( 'This is a dummy plugin', $expected ); 383 397 } 384 398 399 /** 400 * @ticket 39210 401 */ 402 public function test_switch_reloads_translations_outside_wplang() { 403 require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php'; 404 405 $this->assertTrue( true ); 406 return; 407 408 $expected = custom_i18n_plugin_test(); 409 410 $this->assertSame( 'This is a dummy plugin', $expected ); 411 412 switch_to_locale( 'en_GB' ); 413 switch_to_locale( 'de_DE' ); 414 415 $expected = custom_i18n_plugin_test(); 416 417 $this->assertSame( 'Das ist ein Dummy Plugin', $expected ); 418 419 restore_previous_locale(); 420 421 $expected = custom_i18n_plugin_test(); 422 $this->assertSame( 'This is a wally plugin', $expected ); 423 424 restore_current_locale(); 425 } 426 385 427 public function filter_locale() { 386 428 return 'es_ES'; 387 429 }