Support » Alpha/Beta/RC » Duplicate/unwanted loading of plugin textdomain

  • In WP version 4.6-alpha-37590

    I am the author of various plugins.
    One of my plugins ( https://wordpress.org/support/plugin/wp-photo-album-plus ) has the ability to prevent the loading of the plugins textdomain. This is on a special request from a user of the plugin.
    So, i made the option to skip the load as follows:

    // Do we want text domain loaded? Hans Wahlgren does not.
    if ( get_option( 'wppa_use_language', 'yes' ) == 'yes' ) {
    	$bret = load_plugin_textdomain( 'wp-photo-album-plus', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
    }

    For all languages that have a language file in …/wp-content/languages/ provided by the polyglot system, this file will be
    1. updated to the site automaticly so it use useless to remove it.
    2. loaded by load_textdomain() regardless of my switch.

    It is even worse, when the switch is on, the language file is loaded twice, if the file exists in …/wp-content/languages/

Viewing 15 replies - 1 through 15 (of 16 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Honestly, this would be a case where you’re the wrong person to fix the issue, but I don’t know who is… I’ve brought it up with the #meta-i18n folks, but my gut feeling is you shouldn’t be doing this at all. Hans is asking you to circumvent WP core features that are unrelated to your plugin (you’re photos, that’s translations).

    I don’t know if anyone would have a solution, but this would be something I would be personally unwilling to support in any of my plugins.

    Dominik Schilling (ocean90)

    (@ocean90)

    WordPress Core Developer

    If you don’t want to load a text domain you have to use the override_load_textdomain filter which returns true for the text domain, see https://developer.wordpress.org/reference/hooks/override_load_textdomain/.

    If load_plugin_textdomain() is the function to load a plugin text domain, one would expect that when you leave it out, the text domain would not be loaded. Or not?

    @dominik: Thanx for the link. Unfortunately the new style documentation gives so little information

    What about the duplicate loading? Will that be fixed in the release version?

    I would say that the override_load_textdomain function Dominik mentioned is the way to go here.

    But can you explain why Hans didn’t want the translation to be loaded?

    We, in Eurpe, like Hans in Sweden, and Hans his users, are very much used to communicate in English. It is like a 2nd language for us.
    Before there was a swedish translation for wppa, he wrote a manual for his users how to do a lot of things. He used the english terminology of wppa. No big deal for his users to continue using this plugin in the english language. When the swedish translation was there ( far befor the new polyglot system ), it was even easier for me to implement that switch, than it would be for hom to remove the swedish language files from ../wp-photo-album-plus/languages/ after every update. So, that kind of sevice i am used to give my users. On the Reviews screen you can read how much my users appreciate the support i give them.

    I tried this:

    // Load language
    add_action( 'plugins_loaded', 'wppa_load_plugin_textdomain' );
    
    function wppa_load_plugin_textdomain() {
    global $wppa_lang;
    global $wppa_locale;
    
    	// Do we want text domain loaded? Hand Wahlgren does not.
    	if ( get_option( 'wppa_use_language', 'yes' ) == 'yes' ) {
    		$bret = load_plugin_textdomain( 'wp-photo-album-plus', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
    	}
    	else {
    		apply_filters( 'override_load_textdomain', true, 'wp-photo-album-plus', '' );
    	}
    	$wppa_locale = get_locale() ? get_locale() : 'en_US';
    	$wppa_lang = substr( $wppa_locale, 0, 2 );
    }

    But the language file is still loaded by load_textdomain() according to query_monitor:

    wp-photo-album-plus load_textdomain() wp-includes/l10n.php:827	wp-content/languages/plugins/wp-photo-album-plus-nl_NL.mo	373 kB

    If load_plugin_textdomain() is the function to load a plugin text domain, one would expect that when you leave it out, the text domain would not be loaded. Or not?

    Is load_plugin_textdomain() going to be deprecated / phased out?

    Dominik Schilling (ocean90)

    (@ocean90)

    WordPress Core Developer

    @opajaap Please take a look at https://developer.wordpress.org/plugins/hooks/filters/ on how to use actions/filters.

    @dominik Thank you, i know how filters and actions work.

    I can imagine why override_load_textdomain does not work, i do not supply a .mo file to override.
    So i tried unload_textdomain(). It turns out that in 4.6-alpha it returns true while the domain is not unloaded.

    On WP version 4.5.2, with the language set to ‘Nederlands’ ( nl_NL )
    This code snippet on a normal wp page:

    echo "__('Slideshow', 'wp-photo-album-plus' ) prints:".__('Slideshow', 'wp-photo-album-plus' ).'<br />';
    echo "Doing: unload_textdomain( 'wp-photo-album-plus' )";
    $bret = unload_textdomain( 'wp-photo-album-plus' );
    echo ' returned:'.$bret.'<br />';
    echo "__('Slideshow', 'wp-photo-album-plus' ) prints:".__('Slideshow', 'wp-photo-album-plus' ).'<br />';

    produces this output:

    __('Slideshow', 'wp-photo-album-plus' ) prints:Diavoorstelling
    Doing: unload_textdomain( 'wp-photo-album-plus' ) returned:1
    __('Slideshow', 'wp-photo-album-plus' ) prints:Slideshow

    As expected.

    On the same testsite under exactly the same conditions after installing 4.6-alpha-37629 the output is:

    __('Slideshow', 'wp-photo-album-plus' ) prints:Diavoorstelling
    Doing: unload_textdomain( 'wp-photo-album-plus' ) returned:1
    __('Slideshow', 'wp-photo-album-plus' ) prints:Diavoorstelling

    Maybe this convinces you that there is something wrong with loading langage files in 4.6-alpha.

    Dominik Schilling (ocean90)

    (@ocean90)

    WordPress Core Developer

    i know how filters and actions work.

    Well, apply_filters( 'override_load_textdomain', true, 'wp-photo-album-plus', '' ); is definitely wrong.

    This topic is not about IF or HOW i should prevent the load of a text domain, it is about UNEXPECTED MISBEHAVIOUR of i10n in wp 4.6-alpha.

    Why does nobody reply to this point? I clearly proved it with evidence.

    If it is the policy for 4.6 that plugin developers have no more control over language files at all, it’s ok to me, but if so, please tell me, so i can document it and remove my switch and say sorry to Hans and possibly others.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Because it’s unexpected behavior of someone doing things in an unsupported way. We’re not dismissing that you have an issue, it’s just not broken.

    What you were originally doing was wrong because it just said “If someone says YES, use this language, else use whatever WP thinks it should use.” That’s not broken, that was an incorrect assumption about what happens when you don’t define a language. Many people love that they don’t have to define it anymore and WP just picks it up 🙂 It’s perfect for that.

    tl;dr there, WP is behaving as expected.

    But what you have is one person who doesn’t want to translate one plugin. I doubt that’s the case, actually, I BET he wants to not translate MOST plugins, which is why I feel you’ve got the short end of the stick and should tell him no. He’s going to ask everyone to make this change, and it’s not your business.

    tl;dr there, you’re being asked to support something that you really can say no to, and I encourage you to do so for future sanity.

    Dominik told you you were using the filter wrong, though. Which is why he pointed you back to the docs.

    1. The call you want is add_filter() not apply_filter()

    apply_filters() is the filter hook location, is called by core code, and is used to apply any filters that are added to the queue by Themes/Plugins (and core).

    add_filter() is called by Themes/Plugins (and core), and is used to add filters to the queue to be applied to the hook by core.

    2. The function we’re going to filter is override_load_textdomain (cool, got that right)

    override_load_textdomain

    3. Someone’s already done it up properly it looks like

    http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way

    Edit:

    What I think this user needs is to look at https://codex.wordpress.org/Configuring_Automatic_Background_Updates#Translation_Updates_via_Filter

    He should write a plugin of his own to say what plugins and themes he does and doesn’t want to update, and just filter it so they don’t.

    @ipstenu:

    That makes sense, thank you.

    Can you confirm the following conclusions?

    1. From 4.6 onward load_plugin_textdomain() will be no longer needed as it has become redundant.
    2. From 4.6 onward unload_textdomain() no longer works, this is a feature rather than a bug.
    3. Doc page https://developer.wordpress.org/reference/hooks/override_load_textdomain/ is faulty, it says apply_filters(), not add_filter()

    If at least 1. and 2. are true, i will leave out my switch and recommend as you suggested in yur last paragraph.

    1. From 4.6 onward load_plugin_textdomain() will be no longer needed as it has become redundant.

    That’s correct. There will be a post on make.wordpress.org/core that will highlight this change.

    2. From 4.6 onward unload_textdomain() no longer works, this is a feature rather than a bug.

    That sounds like a bug to me. I’ll have a closer look and open a ticket on trac if necessary.

    3. Doc page https://developer.wordpress.org/reference/hooks/override_load_textdomain/ is faulty, it says apply_filters(), not add_filter()

    It’s not a fault. That line doesn’t tell you how to implement this filter, it shows how this filter is being used inside core (i.e. like a function signature). You’ll find that exact line in wp-includes/l10n.php

    The same goes for functions, e.g. https://developer.wordpress.org/reference/functions/load_textdomain/ shows the signature of the load_textdomain() function.

    Thank you all for your patience with me.
    I studied the source and understand the ‘just in time’ load of the text domains; a perfect aproach.

    I now use the following constuction that works both < 4.6 as well as >= 4.6 without duplicate loading:

    // Load language
    add_action( 'init', 'wppa_load_plugin_textdomain' );
    
    function wppa_load_plugin_textdomain() {
    global $wppa_lang;
    global $wppa_locale;
    global $wp_version;
    
    	if ( $wp_version < '4.6' ) {
    		load_plugin_textdomain( 'wp-photo-album-plus', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
    	}
    
    	// 'Our' usefull language info
    	$wppa_locale = get_locale() ? get_locale() : 'en_US';
    	$wppa_lang = substr( $wppa_locale, 0, 2 );
    }

    I dropped the switch to disable the load. It is after all indeed a better idea to develop ( by someone else ) a separate plugin to select textdomains to be excluded.

    I’d like you to open a ticket for unload_textdomain().

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Duplicate/unwanted loading of plugin textdomain’ is closed to new replies.