WordPress.org

Make WordPress Core

Ticket #34114: 34114.5.diff

File 34114.5.diff, 9.8 KB (added by ocean90, 5 years ago)
  • src/wp-includes/l10n.php

     
    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 `load_plugin_texdomain()` or
     787 * `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        // Short-circuit if domain is 'default' which is reserved for core.
     800        if ( 'default' === $domain ) {
     801                return false;
     802        }
     803
     804        if ( null === $cached_mofiles ) {
     805                $cached_mofiles = array();
     806
     807                $locations = array(
     808                        WP_LANG_DIR . '/plugins',
     809                        WP_LANG_DIR . '/themes',
     810                );
     811
     812                foreach ( $locations as $location ) {
     813                        foreach ( get_available_languages( $location ) as $file ) {
     814                                $cached_mofiles[] = "{$location}/{$file}.mo";
     815                        }
     816                }
     817        }
     818
     819        $locale = get_locale();
     820        $mofile = "{$domain}-{$locale}.mo";
     821
     822        if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
     823                return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
     824        }
     825
     826        if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
     827                return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
     828        }
     829
     830        return false;
     831}
     832
     833/**
    783834 * Return the Translations instance for a text domain.
    784835 *
    785836 * If there isn't one, returns empty Translations instance.
     
    793844 */
    794845function get_translations_for_domain( $domain ) {
    795846        global $l10n;
    796         if ( isset( $l10n[ $domain ] ) ) {
     847        if ( isset( $l10n[ $domain ] ) || load_textdomain_just_in_time( $domain ) ) {
    797848                return $l10n[ $domain ];
    798849        }
    799850
  • tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po

     
     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"
  • tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po

     
     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"
  • tests/phpunit/data/plugins/internationalized-plugin.php

     
     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}
  • tests/phpunit/data/themedir1/internationalized-theme/functions.php

     
     1<?php
     2/**
     3 * Dummy theme.
     4 */
     5
     6function i18n_theme_test() {
     7        return __( 'This is a dummy theme', 'internationalized-theme' );
     8}
  • tests/phpunit/data/themedir1/internationalized-theme/index.php

     
     1<?php
     2/**
     3 * Dummy theme.
     4 */
  • tests/phpunit/data/themedir1/internationalized-theme/style.css

     
     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/loadTextdomainJustInTime.php

     
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase {
     8
     9        private $orig_theme_dir;
     10        private $theme_root;
     11
     12        function setUp() {
     13                parent::setUp();
     14
     15                $this->theme_root = DIR_TESTDATA . '/themedir1';
     16                $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
     17
     18                // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
     19                $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
     20                add_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
     21                add_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
     22                add_filter( 'template_root', array( $this, 'filter_theme_root' ) );
     23                wp_clean_themes_cache();
     24                unset( $GLOBALS['wp_themes'] );
     25        }
     26
     27        function tearDown() {
     28                $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
     29                remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
     30                remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
     31                remove_filter( 'template_root', array( $this, 'filter_theme_root' ) );
     32                wp_clean_themes_cache();
     33                unset( $GLOBALS['wp_themes'] );
     34
     35                parent::tearDown();
     36        }
     37
     38        /**
     39         * Replace the normal theme root dir with our pre-made test dir.
     40         */
     41        public function filter_theme_root() {
     42                return $this->theme_root;
     43        }
     44
     45        public function filter_set_locale_to_german() {
     46                return 'de_DE';
     47        }
     48
     49        /**
     50         * @ticket 34114
     51         */
     52        public function test_plugin_translation_should_be_translated_without_calling_load_plugin_textdomain() {
     53                add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     54
     55                require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php';
     56
     57                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-plugin' );
     58                $expected_output             = i18n_plugin_test();
     59                $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-plugin' );
     60
     61                remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     62
     63                $this->assertFalse( $is_textdomain_loaded_before );
     64                $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output );
     65                $this->assertTrue( $is_textdomain_loaded_after );
     66        }
     67
     68        /**
     69         * @ticket 34114
     70         */
     71        public function test_theme_translation_should_be_translated_without_calling_load_theme_textdomain() {
     72                add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     73
     74                switch_theme( 'internationalized-theme' );
     75
     76                include_once get_stylesheet_directory() . '/functions.php';
     77
     78                $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-theme' );
     79                $expected_output             = i18n_theme_test();
     80                $is_textdomain_loaded_after  = is_textdomain_loaded( 'internationalized-theme' );
     81
     82                remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );
     83
     84                $this->assertFalse( $is_textdomain_loaded_before );
     85                $this->assertSame( 'Das ist ein Dummy Theme', $expected_output );
     86                $this->assertTrue( $is_textdomain_loaded_after );
     87        }
     88}
  • tests/phpunit/tests/theme/themeDir.php

     
    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);