0

i am wirting my very first plugin. For publishing it to the repository i want to make it translatable. On my options page i some arrays, which store the default settings. Somehow i get an error when implementing i18n.

class SettingsPagePrivacyHelper {

    private static $optionGroup = 'privacy_helper_option_group';
    private static $optionTitle = 'Simple Privacy Helper';

    /*
     * Default Setting for Options
     */
    private $commentsDefaults = array(
        'check'             => 1,
        'message'           => __('Ich habe die ###Privacy-Policy### zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden.', 'privacy-helper'),
        'error'             => __('Sie müssen die Bedingungen akzeptieren.', 'privacy-helper'),
        'consent_check'     => 0,
    );

    private $regDefaults = array(
        'check'             => 1,
        'message'           => __('Ich habe die ###Privacy-Policy### zur Kenntnis genommen. Ich stimme zu, dass meine Angaben gespeichert werden.', 'privacy-helper'),
        'error'             => __('Sie müssen die Bedingungen akzeptieren.', 'privacy-helper')
    );

    private $wcDefaults = array(
        'check'             => 1,
        'message'           => __('Ich habe die ###Privacy-Policy### zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Verarbeitung des Kaufes und auf Grund der Nachweispflicht gespeichert werden.', 'privacy-helper'),
        'error'             => __('Sie müssen die Bedingungen akzeptieren.', 'privacy-helper'),
        'reg-check'             => 1,
        'reg-message'           => __('Ich habe die ###Privacy-Policy### zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Verarbeitung des Kaufes und auf Grund der Nachweispflicht gespeichert werden.', 'privacy-helper'),
        'reg-error'             => __('Sie müssen die Bedingungen akzeptieren.', 'privacy-helper')
    );

    private $cf7Defaults = array(
        'check'             => 1,
        'message'           => __('Ich habe die ###Privacy-Policy### zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden.', 'privacy-helper'),
    );

    private $ytDefaults = array(
        'check'             => 1,
    );
}

WordPress gives me the following error: Fatal error: Constant expression contains invalid operations in [...]/settings-page.php on line 11

Does someone has an idea how to fix this?

  • you have a problem with your php syntax, probably mismatched quotes or perns – Mark Kaplun May 20 '18 at 14:02
  • FYI if you want to publish your plugin on WordPress.org, your base language has to be English. – swissspidy May 21 '18 at 5:53
  • @swissspidy as far as i know the initial string language does not matter. There are many plugins in e.g. only dutch. I am planning to translate the plugin, thats why i asked my question. You can create a .po file when your plugin is accepted, thats what i am aiming for. – JUVO_Webdes May 21 '18 at 8:52
  • If I want to translate your plugin and don’t know German, I’m lost. Also, why create PO files when you can use translate.wordpress.org? – swissspidy May 21 '18 at 8:54
4

This is a PHP issue, not specific to WordPress.

PHP does not allow you to declare a property as an expression. It should be a literal. So you can not call a function inside a property declararion. (see Invalid property declarations in http://php.net/manual/en/language.oop5.properties.php)

The solution is to create a getter method and use it instead. Or else you could set the propery in the __construct() method.

So, for example, doing it on construct:

class MyClass {

    private $myProperty;

    function __construct() {
        $this->myProperty = [
            'foo' => __('bar')
        ];
    }

}

Or doing this as a getter:

class MyClass {


    private function getMyProperty() {

        return [
            'foo' => __('bar')
        ];


    }

}
  • Semicolons inside arrays there. I tried to edit, but says I need to change 6 characters minimum :-/ – Tim Jun 1 '18 at 16:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.