Ticket #45103: 45103.7.diff
File 45103.7.diff, 13.0 KB (added by , 5 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
478 478 } 479 479 480 480 /** 481 * Registera translation textdomain.481 * Sets a translation textdomain. 482 482 * 483 483 * @since 5.0.0 484 484 * -
src/wp-includes/functions.wp-scripts.php
193 193 } 194 194 195 195 /** 196 * Registertranslated strings for a script.196 * Sets translated strings for a script. 197 197 * 198 198 * Works only if the script has already been added. 199 199 * 200 200 * @see WP_Scripts::set_translations() 201 * @link https://core.trac.wordpress.org/ticket/45103202 201 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 203 202 * 204 203 * @since 5.0.0 -
src/wp-includes/l10n.php
876 876 * @link https://core.trac.wordpress.org/ticket/45103 877 877 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. 878 878 * 879 * @since 5.0.0 880 * 879 881 * @param string $handle Name of the script to register a translation domain to. 880 882 * @param string $domain The textdomain. 881 883 * @param string $path Optional. The full file path to the directory containing translation files. … … 889 891 $locale = is_admin() ? get_locale() : get_user_locale(); 890 892 891 893 // If a path was given and the handle file exists simply return it. 892 $handle_filename = $domain . '-' . $locale . '-' . $handle . '.json'; 894 $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; 895 $handle_filename = $file_base . '-' . $handle . '.json'; 893 896 if ( $path && file_exists( $path . '/' . $handle_filename ) ) { 894 897 return file_get_contents( $path . '/' . $handle_filename ); 895 898 } … … 908 911 909 912 // If the host is the same or it's a relative URL. 910 913 if ( 911 strpos( $ content_url['path'], $src_url['path'] ) === 0 &&914 strpos( $src_url['path'], $content_url['path'] ) === 0 && 912 915 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] ) 913 916 ) { 914 917 // Make the src relative the specific plugin or theme. … … 925 928 ) { 926 929 $relative = trim( $src_url['path'], '/' ); 927 930 } else if ( 928 ( strpos( $s ite_url['path'], $src_url['path'] ) === 0 ) &&931 ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) && 929 932 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) 930 933 ) { 931 934 // Make the src relative to the WP root. … … 943 946 $relative = substr( $relative, 0, -7 ) . '.js'; 944 947 } 945 948 946 $md5_filename = $ domain . '-' . $locale . '-' . md5( $relative ) . '.json';949 $md5_filename = $file_base . '-' . md5( $relative ) . '.json'; 947 950 if ( $path && file_exists( $path . '/' . $md5_filename ) ) { 948 951 return file_get_contents( $path . '/' . $md5_filename ); 949 952 } -
tests/phpunit/data/languages/admin-en_US-script-handle.json
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 file is a translation for script-handle.": [ 13 "This file is a translation for script-handle." 14 ] 15 } 16 } 17 } -
tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json
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 file is too big. Files must be less than %d KB in size.": [13 "This file is too big. Files must be less than %d KB in size."14 ],15 "%d Theme Update": [16 "%d Theme Update",17 "%d Theme Updates"18 ],19 "password strength\u0004Medium": [20 "Medium"21 ],22 "taxonomy singular name\u0004Category": [23 "Category"24 ],25 "post type general name\u0004Pages": [26 "Pages"27 ]28 }29 }30 } -
tests/phpunit/data/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json
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 plugin.": [ 13 "This is a dummy plugin." 14 ] 15 } 16 } 17 } -
tests/phpunit/data/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json
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
770 770 /** 771 771 * @ticket 45103 772 772 */ 773 public function test_wp_set_script_translation () {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 776 wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages/' ); … … 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 ){" . 780 780 "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" . 781 "})(" . file_get_contents( DIR_TESTDATA . '/languages/ default-en_US-813e104eb47e13dd4cc5af844c618754.json') . ");\n</script>\n";781 "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n"; 782 782 $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n"; 783 783 784 784 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); … … 785 785 } 786 786 787 787 /** 788 * @ticket 45103 789 */ 790 public function test_wp_set_script_translations_for_plugin() { 791 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 792 wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null ); 793 wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins/' ); 794 795 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 796 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . 797 "wp.i18n.setLocaleData( translations.locale_data, \"internationalized-plugin\" );" . 798 "})(" . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n"; 799 $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n"; 800 801 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 802 } 803 804 /** 805 * @ticket 45103 806 */ 807 public function test_wp_set_script_translations_for_theme() { 808 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 809 wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null ); 810 wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes/' ); 811 812 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 813 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . 814 "wp.i18n.setLocaleData( translations.locale_data, \"internationalized-theme\" );" . 815 "})(" . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n"; 816 $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n"; 817 818 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 819 } 820 821 /** 822 * @ticket 45103 823 */ 824 public function test_wp_set_script_translations_with_handle_file() { 825 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 826 wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null ); 827 wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' ); 828 829 $expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>"; 830 $expected .= "\n<script type='text/javascript'>\n(function( translations ){" . 831 "wp.i18n.setLocaleData( translations.locale_data, \"admin\" );" . 832 "})(" . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n"; 833 $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n"; 834 835 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 836 } 837 838 /** 839 * @ticket 45103 840 */ 841 public function test_wp_set_script_translations_i18n_dependency() { 842 global $wp_scripts; 843 844 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 845 wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null ); 846 wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages/' ); 847 848 $script = $wp_scripts->registered[ 'test-example' ]; 849 850 $this->assertContains( 'wp-i18n', $script->deps ); 851 } 852 853 /** 854 * @ticket 45103 855 */ 856 public function test_wp_set_script_translations_when_translation_file_does_not_exist() { 857 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 858 wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null ); 859 wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' ); 860 861 $expected = "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n"; 862 863 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 864 } 865 866 /** 867 * @ticket 45103 868 */ 869 public function test_wp_set_script_translations_before_enqueue() { 870 $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, array( 'wp_set_script_translations' ) ); 871 wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null ); 872 wp_register_script( 'test-example', '/wp-admin/js/script.js', array(), null ); 873 wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' ); 874 } 875 876 /** 788 877 * Testing `wp_enqueue_code_editor` with file path. 789 878 * 790 879 * @ticket 41871