WordPress-Plugin-Boilerplate
WordPress-Plugin-Boilerplate copied to clipboard
Correct way of handeling class properties that include translations
Hi Guys,
As the boilerplate itself doesn't include any translation strings, I find it hard to understand how you were planning on working work with translations within class properties.
To explain my problem;
Within the Admin or Public class we want to use a few properties for default options. Those options also include translation strings. So example:
public function __construct() {
$this->example = __( "example string", "textdomain" );
}
However, the textdomain itself is only fired upon 'plugins_loaded'. The Admin and Public classes however are fired on load of the main class within their define_admin_hooks and define_public_hooks functions. With that, the property, with the translations, are build. However, with the textdomain not yet defined, the translation returns the original english string instead of the translation.
The only thing I can come up with to avoid this issue is writing a function that just builds the creates a variable everytime we need it, after the 'plugins_loaded'. But that means we need to do a database call everytime we need the data, while we want to cache is inside a property.
Does anyone has any good examples on how to properly use translation strings within class properties?
You want to set TEXTDOMAIN as a constant, or? Because you shouldn't do that (it has to do something how the gettext handles translation).
https://markjaquith.wordpress.com/2011/10/06/translating-wordpress-plugins-and-themes-dont-get-clever/
Hi Dingo, I used the wrong term. I meant to use class properties instead of constant. The class properties are set on load of the class when called in define_admin_hooks or define_public_hooks, but when we populate the property with a translatable string, it is not being parsed as such, as the textdomain is not yet loaded.