jjgrainger / wp-custom-post-type-class

A PHP Class for creating Wordpress Custom Post Types easily
MIT License
416 stars 114 forks source link

How to translate post type name #58

Open slobich opened 8 years ago

slobich commented 8 years ago

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.

bynicolas commented 8 years ago

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

jjgrainger commented 8 years ago

@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 :)

chieh12lee commented 8 years ago

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

jnaklaas commented 5 years ago

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.

hasinur22 commented 2 years ago

Thanks a lot. I got a solution using this.