I'm looking for a way to make my plugin's options available/translatable across a multi-lingual site.
For example, several of my options are text strings that are then used on the front end.
echo get_option( 'my_plugin_text', __( 'Whoa default text', 'my-plugin' ) );
However, it has come to my attention that these strings are then not translatable by WPML. And I would presume by other multi-lingual plugins as well. The whole point of the options was to give users a way to customize some of the text, but this is currently negated in a multi-lingual context.
Since the option text will vary, it doesn't seem to make sense to wrap the whole thing in a gettext
function:
_e( get_option( 'my_plugin_text', __( 'Whoa default text', 'my-plugin' ) ), 'my-plugin' );
I could wrap the whole thing in a filter, but then people are obligated to know how to write a filter in order to change the text, but again, that defeats the purpose of allowing users to change these certain text strings from the admin.
One suggestion, I've received is to use get_locale()
.
$text = get_option( 'my_plugin_text_' . get_locale(), false );
if( $text === false ){
$text = get_option( 'my_plugin_text', __( 'Whoa default text', 'my-plugin' ) );
}
echo $text;
That seems pretty reasonable, but I'm not too familiar with the multi-lingual plugins to know if that will work universally. Does the locale change on the admin side too, such that someone could save the plugin options in the English version and then save different text for the (for example) Spanish version?
Has anyone ever run into this? Is there another solution that I have overlooked?