I want to translate constant strings in my plugin defined as:
define( 'CONSTANT', __( 'string-A', 'textdomain' ) );.
The 'string-A' is available for translation and I have uploaded the mo files in the desired language including this string but in the WordPress it still reflects as English string. Translation is working for all other strings except the one defined as constants in my config file. Any idea why is this happening?

  • maybe because the textdomain is loaded after the plugin is loaded. codex.wordpress.org/Plugin_API/Action_Reference – inarilo Aug 18 '17 at 11:27
  • 1. In my case, other strings that are not referenced from constants are being shown in the local language. 2. I am laoding text-domain before using add_action( 'plugins_loaded', plugin_load_textdomain'); where plugin_load_textdomain function calls load_plugin_textdomain() function – learning_13 Aug 19 '17 at 0:22
  • To add the way I am using this constant later is like $x= '<h4>'.CONSTANT_NAME.'<h4>' and then echo $x; . Now since the translated value will be there in CONSTANT_NAME key, it should print translated string. But it's not happening – learning_13 Aug 19 '17 at 0:32
  • plugins_loaded runs after plugins are loaded, so any constant definitions will run before that. but i'm not clear on why you are using constants to begin with, you can just use __( 'string-A', 'textdomain' ) directly. – inarilo Aug 19 '17 at 7:27
  • My intention is to put all strings in a config file named xyx_strings.php so it's easier to manage and it will also avoid maintaining duplicate strings if any. That's why I want to use constants. So is it not possible to use constants for translation? Also, how does the constant value got entered in POT file? (Does execution flow of gettext to find all translation ready strings is different from the one when the will be displayed on site?) – learning_13 Aug 19 '17 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.