Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi there,

    could you please elaborate a little here? Are you talking about “translating” (i.e., replacing) the CPT slug in CPT archive links?

    Kind regards,
    Thorsten

    Thread Starter Aurélien Denis

    (@maigret)

    Hi,

    exactly!

    Let’s say we have a CPT for Books. My URL is : domain.com/book/my-book

    In French, it should be : domaine.fr/livre/mon-livre

    Hi,

    thanks for clarifying.

    Currently, there is no built-in way to do this – we plan to add one in the future, though.

    For now, you would have to filter the CPT archive links and replace the slug. This could look something like the following:

    add_filter( 'mlp_linked_element_link', 'filter_mlp_linked_cpt_archive_link', 10, 4 );
    
    /**
     * Returns the given URL after having replaced the CPT slug part wrt. the current language.
     *
     * @param string                    $url               Current URL.
     * @param int                       $target_site_id    Target site ID.
     * @param int                       $target_content_id Target content ID.
     * @param Mlp_Translation_Interface $translation       Translation object.
     *
     * @return string
     */
    function filter_mlp_linked_cpt_archive_link(
    	$url,
    	$target_site_id,
    	$target_content_id,
    	Mlp_Translation_Interface $translation
    ) {
    
    	if ( ! is_post_type_archive() ) {
    		return $url;
    	}
    
    	if ( ! is_a( $translation, 'Mlp_Translation_Interface' ) ) {
    		return $url;
    	}
    
    	$source_site_id = $translation->get_source_site_id();
    	if ( ! $source_site_id ) {
    		return $url;
    	}
    
    	$fragments = array(
    		// EN
    		1 => array(
    			'/book/',
    			'/pizza/',
    		),
    		// FR
    		2 => array(
    			'/livre/',
    			'/crepe/', // ;)
    		),
    	);
    
    	if ( empty( $fragments[ $source_site_id ] ) ) {
    		return $url;
    	}
    
    	if ( empty( $fragments[ $target_site_id ] ) ) {
    		return $url;
    	}
    
    	return str_replace(
    		$fragments[ $source_site_id ],
    		$fragments[ $target_site_id ],
    		$url
    	);
    }

    The $fragments array has the site IDs as keys and the according CPT slugs (i.e., their individual translation in that language) as values.

    Hope this helps!
    Thorsten

    Thread Starter Aurélien Denis

    (@maigret)

    Hey thanks for your detailed answer (I love “crêpes” too!).

    It would be an amazing feature to get it in the plugin admin.

    I tried your code but I get 404 error even if I flush permalink of both sites…

    Hi there,

    I just tested this, and it’s working fine for me. For CPT archives, as I wrote earlier. I just realized, however, that you provided two single CPT post links.
    If this is what you want to adapt (as well), then the above code has to be tweaked a little. Please replace the line if ( ! is_post_type_archive() ) { (this is the first if in the function) with if ( ! is_sinlge() && ! is_post_type_archive() ) {.

    Kind regards,
    Thorsten

    Thread Starter Aurélien Denis

    (@maigret)

    Hi!

    It seems to works fine now for both CPT archives and single CPT post!

    Just 2 things:

    1. is_single is better ??
    2. It doesn’t work for Easy Digital Downloads post type. Need to spend more time on this

    Thanks for your great help!

    I’m wondering how reliable this workaround is. How can we make sure the replaced part is the CPT slug of archives and single views and not – let’s say the slug of a subpage like /page/subpage/books/?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Translate CPT slugs’ is closed to new replies.