wp-custom-post-type-class icon indicating copy to clipboard operation
wp-custom-post-type-class copied to clipboard

How to translate post type name

Open slobich opened this issue 9 years ago • 5 comments

i don't know if i'm doing something wrong but i'm not able to translate the name of the post type/taxonomy... not the 'Add New' label but the singular and plural names of the post type/taxonomy (ie: my CPT singular name is "Brands" and i want to translate "Brands").

this is the way i'm registering the post type

// Register Brand custom post type
$brand = new CPT(
    // Names
    array(
        'post_type_name'    => 'brand',
        'singular'          => 'Brand',
        'plural'            => 'Brands',
        'slug'              => 'brands'
    ),
    // Options
    array(
        'has_archive'           => true,
        'supports'              => array('title', 'editor', 'author', 'thumbnail', 'revisions'),
        'exclude_from_search'   => true,
        'map_meta_cap'          => true,
        'capabilities'          => array(
            'edit_post'             => 'edit_brand',
            'edit_posts'            => 'edit_brands',
            'edit_others_posts'     => 'edit_other_brands',
            'publish_posts'         => 'publish_brands',
            'read_post'             => 'read_brand',
            'read_private_posts'    => 'read_private_brands',
            'delete_post'           => 'delete_brand'
        )
    )                 
);
// Icon
$brand->menu_icon('dashicons-star-filled');
// Translation
$brand->set_textdomain('my-text-domain');

i also tried using WordPress translation functions in the names section like so:

    array(
        'post_type_name'    => 'brand',
        'singular'          => __('Brand'),
        'plural'            => __('Brands'),
        'slug'              => 'brands'
    ),

and with adding a text domain name

    array(
        'post_type_name'    => 'brand',
        'singular'          => __('Brand', 'my-text-domain'),
        'plural'            => __('Brands', 'my-text-domain'),
        'slug'              => 'brands'
    ),

hope you can help. thnx.

slobich avatar Feb 17 '16 12:02 slobich

Your last snippet should work

 array(
        'post_type_name'    => 'brand',
        'singular'          => __('Brand', 'my-text-domain'),
        'plural'            => __('Brands', 'my-text-domain'),
        'slug'              => 'brands'
    ),

Are you using poedit (or similar software) to scan your source files? They should come up in the translator and then you can provide the translated terms

bynicolas avatar Apr 04 '16 16:04 bynicolas

@slobich Sorry for the late reply

As @bynicolas said, your last example should work.

Depending on how you are translating, the set_textdomain() method can work in some cases, however if you are using something like poedit, it reads the source files and not the result of what set_textdomain will do.

Hope this helps,

Thanks :)

jjgrainger avatar Apr 05 '16 16:04 jjgrainger

I have the same problem... My textdomain works fine everywhere but singular name

chieh12lee avatar Aug 01 '16 13:08 chieh12lee

I just encountered the same issue, using CPT in a plugin. Solved it by creating the CPT's after loading the plugin's textdomain (and not before).

// this first
load_plugin_textdomain( 'my_textdomain', false, dirname( PLUGIN_BASENAME ) . '/languages/' );

// then create the CPT's

Perhaps this saves a couple of minutes here and there.

jnaklaas avatar May 10 '19 10:05 jnaklaas

Thanks a lot. I got a solution using this.

hasinur22 avatar Oct 05 '22 06:10 hasinur22