Theme elements not translating

Ive got a theme that is set-up for translating but didn’t come with any translations. I used poedit to automatically detect the strings, and subsequently translated it in full. However certain elements aren’t translating in admin or on the site.

I can see in poedit that Ive translated all the strings including those that aren’t translating on the live site. Some of the non translated string are appearing on the front end however the bulk are in admin.

Ive noticed that it seems that the strings that aren’t translating are those loaded via ‘add_action’. So for example the theme comes with some custom post types with 1 being for events. In admin everything concerning this custom post type is translating except for the strings associated with the metaboxes.

So for example, the admin content created by the following code doesn’t translate in admin;

$meta_box_events = array(
'id' => 'core-meta-box-events',
'title' => __('Events Options', CORE_THEME_NAME),
'post_type' => 'event',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
     array(
        'name' => __('Event Month', CORE_THEME_NAME),
        'desc' => __('Choose the event month ', CORE_THEME_NAME),
        'id' => 'core_event_month',
        'type' => 'select',
        'options' => array(__('January', CORE_THEME_NAME), 
                            __('February', CORE_THEME_NAME),
                            __('March', CORE_THEME_NAME),
                            __('April', CORE_THEME_NAME),
                            __('May', CORE_THEME_NAME),
                            __('June', CORE_THEME_NAME),
                            __('July', CORE_THEME_NAME),
                            __('August', CORE_THEME_NAME),
                            __('September', CORE_THEME_NAME),
                            __('October', CORE_THEME_NAME),
                            __('November', CORE_THEME_NAME), 
                            __('December', CORE_THEME_NAME)
                            )
    ),
    array(
        'name' => __('Event Day', CORE_THEME_NAME),
        'desc' => __('Choose the event day. ', CORE_THEME_NAME),
        'id' => 'core_event_day',
        'type' => 'select',
        'options' => array( '1', 
                            '2', 
                            '3',
                            '4',
                            '5',
                            '6',
                            '7',
                            '8',
                            '9',
                            '10',
                            '11',
                            '12',
                            '13',
                            '14',
                            '15',
                            '16',
                            '17',
                            '18',
                            '19',
                            '20',
                            '21',
                            '22',
                            '23',
                            '24',
                            '25',
                            '26',
                            '27',
                            '28',
                            '29',
                            '30',
                            '31'
                            )
    ),

    array(
       'name' => __('Event Year', CORE_THEME_NAME),
       'desc' => __('Enter the event year using this format <em>0000.</em>', CORE_THEME_NAME),
       'id' => 'core_event_year',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Time', CORE_THEME_NAME),
       'desc' => __('Enter the event time using this format <em>00:00AM/PM to 00:00AM/PM.</em>', CORE_THEME_NAME),
       'id' => 'core_event_time',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Location', CORE_THEME_NAME),
       'desc' => __('Enter the event location, example: <em>Cramton Auditorium.</em>', CORE_THEME_NAME),
       'id' => 'core_event_location',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event City', CORE_THEME_NAME),
       'desc' => __('Enter the city of the event, example: <em>London.</em>', CORE_THEME_NAME),
       'id' => 'core_event_city',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Zip Code', CORE_THEME_NAME),
       'desc' => __('Enter the address zip code, example: <em>10308 89.</em>', CORE_THEME_NAME),
       'id' => 'core_event_zip',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Country', CORE_THEME_NAME),
       'desc' => __('Enter the event country, example: <em>United Kingdom.</em>', CORE_THEME_NAME),
       'id' => 'core_event_country',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Phone', CORE_THEME_NAME),
       'desc' => __('Enter the phone info, example: <em>+44 (0)20 7040 8037.</em>', CORE_THEME_NAME),
       'id' => 'core_event_phone',
       'type' => 'text',
       'std' => ''
    ),

    array(
       'name' => __('Event Map Latitude', CORE_THEME_NAME),
       'desc' => __('Enter the google map longitude information, example: <em>-12.8809532.</em>', CORE_THEME_NAME),
       'id' => 'core_event_lat',
       'type' => 'text',
       'std' => ''
    ),
    array(
       'name' => __('Event Map Longitude', CORE_THEME_NAME),
       'desc' => __('Enter the google map longitude information, example: <em>-38.4174871.</em>', CORE_THEME_NAME),
       'id' => 'core_event_lon',
       'type' => 'text',
       'std' => ''
    )

)
);

add_action('add_meta_boxes', 'core_add_metabox_events');

The ‘CORE_THEME_NAME’ is being picked up, I echo’d it to be sure.

So, my somewhat limited knowledge of WordPress made me suspect that the translations were being triggered before this code was loaded. However, the translation is being called in the theme’s functions.php as follows;

add_action('after_setup_theme', 'lang_setup');
function lang_setup(){
   load_theme_textdomain(CORE_THEME_NAME, CORE_DIRECTORY . '/languages');
}

From what Ive seen this should ensure that the textdomain is loaded once all the theme components are loaded. So, why are the strings not being translated in the site admin for the events posts??

Any ideas / suggestions / etc will be greatly appreciated.

Thanks.

1 Answer
1

You shouldn’t be using a constant (in your case CORE_THEME_NAME) for the textdomain. This is because the call to __ is parsing your code, not running it. Simply said: it’s not looking up the value of the constant. So, it won’t translate. Quoting the famous Otto:

Inside all translation functions, no PHP variables are
allowed in the strings, for any reason, ever. Plain single-quoted
strings only.

Leave a Comment