custom-post-type-ui icon indicating copy to clipboard operation
custom-post-type-ui copied to clipboard

Aid in making translateable via PolyLang

Open tw2113 opened this issue 9 years ago • 5 comments

Anything we can do to make it easier?

https://wordpress.org/support/topic/translate-cpt-and-taxonomy-labels-with-polylang?replies=4

tw2113 avatar Jun 24 '16 16:06 tw2113

Basically what you can do to make small amount of code changes is this. Add one more section next to edit custom post type as translation post type. Which will contain default string for translating and number of languages defined. And before this filter add one more to filter post_type variable, and translate it depending on the current language in administrator or for the user. $post_type['map_meta_cap'] = apply_filters( 'cptui_map_meta_cap', true, $post_type['name'], $post_type );

movingSone avatar Jun 08 '17 23:06 movingSone

Would be heavily used :) I'm currently trying to do this, I'll comment again if I find anything useful.

ffoodd avatar Sep 05 '17 12:09 ffoodd

Hi there,

I made my way around this by creating a mu-plugin, which simply registers CPT labels to Polylang strings on CPT update.

Here is a piece of code:

add_action('cptui_post_register_post_types', 'cpt__labels');

function cpt__labels($cpts)
{
    $types = get_option('cptui_post_types');

    if (!empty($types) && function_exists('pll_register_string'))
    {
        foreach ($types as $type)
        {
            pll_register_string( 'CPT', $type['name'] );
            pll_register_string( 'CPT', $type['singular_label'] );
            pll_register_string( 'CPT', $type['label'] );

            if (!empty($type['description'])) {
                pll_register_string( 'CPT', $type['description'] );
            }

            foreach ($type['labels'] as $label) {
                pll_register_string( 'CPT', $label);
            }
        }
    }

    return $cpts;
}

And wherever it's needed, I check for Polylang translation:

if (function_exists('pll__')) {
            $type = pll__($post_type->labels->name);
}

That may not be perfect but it works. Since it's depending on both plugins, I think this is more a third party plugin way (and there's a lot of thing to filter with Polylang's function, such as:

  • archive titles (filtering get_the_archive_title),
  • menu names (in custom Nav Walker),
  • title tag (filtering document_title_parts),
  • breadcrumb if any,
  • and admin labels.

I did not end this, but I think the main part is good. What's your opinion about this?

PS: Thanks for your work :)

ffoodd avatar Sep 06 '17 08:09 ffoodd

Overall looks good to me. I do believe you could save yourself a get_option call by using the $cpts parameter passed in to the callback.

If you look at https://github.com/WebDevStudios/custom-post-type-ui/blob/master/custom-post-type-ui.php#L245 you'll see it has the exact same source.

Otherwise, good information to know and can help us decide how we want to handle the topic. Thanks.

tw2113 avatar Sep 06 '17 13:09 tw2113

503-polylang-support, needs work, but I have something started

tw2113 avatar May 11 '22 02:05 tw2113