1

I need help how to translate the custom post type and taxonomy when using multisite and multi-language. I am using subdirectory a /en /sv etc.

Are using the plugin (Multisite Language Switcher), but can not change the rewrite settings there. So I am guesing I have to change some rewrite? Or should I translate the post type with translations file, .mo .po?

This is how the post type set up are in functions.php. Should I do something with the rewrite?

function create_posttype_product() {
    register_post_type( 'product',
        array(
            'labels' => array(
                'name' => __('Products'),
                'singular_name' => __('product'),
                'add_new' => __('Add new product'),
                'add_new_item' => __('New product'),
                'edit_item' => __('Edit product')
            ),
            'public' => true,
            'rewrite' => array( 'slug' => 'product', 'with_front' => false ),

            'has_archive' => 'product',
            'menu_icon'   => 'dashicons-editor-help',
            'supports' => array('title', 'editor', 'thumbnail')
        )
    );
}
add_action( 'init', 'create_posttype_product' );

So for example on english webpage url would be:
www.mypage.com/en/products

But for the swedish I want
www.mypage.com/sv/produkter

And other language :
www.mypage.com/xx/product-name-in-this-language

How can I manage to get this result? I have searched and search and can not find the right answer.

0

So firstly whilst this may be possible I imagine that it is probably not best practice. This is because semantically, and for SEO it makes more sense to have fewer URLs.

It could be understood that if your page is /en/foo and I want to switch to French, I can just go to /fr/foo. People may do this manually, and some automated tools may understand the site works like this. If a person or a tool also has to translate 'foo' to French to find the correct page, this is more complicaed and it means everyone has to agree on translations as well as language codes, and translations may not always be 100% the same.

I think this is why it's hard to find solutions to this in searching - generally it is not done so that URLs stay as similar as possible and only the language part changes.

If you do want to do it and you don't have many languages, one way to do this would be to find the manual translations and add .htaccess rules to invisbly rewrite the URL with rules in your root .htaccess something like:

RewriteRule ^/sv/produkter(/.*) /en/product$1 [L]

This would be easy to do as long as you didn't have many slugs or languages to do it for.

There's also some possibility the PolyLang plugin might do this but you'd need to look into that further. See point 3 here: https://polylang.pro/doc/url-modifications/

2
  • Thanks for your answer. Hm, but the mount of url would still be the same? I mean if you have /en/product/product1 , /fr/product/product1. The only thing is to translate the product "url/string". Same if it is /fr/product/ /en/product . It would still be 2 url, but only translation on the product. And product1 would also get two different names, but same mount of urls, or I am thinking wrong? I was thinking how WPML does it, there you can translate the string for the custom post type name. Therefore it would be a solution to do this even without a plugin like wpml or polylang? – Andreeez Jul 17 '20 at 7:04
  • I'm not sure what you're asking. Yes it would be two URLs, my point is just that both parts change and can't be predicted. Like if I'm on eBay or Amazon sometimes I can find the /en/ or /us/ (or even change the .com to .co.uk) part and switch sites really easily. This wouldn't work if I had to translate the whole rest of the URL too. This is probably a corner case though. – mozboz Jul 17 '20 at 9:45
0

To use the rewrite option you have to put "product" in variable. Warning : you have to let the same value "product" for "register_post_type".

function create_posttype_product() {

 if  ($language == 'sv') {
      $productSlug = 'produkter';   
 } else {
      $productSlug = 'product';   
 }

register_post_type( 'product',
    array(
        'labels' => array(
            'name' => __('Products'),
            'singular_name' => __('product'),
            'add_new' => __('Add new product'),
            'add_new_item' => __('New product'),
            'edit_item' => __('Edit product')
        ),
        'public' => true,
        'rewrite' => array( 'slug' => $productSlug, 'with_front' => false ),

        'has_archive' => 'product',
        'menu_icon'   => 'dashicons-editor-help',
        'supports' => array('title', 'editor', 'thumbnail')
    )
)}; 
add_action( 'init', 'create_posttype_product' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.