From 0151b0ebae3566ce473a1946d510c82058ccbd8b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 10 May 2016 20:04:52 +0000 Subject: [PATCH] I18N: Remove the requirement to call `load_plugin_textdomain()` / `load_theme_textdomain()`. By initially scanning the `wp-content/languages` directory and loading available MO files just-in-time, plugins and themes do not need to manually load text domains anymore. Props swissspidy, ocean90. Fixes #34114 git-svn-id: https://develop.svn.wordpress.org/trunk@37415 602fd350-edb4-49c9-b593-d223f7449a82 --- .../plugins/internationalized-plugin-de_DE.mo | Bin 0 -> 717 bytes .../plugins/internationalized-plugin-de_DE.po | 23 +++++ .../themes/internationalized-theme-de_DE.mo | Bin 0 -> 715 bytes .../themes/internationalized-theme-de_DE.po | 23 +++++ .../data/plugins/internationalized-plugin.php | 12 +++ .../internationalized-theme/functions.php | 8 ++ .../internationalized-theme/index.php | 4 + .../internationalized-theme/style.css | 7 ++ .../tests/l10n/loadTextdomainJustInTime.php | 88 ++++++++++++++++++ tests/phpunit/tests/theme/themeDir.php | 1 + 10 files changed, 166 insertions(+) create mode 100644 tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo create mode 100644 tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po create mode 100644 tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo create mode 100644 tests/phpunit/data/languages/themes/internationalized-theme-de_DE.po create mode 100644 tests/phpunit/data/plugins/internationalized-plugin.php create mode 100644 tests/phpunit/data/themedir1/internationalized-theme/functions.php create mode 100644 tests/phpunit/data/themedir1/internationalized-theme/index.php create mode 100644 tests/phpunit/data/themedir1/internationalized-theme/style.css create mode 100644 tests/phpunit/tests/l10n/loadTextdomainJustInTime.php diff --git a/tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo b/tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.mo new file mode 100644 index 0000000000000000000000000000000000000000..e789268e619b399bcf90995cdad1f166e1f8acb9 GIT binary patch literal 717 zcmYjP!EV$r5M2;l_Q(No;4s`kU57ZktsBo$nT9lqBTS<=sqL3dl9g$?2N@@T`%beTDI1ODV5#`h`%d8{l-3^M!VB$uv8v+5C;odNe9YK zq{CMFN(0uWof+FW0h+8O>XuQ`yG-HowT4$q;BS(#{{pwS#+L3YXBA4D<+N=*N-_*Ns$l{p24~OjTq=$xk yPC7GkuQUz0$F94!HI`&<(T#s_jgoFJB@bBGyD(YF{)Kr!rC~KZGBT!rWd8sc3)Id4 literal 0 HcmV?d00001 diff --git a/tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po b/tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po new file mode 100644 index 0000000000..b1fff26113 --- /dev/null +++ b/tests/phpunit/data/languages/plugins/internationalized-plugin-de_DE.po @@ -0,0 +1,23 @@ +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: 2015-12-31 16:31+0100\n" +"PO-Revision-Date: 2016-04-28 18:50+0200\n" +"Last-Translator: Pascal Birchler \n" +"Language-Team: \n" +"Language: de_DE\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.8.4\n" +"X-Poedit-Basepath: .\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;" +"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;" +"esc_html_x:1,2c\n" +"X-Textdomain-Support: yes\n" +"X-Poedit-SearchPath-0: .\n" + +#: internationalized-plugin.php:11 +msgid "This is a dummy plugin" +msgstr "Das ist ein Dummy Plugin" diff --git a/tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo b/tests/phpunit/data/languages/themes/internationalized-theme-de_DE.mo new file mode 100644 index 0000000000000000000000000000000000000000..cfd2b5a27ea6d4177c8723deac7f63f519d415b9 GIT binary patch literal 715 zcmY*X!EV$r5M2;l_K1YIa2Rf&u0x#NR*}<1sNF59+KNB};ue`?*cc^tupQ`r2w%eg z@GVTTyN$5&~v=(uVeDgg?X|(B9Ym zx3Wq(phrU4HO&@$g$+(pYrbRQ*=5N-qjkz?0n_w-#?CEDPm-;qM*&gDj~S2HoC7}* z^Y|#_VMvMWBmPu<*_e02!Uc82&d;pd(?E*wk6LV@BHhw zmP#j3RMI;1%TL(_i^sg)(l@khtqW5sy%7+9Ret(~y+e(5@6}+bJn$e67IcyhlATG1 zt@M?EV4BvQl{LFCws8VYXBA4Da~jU}5~bmJG?qNKl\n" +"Language-Team: \n" +"Language: de_DE\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.8.4\n" +"X-Poedit-Basepath: .\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;" +"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;" +"esc_html_x:1,2c\n" +"X-Textdomain-Support: yes\n" +"X-Poedit-SearchPath-0: .\n" + +#: functions.php:7 +msgid "This is a dummy theme" +msgstr "Das ist ein Dummy Theme" diff --git a/tests/phpunit/data/plugins/internationalized-plugin.php b/tests/phpunit/data/plugins/internationalized-plugin.php new file mode 100644 index 0000000000..4b56846ad3 --- /dev/null +++ b/tests/phpunit/data/plugins/internationalized-plugin.php @@ -0,0 +1,12 @@ +theme_root = DIR_TESTDATA . '/themedir1'; + $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + add_filter( 'theme_root', array( $this, 'filter_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_theme_root' ) ); + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + } + + function tearDown() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_theme_root' ) ); + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + + parent::tearDown(); + } + + /** + * Replace the normal theme root dir with our pre-made test dir. + */ + public function filter_theme_root() { + return $this->theme_root; + } + + public function filter_set_locale_to_german() { + return 'de_DE'; + } + + /** + * @ticket 34114 + */ + public function test_plugin_translation_should_be_translated_without_calling_load_plugin_textdomain() { + add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); + + require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php'; + + $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-plugin' ); + $expected_output = i18n_plugin_test(); + $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-plugin' ); + + remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); + + $this->assertFalse( $is_textdomain_loaded_before ); + $this->assertSame( 'Das ist ein Dummy Plugin', $expected_output ); + $this->assertTrue( $is_textdomain_loaded_after ); + } + + /** + * @ticket 34114 + */ + public function test_theme_translation_should_be_translated_without_calling_load_theme_textdomain() { + add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); + + switch_theme( 'internationalized-theme' ); + + include_once get_stylesheet_directory() . '/functions.php'; + + $is_textdomain_loaded_before = is_textdomain_loaded( 'internationalized-theme' ); + $expected_output = i18n_theme_test(); + $is_textdomain_loaded_after = is_textdomain_loaded( 'internationalized-theme' ); + + remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) ); + + $this->assertFalse( $is_textdomain_loaded_before ); + $this->assertSame( 'Das ist ein Dummy Theme', $expected_output ); + $this->assertTrue( $is_textdomain_loaded_after ); + } +} diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index d5060f8ede..a6df7ba42e 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -159,6 +159,7 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase { 'My Subdir Theme',// theme in a subdirectory should work 'Page Template Theme', // theme with page templates for other test code 'Theme with Spaces in the Directory', + 'Internationalized Theme', ); sort($theme_names);