Can’t we use strings defined as PHP constants if we want to translate them in a plugin?

I think this is happening because Constants cannot be redefined later. Once they are set, they are fixed. http://php.net/manual/en/language.constants.php

I’m not exactly sure how WP language constructs work, but part of me thinks that they are defined, then changed later on the fly when plugins/themes use them. I don’t have a reference for this but it seems likely based on what you are experiencing.

i.e. Constant is set with English text the when the code has initialized, but later, when a different language is set, the language functions hot swap the text…except that with constants, since they cannot be redefined, they stay in English.

Hopefully someone else can confirm/deny this behavior.

For what it’s worth, Constants are a bad place to store dynamic information (like text to be output). They are better used for server location references that don’t change during the execution of a script.

If you want to share variables between very different sections of your site, then perhaps consider making a reference in the $GLOBALS array. http://php.net/manual/en/reserved.variables.globals.php or create a simple function that just returns the information you want it to return after being filtered for language.

Edit:

I ran a test on the only multi-language site I have. I temporarily tweaked the translate function to make it echo my output before returning it (but after filtering), and I got a list of English strings before the Spanish translation kicked in.

My suspicion is that early on the strings are processed so that translation functions can see what strings are available for translating. Since constants cannot be changed after they are defined, they cannot be translated after they are defined in English.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)