I want to translate my functions.php with custom code in it.

There I have a Class "Glbl_Helper" with a public static array that contains strings like "translate_me".

Now I want to translate the strings using WPML. But If I do it like in the Text-Snipped with "translate_me" and "translate_me_2" I get the error:

Fatal error: Constant expression contains invalid operations in /www/.../functions.php on line 502

Line 502 is in the functions.php in my code and the line 502 is: public static $step_questions = array(

if ( ! class_exists( 'Glbl_Helper' ) ) {

    class Glbl_Helper
        // Added keys for question array below

        const QUESTION_KEY = "question";
        const ANSWER_KEY = "answer";
        const INVALID_ANSWER_KEY = "invalid_answer";

        const ANSWER_FILTER_KEY = "answer-type-filter";
        const ANSWER_FILTER_FLAGS_KEY = "answer-type-filter-flags";

        // Added static array to define questions for each child step, where a question needs to be answered

        public static $step_questions = array(     //This is the code line 502 from the error

            self::STEP_BROKER_SIGN => array(
                self::QUESTION_KEY => __("translate me", "text_domain"),

                self::ANSWER_KEY => esc_html__("translate_me_2", "text_domain"),
                self::INVALID_ANSWER_KEY => "translate_me_3",





Can anyone tell me how I have to change my code so I can use WPML String translation to translate the Questions and Answers in the Array?

  • And what exactly is the error message? – fuxia Dec 5 '19 at 21:42
  • Edit the Error-Message in the Post – Patrik .Hörmann Dec 5 '19 at 21:55
  • Is ./functions.php on line 502 your file? Or WPML? – Jacob Peattie Dec 5 '19 at 23:23
  • The functions.php with line 502 is my file and my code. also added the comment in the code to tag line 502. – Patrik .Hörmann Dec 5 '19 at 23:59
  • My guess is that you are pretty much overkilling the idea of mo/po files. Whenever these strings shall be detected, you have to make sure, that your class is accessible by WPML. Simple array outside of a class, just wrapped in a function should do it. Also a look at WPML docs can't hurt. – user3135691 Dec 6 '19 at 0:16

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.