Why can I not use a class constant as text domain?

What makes WordPress.org make an internationalization of a plugin approved?

I’ve created my first plugin and here’s the main code:

https://plugins.trac.wordpress.org/browser/mastercurrency-wp/trunk/mastercurrencywp.php?rev=1374404

I am using a constant for the text domain:

class mastercurrencywp {
    const plugin_textdomain = 'mcwpww';
}

And later in the same class I use it like this:

$curr_desc['EUR'] = __('Euro', self::plugin_textdomain);

I’ve tested this locally and then internationalization of the plugin worked, I’ve also added a Swedish translation (that works on my local server) but wordpress.org states:

This plugin is not properly prepared 
  for localization. If you would like to 
  translate this plugin, please contact the author.

( https://translate.wordpress.org/projects/wp-plugins/mastercurrency-wp )

Why? (What is missing?)

I’ve just updated the plugin, so it might take some time before wordpress.org updates it statement?

1 Answer
1

The value of a constant or variable can be evaluated by a PHP parser only. The tool that is used to extract the translatable strings is written in PHP, but is just a string parser. It doesn’t run your code, because that would be too slow and error prone.

Here is an example for how complex that could be:

interface FooTranslate
{
    const TEXT_DOMAIN = 'foo';
}
interface BarTranslate extends FooTranslate {}

class FooBar implements BarTranslate
{
    public function greeting()
    {
        _e( 'hello!', self::TEXT_DOMAIN );
    }
}

(new FooBar)->greeting();

Now find the correct string value for the text domain without executing PHP. Difficult.


You have to use a simple string for all calls to the translation functions.

The developer handbook is telling us that very clearly, unfortunately in the style of an error message and with wrong quotation marks:

Do not use variable names for the text domain portion of a gettext function. Do not do this as a shortcut: __( ‘Translate me.’ , $text_domain );

On a second note: Please do not load your translation files on init. This is expensive: These files are now loaded on every call to AJAX requests, comment submissions and every other file that might not even be related to your plugin. Check the current request context before you are asking WordPress to include a file.

Leave a Comment