How to add qtranslate multi language support for media?

The multi language plugin qtranslate works for custom post types but there is no capability to translate media information like title, description, captions for images.

I am using the following code (WP v3.7.1) in order to adding support for custom taxonomies translation:

add_action('admin_init', 'qtranslate_edit_taxonomies');

function qtranslate_edit_taxonomies(){
   $args=array(
      'public' => true ,
      '_builtin' => false
   );
   $output="object"; // or objects
   $operator="and"; // 'and' or 'or'
   $taxonomies = get_taxonomies($args,$output,$operator);
   if  ($taxonomies) {
     foreach ($taxonomies  as $taxonomy ) {
         add_action( $taxonomy->name.'_add_form', 'qtrans_modifyTermFormFor');
         add_action( $taxonomy->name.'_edit_form', 'qtrans_modifyTermFormFor');        

     }
   }

}

I think qtrans_modifyTermFormFor can be used for any input box but I don’t know which hook to use. I checked media.php but no luck.

Is there anything that can be done like the example above for the attachment details inputs?

2 Answers
2

Good question .

Well.. After reading Rcalleja answer ..
You can use the plugin provided , but there is really no need for it, nor for code hacking or php juggling ..

All you have to do is to use the quicktags in the attachments fields :

[:en]English Description[:fr]French description[:de]German description

After that ( and now it depends on the access you have to theme files, but if you are here – I assume it is not a problem ) you will need to use a small built in function called qtrans_use() which tells qtranslate which language to use ..

qtrans_use('fr',$string,false);

so for example, if you are using the very popular function the_post_thumbnail() , you will have to translate itßs attributes . since it is using wp_get_attachment_image, we know the attributes are for example (from codex ) :

$default_attr = array(
    'src'   => $src,
    'class' => "attachment-$size",
    'alt'   => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt )),
);

Now , we need to get the alt and title like so :

$thumb_id = get_post_thumbnail_id(get_the_ID());
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
$title = get_post_meta($thumb_id, '_wp_attachment_image_title', true);

So putting 2 and 2 together :

    $attr = array(
'alt'   => qtrans_use('de',$alt,false),
'title' => qtrans_use('de',$alt,false),
    );

and then

the_post_thumbnail('my_size',$attr);

et voila ! the title and description will be in german .. ( 'de' )

( the ‘de’ is just an example, to automate the process one would another built-in qtrans function – qtrans_getLanguage() )

I have used this method in the past , and it always worked for me .
In fact, it is possible to translate any and all aspects of wordpress with qtranslate , without the needs for extra plugins ..

qtranlaste is one of the best written plugins ( albeit a bit old.. ) and I myself am a big fan of it . The author has used internal filters and hooks long before most of the current “experts” have started to apply them in plugins or even knew existed. in fact, It is ( IMHO ) the best translation plugin .

The problem is that ( like wordpress itself ) it is very extensive and extendable , but it has a lot of filter and functions that are poorly documented , which require to “dig into the code ” .

Luckily , someone has already done so .

EDIT I :

There is also another method to filter the whole post thumbnail produced HTML using the post_thumbnail_html filter. But since the above method is general and can be applied on any element ( not just post thumbnail ) , and since you did not specifically inquired for post thumbs but media in general – I thought it is preferred to explain it rather then a much more specific one .

Leave a Comment