I was wondering how to translate content under index.php (or category.php with qtranslate?).

I'm using twenty twelve theme.

My code on index.php:

* The main template file.
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
<?php get_header(); ?>
$page_id = 1500;
$page_data = get_page( $page_id );
echo apply_filters('the_content', $page_data->post_content);
<section id="last-articles_homepage">
<h1>Latest News</h1>

I would like to translate the content inside the <section> and <h1> tag for example in french.

  • Have you read the instructions for the plugin? I've never used it, but the instructions suggest it's as simple as visiting a page/post with a language argument on the URL, eg: ?lang=en or ?lang=fr... – t31os Jul 12 '13 at 9:30
  • So you want to translate this static text: 'Latest News'? – Krzysiek Dróżdż Jul 12 '13 at 9:30
  • @KrzysiekDróżdż : indeed. I've tried [en:]Englishtext[fr:]frenchtext instead of the static text, but it won't work. (sorry for my poor english) – Exalight3666 Jul 12 '13 at 9:46
  • The : should be before the lang, not after. Ex : [:en] – Mario Radomanana Apr 15 '14 at 11:19

Most correct way to do this would be to use WordPress translations.

You should replace this static text with:

<?php _e('YOUR TEXT', 'your_text_domain'); ?>

And add text domain to your theme.

More on this topic: http://codex.wordpress.org/I18n_for_WordPress_Developers

You can also...

... use qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage function.

Just use it like so:

<?php echo qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<!--en:-->Latest News<!--:--><!--fr:-->dernières Nouvelles<!--:-->'); ?>
  • it does not work... when i put it, i see the both language text in the same page :/ even if the other text block are displayed in the right language (because there are editable via the wordpress admin panel) – Exalight3666 Jul 12 '13 at 11:57
  • Which solution are you using? Post your code, pls – Krzysiek Dróżdż Jul 12 '13 at 11:58
  • <?php ?> <?php get_header(); ?> <?php $page_id = 1500; $page_data = get_page( $page_id ); echo apply_filters('the_content', $page_data->post_content); ?> <section id="last-articles_homepage"> <h1>Derniers articles<?php echo qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<!--en:-->Latest News<!--:--><!--fr:-->dDerniers articles<!--:-->'); ?></h1> – Exalight3666 Jul 12 '13 at 12:07
  • It should be: <h1><?php echo qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<!--en:-->Latest News<!--:--><!--fr:-->dDerniers articles<!--:-->'); ?></h1> – Krzysiek Dróżdż Jul 12 '13 at 12:09

This solution worked for me

<?php echo __('[:fr]Plus[:en]More'); ?>

Note : the " : " is before the language, not after

  • 1
    IMHO best answer. Short, simple and works fine. – Julia Feb 10 '17 at 12:24
  • Yes best answer, worked fine for me with qTranslate-X – Florian Boudot Feb 14 '17 at 12:31

I found the solution for a related problem of mine in a Qtranslate Support forums:

<?php if(qtrans_getLanguage() == "fr") : ?> <h1>This is French</h1>
<?php endif ?> 
<?php if(qtrans_getLanguage() == "en") : ?> <h1>This is English</h1> 
<?php endif ?>

I hope it would work for you! :)

  • 1
    qtrans_getLanguage is deprecated use qtranxf_getLanguage instead – Jamel Zarga May 31 '18 at 8:51

I got this from WP Forums and it worked for me.

<?php _e("[:fr]Les réseaux sociaux[:en]Social Media[:]"); ?>

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.