Search code examples
wordpresswpml

Wordpress WPML not translating get_option


i am trying to translate my plugin options through WPML but it is not working. Here is how i have placed my string in the plugin file

get_option(_e('my_label','my-text-domain')); 

I have already scan my plugin through the WPML and have done translation in "German" while default is English.

Can anyone help me.

Thanks


Solution

  • The issue is that _e outputs the translated text.

    You want __ (double underscore), which will return the translated text.

    I would explore other solutions to this problem. In general you shouldn't be using translation to determine what option to pull from the database. The functionality of your plugin should not hinge on whether or not the translated text is a valid option name.

    An alternative approach may be to use get_locale to fetch the current locale and then use that to determine the option name:

    $option_name = get_locale() . 'my_label';
    $label = get_option( $option_name );
    

    Now you can still get the localized version of your option, without depending on translators to input the correct option name.

    If you have to use the translation approach, I would use _x to explain that the translated text should be an option key.

    get_option( _x('my_label', 'Must be a valid option name', 'my-text-domain') );