Ticket #34114: 34114.2.diff
File 34114.2.diff, 10.9 KB (added by , 10 years ago) |
---|
-
src/wp-includes/l10n.php
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php index a69a718..289e49a 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 @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 */ 796 function 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 /** 783 827 * Return the Translations instance for a text domain. 784 828 * 785 829 * If there isn't one, returns empty Translations instance. … … function load_child_theme_textdomain( $domain, $path = false ) { 793 837 */ 794 838 function get_translations_for_domain( $domain ) { 795 839 global $l10n; 796 if ( ! isset( $l10n[$domain]) ) {797 $l10n[ $domain] = new NOOP_Translations;840 if ( ! isset( $l10n[ $domain ] ) && ! load_textdomain_just_in_time( $domain ) ) { 841 $l10n[ $domain ] = new NOOP_Translations; 798 842 } 799 return $l10n[ $domain];843 return $l10n[ $domain ]; 800 844 } 801 845 802 846 /** -
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/pomo/internationalized-plugin-de_DE.po
diff --git tests/phpunit/data/pomo/internationalized-plugin-de_DE.mo tests/phpunit/data/pomo/internationalized-plugin-de_DE.mo new file mode 100644 index 0000000..59c285a Binary files /dev/null and tests/phpunit/data/pomo/internationalized-plugin-de_DE.mo differ diff --git tests/phpunit/data/pomo/internationalized-plugin-de_DE.po tests/phpunit/data/pomo/internationalized-plugin-de_DE.po new file mode 100644 index 0000000..f3d7689
- + 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-01-03 12:53+0100\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/pomo/internationalized-theme-de_DE.po
diff --git tests/phpunit/data/pomo/internationalized-theme-de_DE.mo tests/phpunit/data/pomo/internationalized-theme-de_DE.mo new file mode 100644 index 0000000..f0f740e Binary files /dev/null and tests/phpunit/data/pomo/internationalized-theme-de_DE.mo differ diff --git tests/phpunit/data/pomo/internationalized-theme-de_DE.po tests/phpunit/data/pomo/internationalized-theme-de_DE.po new file mode 100644 index 0000000..44a28a6
- + 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-01-03 12:53+0100\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/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 */ -
tests/phpunit/tests/l10n.php
diff --git tests/phpunit/tests/l10n.php tests/phpunit/tests/l10n.php index 62357f0..b2611c7 100644
5 5 * @group i18n 6 6 */ 7 7 class Tests_L10n extends WP_UnitTestCase { 8 9 8 function test_load_unload_textdomain() { 10 9 $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) ); 11 10 $this->assertFalse( unload_textdomain( 'wp-tests-domain' ) ); … … class Tests_L10n extends WP_UnitTestCase { 17 16 $this->assertFalse( is_textdomain_loaded( 'wp-tests-domain' ) ); 18 17 } 19 18 19 public function _set_locale_to_german() { 20 return 'de_DE'; 21 } 22 23 /** 24 * @ticket 34114 25 */ 26 public function test_load_plugin_textdomain_just_in_time() { 27 add_filter( 'locale', array( $this, '_set_locale_to_german' ) ); 28 29 $this->_copy_file( DIR_TESTDATA . '/pomo/internationalized-plugin-de_DE.mo', WP_CONTENT_DIR . '/languages/plugins' ); 30 $this->_copy_file( DIR_TESTDATA . '/pomo/internationalized-plugin-de_DE.po', WP_CONTENT_DIR . '/languages/plugins' ); 31 $this->_copy_file( DIR_TESTDATA . '/plugins/internationalized-plugin.php', WP_CONTENT_DIR . '/plugins' ); 32 33 activate_plugin( 'internationalized-plugin.php' ); 34 $this->assertTrue( is_plugin_active( 'internationalized-plugin.php' ) ); 35 $this->assertFalse( is_textdomain_loaded( 'internationalized-plugin' ) ); 36 37 $this->assertTrue( function_exists( 'i18n_plugin_test' ) ); 38 39 $this->assertSame( 'Das ist ein Dummy Plugin', i18n_plugin_test() ); 40 $this->assertTrue( is_textdomain_loaded( 'internationalized-plugin' ) ); 41 42 remove_filter( 'locale', array( $this, '_set_locale_to_german' ) ); 43 } 44 45 /** 46 * @ticket 34114 47 */ 48 public function test_load_theme_textdomain_just_in_time() { 49 add_filter( 'locale', array( $this, '_set_locale_to_german' ) ); 50 51 $this->_copy_file( DIR_TESTDATA . '/pomo/internationalized-theme-de_DE.mo', WP_CONTENT_DIR . '/languages/themes' ); 52 $this->_copy_file( DIR_TESTDATA . '/pomo/internationalized-theme-de_DE.po', WP_CONTENT_DIR . '/languages/themes' ); 53 $this->_copy_file( DIR_TESTDATA . '/themedir1/internationalized-theme', WP_CONTENT_DIR . '/themes/internationalized-theme' ); 54 55 switch_theme( 'internationalized-theme' ); 56 $this->assertSame( 'Internationalized Theme', wp_get_theme()->get( 'Name' ) ); 57 $this->assertFalse( is_textdomain_loaded( 'internationalized-theme' ) ); 58 $this->assertTrue( validate_current_theme() ); 59 60 include_once( get_stylesheet_directory() . '/functions.php' ); 61 62 $this->assertTrue( function_exists( 'i18n_theme_test' ) ); 63 64 $this->assertSame( 'Das ist ein Dummy Theme', i18n_theme_test() ); 65 $this->assertTrue( is_textdomain_loaded( 'internationalized-theme' ) ); 66 67 remove_filter( 'locale', array( $this, '_set_locale_to_german' ) ); 68 } 69 20 70 /** 21 71 * @ticket 35073 22 72 */ … … class Tests_L10n extends WP_UnitTestCase { 26 76 $this->assertEquals( 'first-before-bar|second-before-bar', before_last_bar( 'first-before-bar|second-before-bar|after-last-bar' ) ); 27 77 } 28 78 79 /** 80 * Copy file or directory from one place to another. 81 * 82 * @access private 83 * 84 * @param string $source 85 * @param string $destination 86 */ 87 private function _copy_file( $source, $destination ) { 88 if ( ! is_dir( $destination ) ) { 89 mkdir( $destination, 0777, true ); 90 } 91 92 if ( is_dir( $source ) ) { 93 $files_to_move = array(); 94 if ( $source_dir = opendir( $source ) ) { 95 while ( false !== $file = readdir( $source_dir ) ) { 96 if ( 0 !== strpos( $file, '.' ) ) { 97 $files_to_move[] = $file; 98 } 99 } 100 } 101 102 @closedir( $source_dir ); 103 104 foreach ( $files_to_move as $file_to_move ) { 105 @unlink( $destination . '/' . $file_to_move ); 106 copy( $source . '/' . $file_to_move, $destination . '/' . $file_to_move ); 107 chmod( $destination . '/' . $file_to_move, 0777 ); 108 } 109 } else { 110 @unlink( $destination . '/' . basename( $source ) ); 111 copy( $source, $destination . '/' . basename( $source ) ); 112 chmod( $destination . '/' . basename( $source ), 0777 ); 113 } 114 } 115 29 116 } -
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);