1

In a plugin I'm developing, most translations do work, but some strings refuse to work without an apparent reason.

For a settings page, I'm using this pattern:

$clgs_settings_descriptions = array(
    'notification_severity_filter' => __( 'Minimum severity for notification in adminstration menu', 'custom-logging-service' ),
    'def_severity_filter' => __( 'Default minimum severity filter on log page', 'custom-logging-service' ),
    'manager_role' => __( 'Roles that can manage Custom Logs', 'custom-logging-service' ),
    'log_entries_per_page' => __( 'Log entries per page', 'custom-logging-service' )
);

function clgs_settings_init() { 
    global $clgs_settings_descriptions;

    //..

    foreach ( $clgs_settings_descriptions as $key => $desc ) {
        add_settings_field( 
            $key, 
            $desc, 
            'clgs_field_render', 
            CLGS_OPTION_PAGE,
            CLGS_GROUP,
            [ $key ]
        );
    }
}
add_action( 'admin_init', 'clgs_settings_init' );

These four strings are present in the custom-logging-service-de_DE.po file:

#: includes/settings.php:3
msgid "Minimum severity for notification in adminstration menu"
msgstr "Mindestschweregrad für Benachrichtigungen im Administrationsmenü"

#: includes/settings.php:4
msgid "Default minimum severity filter on log page"
msgstr "Standard-Mindestschweregrad auf Log-Seite"

#: includes/settings.php:5
msgid "Roles that can manage Custom Logs"
msgstr "Rollen mit der Fähigkeit, Freie Logs zu verwalten"

#: includes/settings.php:6
msgid "Log entries per page"
msgstr "Log-Einträge pro Seite"

The .mo file is current. There are other translatable strings in the same file, if I change them and run the gettext stack, I can see those translations appearing in the rendered page.

It's only these four strings that remain untranslated.

Can anyone spot why they are not working?

Update 2016-2-3: As a patch, I am now calling add_settings_field like this:

    add_settings_field( 
        $key, 
        __( $desc, 'custom-logging-service' ), 
        'clgs_field_render', 
        CLGS_OPTION_PAGE,
        CLGS_GROUP,
        [ $key ]
    );

It works, but I wouldn't say it is the intended pattern.

2

Looking at this again, I finally saw it: $clgs_settings_descriptions is defined globally, during plugin load. The textdomain is loaded at action hook plugins_loaded, which is obviously later...

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.