WordPress.org

Make WordPress Core

Ticket #39210: 39210.diff

File 39210.diff, 12.5 KB (added by yoavf, 3 years ago)
  • src/wp-includes/class-wp-locale-switcher.php

     
    187187         * @access private
    188188         *
    189189         * @global Mo[] $l10n An array of all currently loaded text domains.
     190         * @global array $l10n_paths An array of all plugin and theme text domains and the paths from which they've been loaded.
    190191         *
    191192         * @param string $locale The locale to load translations for.
    192193         */
    193194        private function load_translations( $locale ) {
    194                 global $l10n;
     195                global $l10n, $l10n_paths;
    195196
    196                 $domains = $l10n ? array_keys( $l10n ) : array();
     197                $loaded_domains = $l10n ? array_keys( $l10n ) : array();
     198                $domains_from_paths = $l10n_paths ? array_keys( $l10n_paths ) : array();
    197199
     200                $domains = array_unique( array_merge( $loaded_domains, $domains_from_paths ) );
     201
    198202                load_default_textdomain( $locale );
    199203
    200204                foreach ( $domains as $domain ) {
  • src/wp-includes/l10n.php

     
    695695 * @since 1.5.0
    696696 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
    697697 *
     698 * @global array $l10n_paths      An array of all plugin and theme text domains and the paths
     699 *                                from which they've been loaded.
     700 *
    698701 * @param string $domain          Unique identifier for retrieving translated strings
    699702 * @param string $deprecated      Optional. Use the $plugin_rel_path parameter instead. Default false.
    700703 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
     
    702705 * @return bool True when textdomain is successfully loaded, false otherwise.
    703706 */
    704707function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
     708        global $l10n_paths;
     709
    705710        /**
    706711         * Filters a plugin's locale.
    707712         *
     
    728733                $path = WP_PLUGIN_DIR;
    729734        }
    730735
     736        $l10n_paths[ $domain ] = array( 'type' => 'plugin', 'path' => str_replace( WP_PLUGIN_DIR . '/', '', $path ) );
     737
    731738        return load_textdomain( $domain, $path . '/' . $mofile );
    732739}
    733740
     
    737744 * @since 3.0.0
    738745 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
    739746 *
     747 * @global array $l10n_paths         An array of all plugin and theme text domains and the paths from which
     748 *                                   they've been loaded.
     749 *
    740750 * @param string $domain             Text domain. Unique identifier for retrieving translated strings.
    741751 * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
    742752 *                                   file resides. Default empty string.
     
    743753 * @return bool True when textdomain is successfully loaded, false otherwise.
    744754 */
    745755function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
     756        global $l10n_paths;
     757
    746758        /** This filter is documented in wp-includes/l10n.php */
    747759        $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
    748760
     
    754766        }
    755767
    756768        $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) );
     769        $l10n_paths[ $domain ] = array( 'type' => 'muplugin', 'path' => str_replace( WPMU_PLUGIN_DIR . '/', '', $path ) );
    757770
    758771        return load_textdomain( $domain, $path . '/' . $mofile );
    759772}
     
    769782 * @since 1.5.0
    770783 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
    771784 *
    772  * @param string $domain Text domain. Unique identifier for retrieving translated strings.
    773  * @param string $path   Optional. Path to the directory containing the .mo file.
    774  *                       Default false.
     785 * @global array $l10n_paths         An array of all plugin and theme text domains and the paths from which
     786 *                                   they've been loaded.
     787 *
     788 * @param string $domain             Text domain. Unique identifier for retrieving translated strings.
     789 * @param bool|string $path          Optional. Path to the directory containing the .mo file.
     790 *                                   Default false.
    775791 * @return bool True when textdomain is successfully loaded, false otherwise.
    776792 */
    777793function load_theme_textdomain( $domain, $path = false ) {
     794        global $l10n_paths;
    778795        /**
    779796         * Filters a theme's locale.
    780797         *
     
    796813                $path = get_template_directory();
    797814        }
    798815
     816        $l10n_paths[ $domain ] = array( 'type' => 'theme', 'path' => $path );
     817
    799818        return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
    800819}
    801820
     
    837856 * @return bool True when the textdomain is successfully loaded, false otherwise.
    838857 */
    839858function _load_textdomain_just_in_time( $domain ) {
    840         global $l10n_unloaded;
     859        global $l10n_unloaded, $l10n_paths;
    841860
    842861        $l10n_unloaded = (array) $l10n_unloaded;
    843862
     
    938957 * @since 2.8.0
    939958 *
    940959 * @global array $l10n
     960 * @global array $l10n_paths      An array of all plugin and theme text domains and the paths
     961 *                                from which they've been loaded.
    941962 *
    942963 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     964 *
    943965 * @return Translations|NOOP_Translations A Translations instance.
    944966 */
    945967function get_translations_for_domain( $domain ) {
    946         global $l10n;
     968        global $l10n, $l10n_paths;
     969
    947970        if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
    948971                return $l10n[ $domain ];
    949972        }
    950973
     974        if ( isset( $l10n_paths[ $domain ] ) ) {
     975                $loaded = call_user_func( 'load_' . $l10n_paths[ $domain ]['type'] . '_textdomain', $domain, false, $l10n_paths[ $domain ]['path'] );
     976
     977                if ( $loaded && isset( $l10n[ $domain ] ) ) {
     978                        return $l10n[ $domain ];
     979                }
     980        }
     981
    951982        static $noop_translations = null;
    952983        if ( null === $noop_translations ) {
    953984                $noop_translations = new NOOP_Translations;
  • tests/phpunit/data/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php

     
     1<?php
     2/*
     3Plugin Name: Custom Dummy Plugin
     4Plugin URI: https://wordpress.org/
     5Description: For testing purposes only.
     6Version: 1.0.0
     7Text Domain: custom-internationalized-plugin
     8*/
     9
     10load_plugin_textdomain( 'custom-internationalized-plugin', false, basename(  dirname( __FILE__ )  ) . '/languages' );
     11
     12function custom_i18n_plugin_test() {
     13        return __( 'This is a dummy plugin', 'custom-internationalized-plugin' );
     14}
  • tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.mo

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
  • tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.po

    Property changes on: tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-de_DE.mo
    ___________________________________________________________________
    Added: svn:mime-type
    ## -0,0 +1 ##
    +application/octet-stream
    \ No newline at end of property
     
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:31+0100\n"
     5"PO-Revision-Date: 2016-10-26 00:02+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 1.8.10\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"X-Poedit-SearchPath-0: .\n"
     18
     19#: internationalized-plugin.php:11
     20msgid "This is a dummy plugin"
     21msgstr "Das ist ein Dummy Plugin"
  • tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.mo

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
  • tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.po

    Property changes on: tests/phpunit/data/plugins/custom-internationalized-plugin/languages/custom-internationalized-plugin-en_GB.mo
    ___________________________________________________________________
    Added: svn:mime-type
    ## -0,0 +1 ##
    +application/octet-stream
    \ No newline at end of property
     
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:31+0100\n"
     5"PO-Revision-Date: 2016-12-15 15:26+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 1.8.9\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: \n"
     18"Language-Team: \n"
     19"X-Poedit-SearchPath-0: .\n"
     20
     21#: internationalized-plugin.php:11
     22msgid "This is a dummy plugin"
     23msgstr "This is a wally plugin"
  • tests/phpunit/includes/bootstrap.php

     
    3030define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
    3131
    3232define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
     33define( 'WP_PLUGIN_DIR', DIR_TESTDATA . '/plugins' );
    3334
    3435if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
    3536        define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
  • tests/phpunit/tests/l10n/localeSwitcher.php

     
    382382                $this->assertSame( 'This is a dummy plugin', $expected );
    383383        }
    384384
     385        public function test_switch_reloads_translations_outside_wplang( ){
     386                require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
     387
     388                $expected = custom_i18n_plugin_test();
     389                $this->assertSame( 'This is a dummy plugin', $expected );
     390
     391                switch_to_locale( 'en_GB');
     392                switch_to_locale( 'de_DE');
     393                $expected = custom_i18n_plugin_test();
     394                $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
     395
     396                restore_previous_locale();
     397                $expected = custom_i18n_plugin_test();
     398                $this->assertSame( 'This is a wally plugin', $expected );
     399
     400                restore_current_locale();
     401        }
     402
    385403        public function filter_locale() {
    386404                return 'es_ES';
    387405        }