0

I'm building a wordpress plugin, that will register a post type. Im trying to set up a simple test case where all it does is registering a post type with get text functions and translating it with poedit. I have successfully created the .po and .mo files, and loaded them with the following code:

add_action('init', 'client_functions_textdomain');
function client_functions_textdomain(){
    load_plugin_textdomain( 'client-functions', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

If i test a gettextcall right after loading this, everything translates fine. And the load_plugin_textdomain() returns true.

But when i afterwards try to register a post type with the following code (taken from the codex), its doesn't translate:

add_action( 'init', 'codex_book_init' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'client-functions' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'client-functions' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'client-functions' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'client-functions' ),
        'add_new'            => _x( 'Add New', 'book', 'client-functions' ),
        'add_new_item'       => __( 'Add New Book', 'client-functions' ),
        'new_item'           => __( 'New Book', 'client-functions' ),
        'edit_item'          => __( 'Edit Book', 'client-functions' ),
        'view_item'          => __( 'View Book', 'client-functions' ),
        'all_items'          => __( 'All Books', 'client-functions' ),
        'search_items'       => __( 'Search Books', 'client-functions' ),
        'parent_item_colon'  => __( 'Parent Books:', 'client-functions' ),
        'not_found'          => __( 'No books found.', 'client-functions' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'client-functions' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => _x( 'book', 'URL slug', 'client-functions' ) ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}

I suspect that this has to do with the fact that both are hooked into the 'init' action, and the registering happens before the text domain is loaded? I have tried the following with no success:

  • added priority 0 to the textdoamin action, and priority 20 to book_init function
  • i have loaded the text domain in the 'plugins_loaded' action

Any help would be greatly appreciated!

Thanks Malthe

  • Turns out that all the __() and _e() functions loads fine - but not the _x() functions? – Malibur Jul 3 '14 at 13:06
1

Turns out you have to add some special function sniffing in the configuration of the pot file (from http://www.cssigniter.com/ignite/wordpress-poedit-translation-secrets/):

__
_e
__ngettext:1,2
_n:1,2
__ngettext_noop:1,2
_n_noop:1,2
_c
_nc:4c,1,2
_x:1,2c
_nx:4c,1,2
_nx_noop:4c,1,2
_ex:1,2c
esc_attr__
esc_attr_e
esc_attr_x:1,2c
esc_html__
esc_html_e
esc_html_x:1,2c

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.