WordPress.org

Make WordPress Core

Ticket #34114: 34114.3.diff

File 34114.3.diff, 10.4 KB (added by swissspidy, 5 years ago)
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index 4bfb989..63c368c 100644
    function load_child_theme_textdomain( $domain, $path = false ) { 
    780780}
    781781
    782782/**
     783 * Just in time loading of plugin and theme textdomains.
     784 *
     785 * When a textdomain is encountered for the first time, we try to load the translation file
     786 * from wp-content/languages, removing the need to call @see load_plugin_texdomain() or
     787 * @see load_theme_texdomain(). Holds a cached list of available .mo files to improve performance.
     788 *
     789 * @since 4.5.0
     790 *
     791 * @see get_translations_for_domain()
     792 *
     793 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     794 * @return bool True when the textdomain is successfully loaded, false otherwise.
     795 */
     796function load_textdomain_just_in_time( $domain ) {
     797        static $cached_mofiles = array();
     798
     799        if ( empty( $cached_mofiles ) ) {
     800                $locations = array(
     801                        WP_LANG_DIR . '/plugins',
     802                        WP_LANG_DIR . '/themes',
     803                );
     804
     805                foreach ( $locations as $location ) {
     806                        foreach ( get_available_languages( $location ) as $file ) {
     807                                $cached_mofiles[] = "{$location}/{$file}.mo";
     808                        }
     809                }
     810        }
     811
     812        $locale = get_locale();
     813        $mofile = "{$domain}-{$locale}.mo";
     814
     815        if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
     816                return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
     817        }
     818
     819        if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
     820                return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
     821        }
     822
     823        return false;
     824}
     825
     826/**
    783827 * Return the Translations instance for a text domain.
    784828 *
    785829 * If there isn't one, returns empty Translations instance.
    function load_child_theme_textdomain( $domain, $path = false ) { 
    793837 */
    794838function get_translations_for_domain( $domain ) {
    795839        global $l10n;
    796         if ( isset( $l10n[ $domain ] ) ) {
     840        if ( load_textdomain_just_in_time( $domain ) || isset( $l10n[ $domain ] ) ) {
    797841                return $l10n[ $domain ];
    798842        }
    799843
  • new file tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po

    diff --git tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo
    new file mode 100644
    index 0000000..9130bb0
    Binary files /dev/null and tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo differ
    diff --git tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po
    new file mode 100644
    index 0000000..41778f3
    - +  
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:31+0100\n"
     5"PO-Revision-Date: 2016-04-21 21:46+0200\n"
     6"Last-Translator: Pascal Birchler <pascal@required.ch>\n"
     7"Language-Team: \n"
     8"Language: de_DE\n"
     9"MIME-Version: 1.0\n"
     10"Content-Type: text/plain; charset=UTF-8\n"
     11"Content-Transfer-Encoding: 8bit\n"
     12"X-Generator: Poedit 1.8.4\n"
     13"X-Poedit-Basepath: .\n"
     14"Plural-Forms: nplurals=2; plural=(n != 1);\n"
     15"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
     16"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;"
     17"esc_html_x:1,2c\n"
     18"X-Textdomain-Support: yes\n"
     19"X-Poedit-SearchPath-0: .\n"
     20
     21#: internationalized-plugin.php:11
     22msgid "This is a dummy plugin"
     23msgstr "Das ist ein Dummy Plugin"
  • new file tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po

    diff --git tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo
    new file mode 100644
    index 0000000..c88c102
    Binary files /dev/null and tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo differ
    diff --git tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po
    new file mode 100644
    index 0000000..ef4e31f
    - +  
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:38+0100\n"
     5"PO-Revision-Date: 2016-04-21 21:46+0200\n"
     6"Last-Translator: Pascal Birchler <pascal@required.ch>\n"
     7"Language-Team: \n"
     8"Language: de_DE\n"
     9"MIME-Version: 1.0\n"
     10"Content-Type: text/plain; charset=UTF-8\n"
     11"Content-Transfer-Encoding: 8bit\n"
     12"X-Generator: Poedit 1.8.4\n"
     13"X-Poedit-Basepath: .\n"
     14"Plural-Forms: nplurals=2; plural=(n != 1);\n"
     15"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
     16"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;"
     17"esc_html_x:1,2c\n"
     18"X-Textdomain-Support: yes\n"
     19"X-Poedit-SearchPath-0: .\n"
     20
     21#: functions.php:7
     22msgid "This is a dummy theme"
     23msgstr "Das ist ein Dummy Theme"
  • new file tests/phpunit/data/plugins/internationalized-plugin.php

    diff --git tests/phpunit/data/plugins/internationalized-plugin.php tests/phpunit/data/plugins/internationalized-plugin.php
    new file mode 100644
    index 0000000..4b56846
    - +  
     1<?php
     2/*
     3Plugin Name: Dummy Plugin
     4Plugin URI: https://wordpress.org/
     5Description: For testing purposes only.
     6Version: 1.0.0
     7Text Domain: internationalized-plugin
     8*/
     9
     10function i18n_plugin_test() {
     11        return __( 'This is a dummy plugin', 'internationalized-plugin' );
     12}
  • new file tests/phpunit/data/themedir1/internationalized-theme/functions.php

    diff --git tests/phpunit/data/themedir1/internationalized-theme/functions.php tests/phpunit/data/themedir1/internationalized-theme/functions.php
    new file mode 100644
    index 0000000..d836c4b
    - +  
     1<?php
     2/**
     3 * Dummy theme.
     4 */
     5
     6function i18n_theme_test() {
     7        return __( 'This is a dummy theme', 'internationalized-theme' );
     8}
  • new file tests/phpunit/data/themedir1/internationalized-theme/index.php

    diff --git tests/phpunit/data/themedir1/internationalized-theme/index.php tests/phpunit/data/themedir1/internationalized-theme/index.php
    new file mode 100644
    index 0000000..c674182
    - +  
     1<?php
     2/**
     3 * Dummy theme.
     4 */
  • new file tests/phpunit/data/themedir1/internationalized-theme/style.css

    diff --git tests/phpunit/data/themedir1/internationalized-theme/style.css tests/phpunit/data/themedir1/internationalized-theme/style.css
    new file mode 100644
    index 0000000..ea472af
    - +  
     1/*
     2Theme Name: Internationalized Theme
     3Theme URI: https://wordpress.org/
     4Description: For testing purposes only.
     5Version: 1.0.0
     6Text Domain: internationalized-theme
     7*/
  • tests/phpunit/tests/l10n.php

    diff --git tests/phpunit/tests/l10n.php tests/phpunit/tests/l10n.php
    index e5713da..777d9f9 100644
    class Tests_L10n extends WP_UnitTestCase { 
    4444                $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
    4545        }
    4646
     47        public function _set_locale_to_german() {
     48                return 'de_DE';
     49        }
     50
     51        /**
     52         * @ticket 34114
     53         */
     54        public function test_load_plugin_textdomain_just_in_time() {
     55                add_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     56
     57                $this->_copy_file( DIR_TESTDATA . '/plugins/internationalized-plugin.php', WP_PLUGIN_DIR );
     58
     59                activate_plugin( 'internationalized-plugin.php' );
     60
     61                $is_plugin_active            = is_plugin_active( 'internationalized-plugin.php' );
     62                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-plugin' );
     63                $expected_output             = i18n_plugin_test();
     64                $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-plugin' );
     65
     66                remove_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     67
     68                $this->assertTrue( $is_plugin_active );
     69                $this->assertFalse( $is_textdomain_loaded_before );
     70                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output );
     71                $this->assertTrue( $is_textdomain_loaded_after );
     72
     73        }
     74
     75        /**
     76         * @ticket 34114
     77         */
     78        public function test_load_theme_textdomain_just_in_time() {
     79                add_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     80
     81                $this->_copy_file( DIR_TESTDATA . '/themedir1/internationalized-theme', WP_CONTENT_DIR . '/themes/internationalized-theme' );
     82
     83                switch_theme( 'internationalized-theme' );
     84
     85                include_once( get_stylesheet_directory() . '/functions.php' );
     86
     87                $theme_name                  = wp_get_theme()->get( 'Name' );
     88                $theme_validated             = validate_current_theme();
     89                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-theme' );
     90                $expected_output             = i18n_theme_test();
     91
     92                $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-theme' );
     93
     94                remove_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     95
     96                $this->assertSame( 'Internationalized Theme', $theme_name );
     97                $this->assertTrue( $theme_validated );
     98                $this->assertFalse( $is_textdomain_loaded_before );
     99                $this->assertSame( 'Das ist ein Dummy Theme', $expected_output );
     100                $this->assertTrue( $is_textdomain_loaded_after );
     101        }
     102
    47103        /**
    48104         * @ticket 35073
    49105         */
    class Tests_L10n extends WP_UnitTestCase { 
    177233                $this->assertNotEmpty( $array['X-Generator'] );
    178234        }
    179235
     236        /**
     237         * Copy file or directory from one place to another.
     238         *
     239         * @access private
     240         *
     241         * @param string $source
     242         * @param string $destination
     243         */
     244        private function _copy_file( $source, $destination ) {
     245                if ( ! is_dir( $destination ) ) {
     246                        mkdir( $destination, 0777, true );
     247                }
     248
     249                if ( is_dir( $source ) ) {
     250                        $files_to_move = array();
     251                        if ( $source_dir = opendir( $source ) ) {
     252                                while ( false !== $file = readdir( $source_dir ) ) {
     253                                        if ( 0 !== strpos( $file, '.' ) ) {
     254                                                $files_to_move[] = $file;
     255                                        }
     256                                }
     257                        }
     258
     259                        @closedir( $source_dir );
     260
     261                        foreach ( $files_to_move as $file_to_move ) {
     262                                @unlink( $destination . '/' . $file_to_move );
     263                                copy( $source . '/' . $file_to_move, $destination . '/' . $file_to_move );
     264                                chmod( $destination . '/' . $file_to_move, 0777 );
     265                        }
     266                } else {
     267                        @unlink( $destination . '/' . basename( $source ) );
     268                        copy( $source, $destination . '/' . basename( $source ) );
     269                        chmod( $destination . '/' . basename( $source ), 0777 );
     270                }
     271        }
     272
    180273}
  • tests/phpunit/tests/theme/themeDir.php

    diff --git tests/phpunit/tests/theme/themeDir.php tests/phpunit/tests/theme/themeDir.php
    index d5060f8..a6df7ba 100644
    class Tests_Theme_ThemeDir extends WP_UnitTestCase { 
    159159                        'My Subdir Theme',// theme in a subdirectory should work
    160160                        'Page Template Theme', // theme with page templates for other test code
    161161                        'Theme with Spaces in the Directory',
     162                        'Internationalized Theme',
    162163                );
    163164
    164165                sort($theme_names);