,

Encourage community translations for WordPress.org plugins

Plugins and themes hosted on WordPress.org are, as of December 5th(-ish) 2015, translateable through translate.wordpress.org.

This opens up for a lot of interesting new interactions for existing, and potential new users, who may not be fluent in English, and have not yet picked up your plugin or theme for this reason alone.

So, your plugin is now up there, it can be translated…but how do you encourage your users to participate and translate the plugin?
I threw together a snippet for just this purpose, it may not be for everyone, but I do believe it’s a fairly safe starting point for most people.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
function language_detector_admin_notices() {
	// Get the current language locale
	$language = get_locale();
	// Check if the nag screen has been disabled for this language
	if ( false === get_option'plugin_slug_language_detector_' . $languagefalse ) ) {
		
		// Check if a translation file already exists for this language
		if ( $loaded = load_plugin_textdomain'text_domain'falseplugin_dir_path__FILE__ ) . '/languages/' ) ) {
			// If a translation file is discovered, we disable all future nag screens for this language
			update_option'plugin_slug_language_detector_' . $languagetruetrue );
			return;
		}
		
		// We need the translation data from core to display human readable locale names
		require_onceABSPATH . 'wp-admin/includes/translation-install.php' );
		$translations = wp_get_available_translations();
		// Gather information about the plugin (in this case we really only watn the name and text domain/slug)
		$plugin = get_plugin_data__FILE__ );
		printf(
			'<div class="notice notice-info"><p><strong>%s</strong></p><p>%s</p></div>',
			esc_html$plugin['Name'] ),
			sprintf(
				__'This plugin does not have a translation for <strong>%1$s</strong>, you can help translating the plugin at <a href="%2$s" target="_blank">%2$s</a>.' ),
				$translations$language ]['native_name'], // The native name of the language currently in use
				esc_url'https://translate.wordpress.org/projects/wp-plugins/' . $plugin['TextDomain'] ) // The URL to the translation overview page for our plugin
			)
		);
	}
}
// Hook into admin notices
add_action'admin_notices''language_detector_admin_notices' );
// Output JavaScript for handling dismissals in the footer
function language_detector_admin_footer() {
	$language = get_locale();
	// We only add our JavaScript if the nag notice is being displayed any way
	if ( false === get_option'$plugin_slug_language_detector_' . $languagefalse ) ) {
?>
<script type="text/javascript">
	jQuery(document).ready(function ($) {
		$".notice.is-dismissible.language-detect-nag-dismiss" ).on"click"".notice-dismiss"function() {
			var data = {
				action : 'language_nag_dismiss'
			};
			$.post(
				ajaxurl,
				data
			);
		});
	});
</script>
<?php
	}
}
add_action'admin_footer''language_detector_admin_footer' );
// Trigger function for when the ajax call goes through
function language_detector_dismiss() {
	$language = get_locale();
	update_option'$plugin_slug_language_detector_' . $languagetrue );
	wp_die();
}
add_action'wp_ajax_language_nag_dismiss''language_detector_dismiss' );


The code requires some minor edits before you use it (technically only one edit is required, the text_domain one);

  • $plugin_slug in the get_option and update_option calls should reflect your own plugins slug
  • $text_domain needs to match your plugin slug (adn the langauge path ideally, just like your normal textdomain loading function)

The snippet follows the plugin guidelines for nag screens (dismissable and will stay dismissed, yay!), so you can use it as is, or you can modify it (it’s just a way to get the ball rolling really).

Caveats (before you say it’s broken);

  • The code must be in your main plugin file, this is due to the get_plugin_data call being used
  • The nag screen is per language, so if the user changes their language, they’ll get a new nag screen (possible yay, possible nay?)
  • No nag screen is shown if a translation file actually exists for the current language, if so the nag screen is automatically disabled

I also added in text domains to the nag message, this is so you can utilize it even if you decide to remove the disable nag checker, and want to display a translated message to your users, one idea here is to show a “You’ve used this plugin for 1 week, please consider helping with translations” kind of message, or any other approach you feel is valid.

5 responses to “Encourage community translations for WordPress.org plugins”

  1. François Avatar
    François

    this is very interesting, Marius, and i am sure that it will be most useful to me.
    I wonder if you might also have some small snippet or plugin which will gather statistics for me on how many users from each different languages / locales have either downloaded or are using my plugins? Unfortunately WordPress.org seems to give me statistics only for total user downloads without telling me the totals per the locales of those people.
    Regards,
    François

    1. Marius Avatar
      Marius

      You could do it by providing an opt-in stats gathering element to your plugin and sending the data your self (I’d send the locale and URL so you can distinguish between sites if you go this route).
      The stats on WordPress.org may improve over time though, the plugin directory is at this time undergoing an overhaul which will make enhancements easier to do with time, you could drop by then they have their chats and see what plans they have in regard to this (they meet on Thursdays in the #meta room on Slack – https://make.wordpress.org/meta/tag/plugin-directory/)

      1. François Avatar
        François

        Hello Marius,
        Many thanks for your prompt response and useful suggestions. I will certainly look into these.
        With a bit of luck the WP organizers and developers will implement such a functionality anyway in the near future, thus save the rest us from needing to come up with our own stats-gathering extension of our plugins.
        By the way, I really like your plugin ‘String Locator’, which is ideal for addressing a need I frequently experience.
        Keep up the good work 😉
        François

  2. David Artiss Avatar

    Hey Marius 👋🏼

    I know this code is 5 years old now but I’ve been giving it a go in the hope of implementing something in my own plugins. It appears to… mainly… work. But it doesn’t appear to be dismissible (unless I’m missing something).

  3. Marius Avatar
    Marius

    Hiya David!

    It should still, in theory, still work, but it may be that `ajaxurl` is no longer automatically populated in the admin area as a variable with a link, you would then need to replace `ajaxurl` with (in this simple example above at least) `’‘` (with single quotes around it and all), which should output the URL instead, it’s not super elegant, but for a comment reply, it’ll do 😁 I’ll probably look at updating the example, as i see the syntax highlighter used at the time of posting isn’t quite right either.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.