Ticket #45488: 45488.diff
File 45488.diff, 11.0 KB (added by , 6 years ago) |
---|
-
src/wp-includes/l10n.php
diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php index def655b414..37b7e492c1 100644
function load_child_theme_textdomain( $domain, $path = false ) { 888 888 function load_script_textdomain( $handle, $domain, $path = null ) { 889 889 global $wp_scripts; 890 890 891 $path = untrailingslashit( $path ); 892 $locale = determine_locale(); 893 894 // If a path was given and the handle file exists simply return it. 895 $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; 896 $handle_filename = $file_base . '-' . $handle . '.json'; 897 if ( $path && file_exists( $path . '/' . $handle_filename ) ) { 898 return file_get_contents( $path . '/' . $handle_filename ); 899 } 891 $path = untrailingslashit( $path ); 892 $locale = determine_locale(); 893 $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; 900 894 901 895 $obj = $wp_scripts->registered[ $handle ]; 902 896 … … function load_script_textdomain( $handle, $domain, $path = null ) { 933 927 } 934 928 } 935 929 936 // If the source is not from WP.937 if ( false === $relative ) {938 return false;939 }940 941 930 // Translations are always based on the unminified filename. 942 if ( substr( $relative, -7 ) === '.min.js' ) {931 if ( $relative && substr( $relative, -7 ) === '.min.js' ) { 943 932 $relative = substr( $relative, 0, -7 ) . '.js'; 944 933 } 945 934 946 $md5_filename = $file_base . '-' . md5( $relative ) . '.json'; 947 if ( $path && file_exists( $path . '/' . $md5_filename ) ) { 948 return file_get_contents( $path . '/' . $md5_filename ); 935 $handle_filename = $file_base . '-' . $handle . '.json'; 936 937 $md5_filename = false; 938 939 if ( $relative ) { 940 $md5_filename = $file_base . '-' . md5( $relative ) . '.json'; 949 941 } 950 if ( file_exists( $languages_path . '/' . $md5_filename ) ) { 942 943 if ( $md5_filename && file_exists( $languages_path . '/' . $md5_filename ) ) { 951 944 return file_get_contents( $languages_path . '/' . $md5_filename ); 952 945 } 953 946 947 if ( $path ) { 948 if ( file_exists( $path . '/' . $handle_filename ) ) { 949 return file_get_contents( $path . '/' . $handle_filename ); 950 } 951 952 if ( $md5_filename && file_exists( $path . '/' . $md5_filename ) ) { 953 return file_get_contents( $path . '/' . $md5_filename ); 954 } 955 } 956 954 957 return false; 955 958 } 956 959 -
new file tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json
diff --git tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json new file mode 100644 index 0000000000..280ccc1424
- + 1 { 2 "translation-revision-data": "+0000", 3 "generator": "GlotPress/2.3.0-alpha", 4 "domain": "messages", 5 "locale_data": { 6 "messages": { 7 "": { 8 "domain": "messages", 9 "plural-forms": "n != 1", 10 "lang": "en-gb" 11 }, 12 "This is a dummy theme.": [ 13 "This is a dummy theme." 14 ] 15 } 16 } 17 } -
new file tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json
diff --git tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json new file mode 100644 index 0000000000..280ccc1424
- + 1 { 2 "translation-revision-data": "+0000", 3 "generator": "GlotPress/2.3.0-alpha", 4 "domain": "messages", 5 "locale_data": { 6 "messages": { 7 "": { 8 "domain": "messages", 9 "plural-forms": "n != 1", 10 "lang": "en-gb" 11 }, 12 "This is a dummy theme.": [ 13 "This is a dummy theme." 14 ] 15 } 16 } 17 } -
tests/phpunit/tests/dependencies/scripts.php
diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php index 6b84a81ae7..07c60b4094 100644
class Tests_Dependencies_Scripts extends WP_UnitTestCase { 773 773 public function test_wp_set_script_translations() { 774 774 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 775 775 wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null ); 776 wp_set_script_translations( 'test-example', 'default' , DIR_TESTDATA . '/languages');776 wp_set_script_translations( 'test-example', 'default' ); 777 777 778 778 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 779 779 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 791 791 public function test_wp_set_script_translations_for_plugin() { 792 792 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 793 793 wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null ); 794 wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' );794 wp_set_script_translations( 'plugin-example', 'internationalized-plugin', WP_LANG_DIR . '/plugins' ); 795 795 796 796 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 797 797 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 809 809 public function test_wp_set_script_translations_for_theme() { 810 810 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 811 811 wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null ); 812 wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' );812 wp_set_script_translations( 'theme-example', 'internationalized-theme', WP_LANG_DIR . '/themes' ); 813 813 814 814 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 815 815 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 821 821 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 822 822 } 823 823 824 /** 825 * @ticket 45488 826 */ 827 public function test_wp_set_script_translations_for_theme_in_theme_dir_md5() { 828 $theme_dir = $GLOBALS['wp_theme_directories'][0]; 829 830 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 831 wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null ); 832 wp_set_script_translations( 'theme-example', 'internationalized-theme', $theme_dir . '/internationalized-theme/languages' ); 833 834 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 835 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . 836 "translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" . 837 "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" . 838 "})(" . file_get_contents( $theme_dir . '/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json' ) . ");\n</script>\n"; 839 $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n"; 840 841 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 842 } 843 844 /** 845 * @ticket 45488 846 */ 847 public function test_wp_set_script_translations_for_theme_in_theme_dir_handle() { 848 $theme_dir = $GLOBALS['wp_theme_directories'][0]; 849 850 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 851 wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/another-script.js', array(), null ); 852 wp_set_script_translations( 'theme-example', 'internationalized-theme', $theme_dir . '/internationalized-theme/languages' ); 853 854 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 855 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . 856 "translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" . 857 "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" . 858 "})(" . file_get_contents( $theme_dir . '/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json' ) . ");\n</script>\n"; 859 $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/another-script.js'></script>\n"; 860 861 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 862 } 863 824 864 /** 825 865 * @ticket 45103 826 866 */ 827 867 public function test_wp_set_script_translations_with_handle_file() { 828 868 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 829 869 wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null ); 830 wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/');870 wp_set_script_translations( 'script-handle', 'admin', WP_LANG_DIR ); 831 871 832 872 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 833 873 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 847 887 848 888 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 849 889 wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null ); 850 wp_set_script_translations( 'test-example', 'default' , DIR_TESTDATA . '/languages/');890 wp_set_script_translations( 'test-example', 'default' ); 851 891 852 892 $script = $wp_scripts->registered[ 'test-example' ]; 853 893 … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 878 918 public function test_wp_set_script_translations_after_register() { 879 919 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 880 920 wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null ); 881 wp_set_script_translations( 'test-example', 'default' , DIR_TESTDATA . '/languages');921 wp_set_script_translations( 'test-example', 'default' ); 882 922 883 923 wp_enqueue_script( 'test-example' ); 884 924 … … class Tests_Dependencies_Scripts extends WP_UnitTestCase { 898 938 public function test_wp_set_script_translations_dependency() { 899 939 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 900 940 wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null ); 901 wp_set_script_translations( 'test-dependency', 'default' , DIR_TESTDATA . '/languages');941 wp_set_script_translations( 'test-dependency', 'default' ); 902 942 903 943 wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null ); 904 944