WordPress.org

Make WordPress Core

Ticket #34114: 34114.4.diff

File 34114.4.diff, 10.2 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..ad986e4 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.6.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 = null;
     798
     799        if ( null === $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..e789268
    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..b1fff26
    - +  
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:31+0100\n"
     5"PO-Revision-Date: 2016-04-28 18:50+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..cfd2b5a
    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..78c90e4
    - +  
     1msgid ""
     2msgstr ""
     3"Project-Id-Version: \n"
     4"POT-Creation-Date: 2015-12-31 16:38+0100\n"
     5"PO-Revision-Date: 2016-04-28 18:50+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..7db9fa7 100644
     
    55 * @group i18n
    66 */
    77class Tests_L10n extends WP_UnitTestCase {
     8        function setUp() {
     9                parent::setUp();
     10                $this->theme_root = DIR_TESTDATA . '/themedir1';
     11
     12                $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
     13
     14                // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
     15                $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
     16
     17                add_filter( 'theme_root', array( $this, '_theme_root' ) );
     18                add_filter( 'stylesheet_root', array( $this, '_theme_root' ) );
     19                add_filter( 'template_root', array( $this, '_theme_root' ) );
     20                // clear caches
     21                wp_clean_themes_cache();
     22                unset( $GLOBALS['wp_themes'] );
     23        }
     24
     25        function tearDown() {
     26                $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
     27                remove_filter( 'theme_root', array( $this, '_theme_root' ) );
     28                remove_filter( 'stylesheet_root', array( $this, '_theme_root' ) );
     29                remove_filter( 'template_root', array( $this, '_theme_root' ) );
     30                wp_clean_themes_cache();
     31                unset( $GLOBALS['wp_themes'] );
     32                parent::tearDown();
     33        }
     34
     35        /**
     36         * Replace the normal theme root dir with our pre-made test dir
     37         */
     38        function _theme_root() {
     39                return $this->theme_root;
     40        }
    841
    942        /**
    1043         * @ticket 35961
    class Tests_L10n extends WP_UnitTestCase { 
    4477                $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) );
    4578        }
    4679
     80        public function _set_locale_to_german() {
     81                return 'de_DE';
     82        }
     83
     84        /**
     85         * @ticket 34114
     86         */
     87        public function test_load_plugin_textdomain_just_in_time() {
     88                add_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     89
     90                require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     91
     92                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-plugin' );
     93                $expected_output             = i18n_plugin_test();
     94                $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-plugin' );
     95
     96                remove_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     97
     98                $this->assertFalse( $is_textdomain_loaded_before );
     99                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output );
     100                $this->assertTrue( $is_textdomain_loaded_after );
     101        }
     102
     103        /**
     104         * @ticket 34114
     105         */
     106        public function test_load_theme_textdomain_just_in_time() {
     107                add_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     108
     109                switch_theme( 'internationalized-theme' );
     110
     111                include_once( get_stylesheet_directory() . '/functions.php' );
     112
     113                $theme_name                  = wp_get_theme()->get( 'Name' );
     114                $theme_validated             = validate_current_theme();
     115                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-theme' );
     116                $expected_output             = i18n_theme_test();
     117
     118                $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-theme' );
     119
     120                remove_filter( 'locale', array( $this, '_set_locale_to_german' ) );
     121
     122                $this->assertSame( 'Internationalized Theme', $theme_name );
     123                $this->assertTrue( $theme_validated );
     124                $this->assertFalse( $is_textdomain_loaded_before );
     125                $this->assertSame( 'Das ist ein Dummy Theme', $expected_output );
     126                $this->assertTrue( $is_textdomain_loaded_after );
     127        }
     128
    47129        /**
    48130         * @ticket 35073
    49131         */
  • 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);