I’d like to translate one error message from a plugin that blocks spam from our forms but occasionally blocks legitimate (live-person) form submissions. The message is coded to be translateable – in the plugin itself, the line is:

$err_msg = __( 'That action is currently not allowed.' );

My problem is, my site is already in U.S. English just like the plugin, and technically when I “translate” this string so that visitors see a “it’s not your fault; please contact so-and-so so we can fix this issue for you” message, it will still be in U.S. English. I’ve set up a textdomain in my theme and was able to generate the .pot, .po and .mo files to translate this one string. The problem is, since I’m setting wp-config to en_US, it’s not actually changing anything.

Since I don’t have an exact process to trigger the error on the forms themselves, I set up an admin dashboard widget to test the translation. Sure enough, if the contents of the widget are

$err_msg = __( 'That action is currently not allowed.' );
echo $err_msg;

I get ‘That action is currently not allowed.’ If I change the widget contents to

$err_msg = __( 'That action is currently not allowed.', 'mytextdomain' );
echo $err_msg;

I get the custom message that I’m trying to achieve.

I don’t want to edit the plugin’s files themselves since they would revert every time there’s an update. There’s no filter or hook in the plugin for this particular message – there are filters for other errors, just not this generic one. Is there a better approach to getting this one string of user-facing text changed? I want something server-side rather than a JS/jQuery solution, if at all possible.

1 Answer
1

The gettext filter can be used to change strings that use the gettext functions.

add_filter( 'gettext', 'wpse_change_error_string', 10, 3 );
/**
 * Translate text.
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
 *
 * @return string
 */
function wpse_change_error_string( $translation, $text, $domain ) {
    // The 'default' text domain is reserved for the WP core. If no text domain
    // is passed to a gettext function, the 'default' domain will be used.
    if ( 'default' === $domain && 'That action is currently not allowed.' === $text ) {
        $translation = "This is the modified version of the original string...";
    }

    return $translation;
}

Note: Fortunately, the WordPress core does not currently use the string That action is currently not allowed., so that message will not be undesirably changed using the code above. But, that would happen if the core did use that message. This is one reason why it’s important for plugins and themes to use their own unique text domain.

The gettext family of functions (__(), _e, etc) should always be passed a text domain though. The exception to this rule is for the WordPress core, which doesn’t pass a text domain explicitly, but it still has one named default.

I would contact the plugin’s authors and suggest that they add their own text domain to their gettext function calls.

Leave a Reply

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