[wp-trac] [WordPress Trac] #44937: Add _doing_it_wrong to load_plugin_textdomain

WordPress Trac noreply at wordpress.org
Thu Sep 13 07:03:43 UTC 2018


#44937: Add _doing_it_wrong to load_plugin_textdomain
------------------------------+-----------------------------
 Reporter:  garrett-eclipse   |      Owner:  (none)
     Type:  enhancement       |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  I18N              |    Version:  4.7
 Severity:  normal            |   Keywords:
  Focuses:  coding-standards  |
------------------------------+-----------------------------
 Hello,

 Came across a race condition when the load_plugin_textdomain is called
 (for lack of a better term) 'naked' in plugins, most recently Github
 Updater.

 This only affects WP 4.7+ as it relates to the User Locale vs. the Site
 Locale within the admin area.

 If you call load_plugin_textdomain in your plugin outside of a init or
 plugins_loaded action then your plugin will load it's strings based on the
 site locale rather than the admin's locale.

 The Codex -
 https://codex.wordpress.org/Function_Reference/load_plugin_textdomain

 It provides an example using plugins_loaded but doesn't indicate any
 issue.

 I'm unsure the root of the issue but I assume the plugin loads prior to
 the user so their locale isn't available when the MO object is loaded into
 the $l10n global.

 I don't know if it's possible to make the user locale available to plugins
 running load_plugin_textdomain prior to init and since the issue isn't
 present prior to 4.7 feel a _doing_it_wrong prompt will help plugin
 developers avoid the pitfall.

 I haven't tried with other plugins or examples but to reproduce;
 1. Install clean WP
 2. Install GitHub Updater <8.3 - (8.2.1 - https://github.com/afragen
 /github-updater/tree/8.2.1)
 3. Set Site Locale to de_DE or fr_FR or ....
 4. Set User Locale to en_US
 5. Install all translations
 6. Visit GitHub Updater dashboard
 And you'll find it's not in en_US it's in the site locale.
 *If it's half en and half your site locale those are from missing strings
 on the site locale.

 Then if you replace with 8.3+ (https://github.com/afragen/github-
 updater/tree/develop) you'll find the panel in user locale as 8.3 fixed
 this issue by wrapping load_plugin_textdomain in a plugins_loaded action
 function.

 And I'll wrap up with a concept snippet for the load_plugin_textdomain
 function;
 {{{#!php
 <?php
 if ( ! did_action( 'init' ) && ! did_action( 'plugins_loaded' ) ) {
         _doing_it_wrong(
                 'load_plugin_textdomain',
                 sprintf(
                         /* translators: 1: function 2: init, 3:
 plugins_loaded */
                         __( 'The %1$s should be called from the %2$s or
 %3$ hooks.' ),
                         '<code>load_plugin_textdomain</code>',
                         '<code>init</code>',
                         '<code>plugins_loaded</code>'
                 ),
                 '4.7.0'
         );
 }
 }}}

 And it seems I'm not alone;
 https://status301.net/why-load_plugin_textdomain-would-not-work/

 Cheers

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/44937>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list