2

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
  • I created a simple fix that allows you to select the language for multimedia files using Wordpress qTranslate plugin https://github.com/rafaelcalleja/qTranslateMultimediaLanguage
    – Rcalleja
    Commented Jan 30, 2014 at 17:01
  • @Rcalleja how can we use this patch? Is the dropdown bringing up other languages' input boxes? Or are you labeling this media belonging to a specific language?
    – cenk
    Commented Jan 30, 2014 at 20:37

2 Answers 2

1

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 .

3
  • Thanks. This looks logical, however it is nearly impossible for me to make the actual users learn and apply this method. If it was me only, it would work. I need a solution within the UI.
    – cenk
    Commented Feb 22, 2014 at 22:26
  • @cenk. this IS within the UI ...
    – krembo99
    Commented Feb 23, 2014 at 4:09
  • You are right, it is within the UI. But the need for teaching ordinary users to use the non standard qtranslate tags is a blocker for me. What I meant was, some sort of js solution to make the UI similar to the editor like selecting language tabs etc. Thanks again :)
    – cenk
    Commented Feb 23, 2014 at 8:35
0

You can download the plugin with the fix from GitHub.

Extracts qTranslate folder into your WordPress plugin directory and overwrite your actual "qTranslate" folder.

If you prefer, you can only extract qtranslate_hook.php file into your plugin folder.

Now, you must select the language for the media from /wp-admin dashboard.

Then, with this fix you can use the method get_attachment_language($attachment_id) to get the language.

You need to create one mediafile for each language required. Each one with the title, description, captions in the language selected. And change the logic of your template to show only the attachments of the current language.

1
  • Thank you for the answer. But my question was about having one media file and having the ability to translate titles and other fields for that single file.
    – cenk
    Commented Feb 22, 2014 at 22:23

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