Polylang post duplication and media translation
-
I’ve seen several threads in the support forum asking how to duplicate a post into a different language using the Polylang plugin instead of having to start the translation of an existing page or post from scratch.
I myself ran into this issue and decided to improve the Polylang plugin as follows.
Simple post duplication
Line numbers are for version 1.7.3 (but I had this code running on Polylang version 1.6)
Edit wp-content\plugins\polylang\admin\admin-sync.php
Insert the 2 new lines shown below in function add_meta_boxes:Line 57: $from_post = get_post($from_post_id); $post->post_title = $from_post->post_title . ' Copy_' . $_GET['new_lang']; // NEW appends Copy_Language to original Post Title $post->post_content = $from_post->post_content; // NEW copy the content of the original post into the new translated post Line 58: foreach (array('menu_order', 'comment_status', 'ping_status') as $property)
You can now proceed to “Add new translations” duplicating the post or page using the standard “+” icon that Polylang sets on your pages and posts. This will copy the post from the original language into the new language and provide a new post title that you should edit. You can then start translating.
Post Duplication with Image translation:
If you are building a multi language site you also want your images to be translated; that is having the “alt attribute” or alternate text for the image translated for each language of your site. The first step is thus to translate the images.
Go to WordPress Setings->Languages then the “Settings” tab and make sure the Media checkbox is checked to “Activate languages and translations for media” in Polylang.
Then translate your images in the media library by clicking in the “+” icon that polylang displays. Make sure you translate the “Alternative Text” before clicking the “Update” button.
Note that since Polylang version 1.7 the alt text gets copied from the original language.
Once your images are translated you can then proceed to “Add new translations” duplicating the post or page using the standard “+” icon on your pages/post. But first read the next section…
Polylang code changes:
Edit wp-content\plugins\polylang\admin\admin-sync.php
Insert the 4 new lines shown below in function add_meta_boxes:Line 57: $from_post = get_post($from_post_id); $post->post_title = $from_post->post_title . ' Copy_' . $_GET['new_lang']; // NEW appends Copy_Language to original Post Title $post->post_content = $from_post->post_content; // NEW copy the content of the original post into the new translated post if ($this->options['media_support']) // NEW Check if images are to be translated (as set in polylang "Settings" tab) $this->translate_images_in_duplicated_post($post,$_GET['new_lang']); // NEW do the magic to update the images Line 58: foreach (array('menu_order', 'comment_status', 'ping_status') as $property)
Then insert the function that does the translation of image alt attributes:
public function translate_images_in_duplicated_post(…)
somewhere in this same file like right after the function add_meta_boxes you just modified.
This function parses the post_content string looking for images, extracts the ID of the image, gets the newID of the translated image (calling polylang get_translation) and then replaces the image alt=”alt text in original language” with the alt=”alt text in the translated language”.public function translate_images_in_duplicated_post($post, $to_lang) { $content = $post->post_content; $imgend = 0; $translated_content = ''; $imgstart = strpos($content,'<img',$imgend); while ($imgstart !== false) { $translated_content .= substr($content,$imgend,$imgstart-$imgend); // Keep string up to imgstart $index = strpos($content,'wp-image-',$imgstart); // Search from <img $imgend = strpos($content,'/>',$imgstart); // Search from <img $imgend += 2; // Advance /> $image_id = 0; if (sscanf(substr($content,$index,$imgend-$index),"wp-image-%d",$image_id) == 1) { $translated_image_id = $this->model->get_translation('post', $image_id, $to_lang); if(!$translated_image_id) { echo "No translation to $to_lang exists for image $image_id"; $translated_content .= substr($content,$imgstart,$imgend-$imgstart); // Keep original image } else { $translated_content .= substr($content,$imgstart,$index-$imgstart); // Keep up to wp-image $old_imageId = sprintf("wp-image-%d",$image_id); $new_imageId = sprintf("wp-image-%d",$translated_image_id); $translated_content .= $new_imageId; $imgstart = $index + strlen($old_imageId); // Skip wp-image-oldId // Update the parent of the translated image if it has none $image_post = get_post($translated_image_id); if ($image_post->post_parent === 0) { $translated_image_post = array('ID'=> $translated_image_id, 'post_parent' => $post->ID); wp_update_post($translated_image_post); } $index = strpos($content,'alt="',$imgstart); if ($index !== false) { // Found alt text replace with new image if available $index += 5; // Skip alt=" $new_image_alt = get_post_meta($translated_image_id, '_wp_attachment_image_alt', true); if (is_string($new_image_alt) AND $new_image_alt != '') { $translated_content .= substr($content,$imgstart,$index-$imgstart); // Keep up to including alt=" $translated_content .= $new_image_alt; $imgstart = strpos($content,'"',$index); // find the closing " in the original alt } } $translated_content .= substr($content,$imgstart,$imgend-$imgstart); // Keep the rest of img } } else { $translated_content .= substr($content,$imgstart,$imgend-$imgstart); // Keep original image } $imgstart = strpos($content,'<img',$imgend); // look for another image } $translated_content .= substr($content,$imgend,strlen($content)-$imgend); $post->post_content = $translated_content; }
If the post has images that are not yet translated, this function will display the ID’s (using a simple echo message not fancy at all…) to give you a chance to go back, translate the media and then duplicate the post again.
We are using this to translate this site: fibracreativa.com from French into English and Spanish.
Hope someone finds it useful.
Chouby, maybe this can be incorporated in the next version of the plugin.
Enjoy!
- The topic ‘Polylang post duplication and media translation’ is closed to new replies.