WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Translating Custom Post Type & Taxonomy Slugs (15 posts)

  1. gyop
    Member
    Posted 1 year ago #

    Hello,

    First of all thanks for this great plugin, it looks great so far!

    I have a question though: how can I translate custom post types and taxonomy slugs, so that they are displayed correctly in the URL?

    For example, standard 'posts' have 'category' and the URLs are in the form website.com/category/english-cat, website.com/it/category/italian-cat. However it seems not possible to do website.com/it/categoria/italian-cat.

    The same is true for custom post types and taxonomies, where website.com/cars/used works but not website.com/automobili/usate.
    website.com/it/cars/usate works as expected but it's weird to mix languages in the URL.

    Perhaps I'm doing something wrong? Or maybe this can be fixed with some rewrite-rules?

    Thank you!

    My WordPress and Polylang settings:

    ---
    WordPress: v3.5.1
    Permalinks structure: Post name
    Polylang: v1.0.2
    2 languages: English, Italian
    Default language: English
    Language code '/en/' added to all urls
    Remove '/language/' in pretty permalinks
    Hide URL language information for default language
    Activate languages and translations for media
    Synchronization, Custom post types, Custom taxonomies: Everything checked
    ---

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    You can try to add the following code in a custom plugin or in the functions.php of your theme. This will work only if you add the language code to all urls.

    add_filter('option_category_base', 'pll_category_base');
    function pll_category_base($option) {
    	if (is_admin())
    		pll_register_string('category_base', $option);
    	else
    		$option = pll__($option);
    	return $option;
    }

    Then you type your original slug as category base in the permalinks panel, and translate it in the strings translation panel.

  3. gyop
    Member
    Posted 1 year ago #

    Hello Chouby!

    I've been trying your solution and – with my surprise – it actually only worked once.
    So I tried on fresh installs, but I couldn't make it working again, perhaps I'm doing something wrong.

    Also, how would it be possible to apply the same to Custom Post Types and Taxonomies?

    Thanks for your help.

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I am sorry. I confirm that it doesn't work. Only a small part of the job is done here and in fact it will be quite complex to make it work.

  5. jmgodin
    Member
    Posted 1 year ago #

    Hi,

    I am actually looking for a way to do this myself. Any progress on your end?

  6. gyop
    Member
    Posted 1 year ago #

    I haven't found a solution yet, but I think some rewrite-magic should make it possible though.

  7. Westeros
    Member
    Posted 1 year ago #

    I have a similar issue, can't find a way to translate the custom post type name in permalinks:
    example:
    http://mywebsite.com/product/herbs-spices/
    Where "product" is the custom post type name.
    It remain the same, no matter i change the language..
    Did you found a solution to this?

  8. Juan Viñas
    Member
    Posted 1 year ago #

    I have the same problem than Westeros, I want to translate the custom post type name, and I don't know how.

    For example, I have a custom post type called "cars". I usually work in english and spanish, the translation for cars is "coches". Now I have in my website permalinks like this: http://mywebsite.com/cars/..., when I'm navigatin in english. But I want the permalink in spanish too: http://mywebsite.com/coches/....

    I suppose this is not possible yet, and I suggest you it for a next release. As a WordPress developer, I really have no idea of how to do this, I understand that it is a complicated change. But if you can do it, it will be great! :)

  9. aigarinsh
    Member
    Posted 1 year ago #

    I agree with everybody on this one. Such a feature would make the plugin almost perfect. I have tried others and qTranslate Slug does it.

    But with it I ran into a problem that it didn't work on pages as custom post type archives. And the main difference of keeping posts in one or separated depends more on client and website, both versions have bonuses and drawbacks.

    For testing purposes I have managed to use different slugs depending on conditions. And the links in menu appear as I want, but I suppose the rewrite is not happening.

    For example here is how I register my custom taxonomy:

    <br />
    function register_my_cpt() {<br />
    	$labels = array(<br />
    		...<br />
    	);<br />
    	$args = array(<br />
    		'labels'		=> $labels,<br />
    		'public'		=> true,<br />
    		'menu_position'	=> 5,<br />
    		'supports'		=> array( 'title', 'editor', 'thumbnail' ),<br />
    		'has_archive'	=> true,<br />
    		'rewrite'		=> array( 'slug' => 'menu' )<br />
    	);<br />
    	register_post_type( 'my_cpt', $args );<br />
    }</p>
    <p>function register_my_taxonomy() {<br />
    	$labels = array(<br />
    		...<br />
    	);<br />
    	$cat_slug = get_translated_cat_slug();<br />
    	$args = array(<br />
    		'labels'		=> $labels,<br />
    		'hierarchical'	=> true,<br />
    		'rewrite'		=> array( 'slug' => $cat_slug )<br />
    	);<br />
    	register_taxonomy( 'my_taxonomy', 'my_cpt', $args );<br />
    }</p>
    <p>function get_translated_cat_slug() {<br />
    	$current_ln = get_bloginfo('language');<br />
    	if( empty( $current_ln ) ) return 'default_taxonomy';<br />
    	$slugs = array(<br />
    		'lv'	=> 'slug-language-lv',<br />
    		'en-US'	=> 'slug-language-en',<br />
    		'ru-RU'	=> 'slug-language-ru'<br />
    	);<br />
    	return $slugs[$current_ln];<br />
    }<br />

    Not the best solution as it changes also slugs for the links to contents in other languages, but that is just for testing. Anyway, I found out that the slugs of the links changed depending on the condition, but the rewrite doesn't happen. Why so? The CPT archive page can be accessed with default slug for all languages (en/slug/). When do the rewrite functions take place? And where are they?

    For the current project I could get away even with some hardcoded stuff, but for the future it would be nice to have a place in Admin where you could enter slug translations for all custom post types and taxonomies.

  10. aigarinsh
    Member
    Posted 1 year ago #

    Code got messed up with
    , so here I post again:

    function register_my_cpt() {
    	$labels = array(
    		...
    	);
    	$args = array(
    		'labels'		=> $labels,
    		'public'		=> true,
    		'menu_position'	=> 5,
    		'supports'		=> array( 'title', 'editor', 'thumbnail' ),
    		'has_archive'	=> true,
    		'rewrite'		=> array( 'slug' => 'my_cpt_slug' )
    	);
    	register_post_type( 'my_cpt', $args );
    }
    
    function register_my_taxonomy() {
    	$labels = array(
    		...
    	);
    	$cat_slug = get_translated_cat_slug();
    	$args = array(
    		'labels'		=> $labels,
    		'hierarchical'	=> true,
    		'rewrite'		=> array( 'slug' => $cat_slug )
    	);
    	register_taxonomy( 'my_taxonomy', 'my_cpt', $args );
    }
    
    function get_translated_cat_slug() {
    	$current_ln = get_bloginfo('language');
    	if( empty( $current_ln ) ) return 'default_taxonomy';
    	$slugs = array(
    		'lv'	=> 'slug-language-lv',
    		'en-US'	=> 'slug-language-en',
    		'ru-RU'	=> 'slug-language-ru'
    	);
    	return $slugs[$current_ln];
    }
  11. KLicheR
    Member
    Posted 8 months ago #

    Hi!,

    I wrote a little plugin to handle translations of custom post type rewrite slugs.

    I use it with lang prefix in URL (/en/my_post_type/title, /fr/mon_type_darticle/titre).

    It also handle correct translation for "get_permalink()" function;

    To translate your post types slugs, install/activate my plugin and use this code in your functions.php or in your plugin:

    add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
    	// Add translation for "my_post_type".
    	$post_type_translated_slugs['my_post_type'] = array(
    		'en' => 'my-english/rewrite-slug',
    		'fr' => 'my-french/rewrite-slug',
    	);
    	// Add translation for "product".
    	$post_type_translated_slugs['product'] = array(
    		'en' => 'products',
    		'fr' => 'produits',
    	);
    	return $post_type_translated_slugs;
    });

    It is really NOT well tested. It work for the specified configuration: not a multisite, language determine by folder (prefix /en, /fr) and only with post types slugs. But hey, it's a start:)

    For now, it's on a GitHub repo so if you want to make it better, just fork it. Maybe this could be eventually merge to Polylang, we'll see.

    Polylang - Translate URL Rewrite Slugs
    GitHub Repo
    Download latest version as zip

  12. aigarinsh
    Member
    Posted 7 months ago #

    This is something a lot of people have been waiting for. I somehow managed it myself a while ago, but this is much better. Works for me, except when it has been ticked "Hide URL language information for default language". If we could fix that, it would be awesome and author should definitely think about implementing something like this in the core code.

  13. KLicheR
    Member
    Posted 7 months ago #

    Ok! I'll try to add this. But like I said, I did this for a particular project but keeping in mind that it can evolve as a general plugin or add to the core of Polylang so not all functionnalities of Polylang work for now and there is probably a lot of work to do so.

  14. Sprul66
    Member
    Posted 6 months ago #

    Any updates to the "hide URL language information for default language" problem with your plugin?

  15. KLicheR
    Member
    Posted 6 months ago #

    It's the next item on my to-do list. I'll keep you update.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic