Ticket #34114: 34114.6.diff
File 34114.6.diff, 9.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/l10n.php
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php index 6a88fbb..889b154 100644
function load_child_theme_textdomain( $domain, $path = false ) { 780 780 } 781 781 782 782 /** 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 * @access private 791 * 792 * @see get_translations_for_domain() 793 * 794 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 795 * @return bool True when the textdomain is successfully loaded, false otherwise. 796 */ 797 function _load_textdomain_just_in_time( $domain ) { 798 static $cached_mofiles = null; 799 800 // Short-circuit if domain is 'default' which is reserved for core. 801 if ( 'default' === $domain ) { 802 return false; 803 } 804 805 if ( null === $cached_mofiles ) { 806 $cached_mofiles = array(); 807 808 $locations = array( 809 WP_LANG_DIR . '/plugins', 810 WP_LANG_DIR . '/themes', 811 ); 812 813 foreach ( $locations as $location ) { 814 foreach ( get_available_languages( $location ) as $file ) { 815 $cached_mofiles[] = "{$location}/{$file}.mo"; 816 } 817 } 818 } 819 820 $locale = get_locale(); 821 $mofile = "{$domain}-{$locale}.mo"; 822 823 if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) { 824 return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ); 825 } 826 827 if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) { 828 return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ); 829 } 830 831 return false; 832 } 833 834 /** 783 835 * Return the Translations instance for a text domain. 784 836 * 785 837 * If there isn't one, returns empty Translations instance. … … function load_child_theme_textdomain( $domain, $path = false ) { 793 845 */ 794 846 function get_translations_for_domain( $domain ) { 795 847 global $l10n; 796 if ( isset( $l10n[ $domain ] ) ) {848 if ( isset( $l10n[ $domain ] ) || _load_textdomain_just_in_time( $domain ) ) { 797 849 return $l10n[ $domain ]; 798 850 } 799 851 -
new file tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po
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
- + 1 msgid "" 2 msgstr "" 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 22 msgid "This is a dummy plugin" 23 msgstr "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.po tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po new file mode 100644 index 0000000..78c90e4
- + 1 msgid "" 2 msgstr "" 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 22 msgid "This is a dummy theme" 23 msgstr "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 /* 3 Plugin Name: Dummy Plugin 4 Plugin URI: https://wordpress.org/ 5 Description: For testing purposes only. 6 Version: 1.0.0 7 Text Domain: internationalized-plugin 8 */ 9 10 function 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 6 function 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 /* 2 Theme Name: Internationalized Theme 3 Theme URI: https://wordpress.org/ 4 Description: For testing purposes only. 5 Version: 1.0.0 6 Text Domain: internationalized-theme 7 */ -
new file tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
diff --git tests/phpunit/tests/l10n/loadTextdomainJustInTime.php tests/phpunit/tests/l10n/loadTextdomainJustInTime.php new file mode 100644 index 0000000..ea43768
- + 1 <?php 2 3 /** 4 * @group l10n 5 * @group i18n 6 */ 7 class 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
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 { 159 159 'My Subdir Theme',// theme in a subdirectory should work 160 160 'Page Template Theme', // theme with page templates for other test code 161 161 'Theme with Spaces in the Directory', 162 'Internationalized Theme', 162 163 ); 163 164 164 165 sort($theme_names);