WordPress.org

Support

Support » Plugins and Hacks » Polylang » [Plugin: Polylang] Keeping images for translated posts

[Plugin: Polylang] Keeping images for translated posts

  • Hello,
    I am searching for the possibility to not only keep the featured image, but all attachments for the translated post, is there a way to do this?
    Of course the second step would be to translate also the image metadata ( tiltle, description, caption,..), this would avoid uploading the same picture again times for each language.
    Anybody has got an idea?
    thanks for your support
    felicino

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

Viewing 15 replies - 1 through 15 (of 19 total)
  • Hello
    for the translations of the image data I wrote this plug-in.
    I really don’t know if this can create problems with other parts of polylang but on my local test site it seems to work.

    <?php
    /*
    Plugin Name: polylangimagetags
    Description: extention for img meta fields for polylang
     * Add translation strings to image post meta
     *
     * @param $form_fields array, fields to include in attachment form
     * @param $post object, attachment record in database
     * @return $form_fields, modified form fields
     */
    
    class Polylimg extends Polylang_Base {
    	function __construct() {
    		parent::__construct();
    		$polylang = new Polyimg();
    		if(!function_exists('get_languages_list')){
    			function get_languages_list($hide_empty = false) {
    				return get_terms('language', array('hide_empty'=>$hide_empty, 'orderby'=>'term_group' ));
    			}
    		}
    	}
    }
    
    function pll_the_post_thumbnail($size = 'post-thumbnail', $attr = ''){
    	$lang=pll_current_language();
    	$opt=get_option('polylang');
    	if($opt['default_lang']==$lang) {
    		echo get_the_post_thumbnail( null, $size, $attr );
    	}else{
    		   (int) $id = get_post_thumbnail_id();
    		$attr=array(
    			'title'=> get_post_meta( $id, 'pll_img_tags_name_'.$lang, true),
    
    			'class'	=> "attachment-$size",
    			'alt'	=> get_post_meta($id, 'pll_img_tags_alt_'.$lang, true),
    
    		);
    		echo get_the_post_thumbnail( null, $size, $attr );
    	}
    	}
    
    function pll_img_tags( $form_fields, $post ) {
    
    	global $polylang;
    	$lang=$polylang->get_languages_list();
    	$opt=get_option('polylang');
    
    	foreach ($lang as $language_name){
    		if( $language_name -> slug !== $opt['default_lang']){
    	$form_fields['pll_img_tags_name_'.$language_name -> slug ] = array(
    		'label' => 'Title '.$language_name -> name ,
    		'input' => 'text',
    		'value' => get_post_meta( $post->ID, 'pll_img_tags_name_'.$language_name -> slug , true ),
    		'helps' => 'Title in '.$language_name -> name
    	);
    
    	$form_fields['pll_img_tags_alt_'.$language_name -> slug ] = array(
    		'label' => 'Alt '.$language_name -> name ,
    		'input' => 'text',
    		'value' => get_post_meta( $post->ID, 'pll_img_tags_alt_'.$language_name -> slug , true ),
    		'helps' => 'Alt text in '.$language_name -> name
    	);
    	$form_fields['pll_img_tags_caption_'.$language_name -> slug ] = array(
    		'label' => 'Caption '.$language_name -> name ,
    		'input' => 'text',
    		'value' => get_post_meta( $post->ID, 'pll_img_tags_caption_'.$language_name -> slug , true ),
    		'helps' => 'Caption in '.$language_name -> name
    	);
    
    	}
    	}
    
    	return $form_fields;
    }
    
    add_filter( 'attachment_fields_to_edit', 'pll_img_tags', 10, 2 );
    
    /**
     * Save values of image metadata in media uploader
     *
     * @param $post array, the post data for database
     * @param $attachment array, attachment fields from $_POST form
     * @return $post array, modified post data
     */
    
    function pll_img_tags_save( $post, $attachment ) {
    
    	global $polylang;
    	$lang=$polylang->get_languages_list();
    	foreach ($lang as $language_name){
    	if( isset( $attachment['pll_img_tags_name_'.$language_name -> slug] ) )
    		update_post_meta( $post['ID'], 'pll_img_tags_name_'.$language_name -> slug, $attachment['pll_img_tags_name_'.$language_name -> slug] );
    
    	if( isset( $attachment['pll_img_tags_alt_'.$language_name -> slug] ) )
    update_post_meta( $post['ID'], 'pll_img_tags_alt_'.$language_name -> slug,  $attachment['pll_img_tags_alt_'.$language_name -> slug]  );
    
    	if( isset( $attachment['pll_img_tags_caption_'.$language_name -> slug] ) )
    update_post_meta( $post['ID'], 'pll_img_tags_caption_'.$language_name -> slug,  $attachment['pll_img_tags_caption_'.$language_name -> slug]  );
    
    	}
    	return $post;
    }
    
    add_filter( 'attachment_fields_to_save', 'pll_img_tags_save', 10, 2 );
    Plugin Author Chouby

    @chouby

    That’s really nice to share this.

    For your information, I am currently working on this feature for Polylang v0.9 (planned for the end of the summer). However I use a much different way. A different attachment is created for each language (although the media itself is of course not duplicated) and the language is associated with a taxonomy (like it is for posts) instead of using post metas.

    Sorry that it will come too late for you.

    Hello,

    I have Polylang Version 0.9.3 installed. Is the feature of having the same images for translatable post already implemented?

    I have post with many images attached. I need to translate it. If i click to add new translation, the images does not appear in the media uploader gallery of the translatable post.

    Plugin Author Chouby

    @chouby

    The reason for your problem is that you did not set any language for your images. And the media uploader shows only the media with the same language as the post you are currently editing.

    If you don’t need to translate images captions and descriptions, just add the following line in your wp-config.php (as described at the end of the documentation):

    define(PLL_MEDIA_SUPPORT, false);

    Hello once again,

    When editing post, I still crucially need to see all uploaded images to wordpress site no matter what post’s language is. In other words, I need to use same images for posts across multiple languages.

    Googled everywhere but did not find a solution. Maybe I have missed something very obvious.

    Adding define(‘PLL_MEDIA_SUPPORT’, false); does not help. Should it? It just disables posibility to choose image language. But image still does not appear at posts in different languages.

    I am using Polylang Version 0.9.8.

    Plugin Author Chouby

    @chouby

    Thank you, after upgrading I finally can see images for all posts no matter what their language is. Great! Thanks.

    Hello

    I am new to polylang and I have to say that it is one of the best plugins I have used. However I have a problem when I am trying to translate my media. The thing is that I haven’t found how I can do it. I have declared the language for all my media and when I am pressing Add new for the second language, I get back the editing screen. Is there a more detailed guideline? Maybe it’s so obvious that I can’t see it.

    regards

    G

    Plugin Author Chouby

    @chouby

    Yes you go to the editing screen, but you will notice that the language has changed. So you can enter the caption and so on in the new language.

    yes, but then need to upload images again?

    Plugin Author Chouby

    @chouby

    Of course not! Only one image is stored.

    So, the translation refers to the title and the alt text right? We don’t link two images together, one for each language, right?

    G

    Plugin Author Chouby

    @chouby

    Yes you can translate the title and the alt text, but the image is the same.

    I still don’t know how to do it…help.

    Plugin Author Chouby

    @chouby

    How to do what?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘[Plugin: Polylang] Keeping images for translated posts’ is closed to new replies.
Skip to toolbar