3

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:

<?php
/**
* The main template file.
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
?>
<?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>Latest News</h1>
...

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

4
  • 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, 2013 at 9:30
  • So you want to translate this static text: 'Latest News'? Jul 12, 2013 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) Jul 12, 2013 at 9:46
  • The : should be before the lang, not after. Ex : [:en] Apr 15, 2014 at 11:19

4 Answers 4

4

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<!--:-->'); ?>
4
  • 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) Jul 12, 2013 at 11:57
  • Which solution are you using? Post your code, pls Jul 12, 2013 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> Jul 12, 2013 at 12:07
  • It should be: <h1><?php echo qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<!--en:-->Latest News<!--:--><!--fr:-->dDerniers articles<!--:-->'); ?></h1> Jul 12, 2013 at 12:09
10

This solution worked for me

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

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

3
  • 1
    IMHO best answer. Short, simple and works fine.
    – Julia
    Feb 10, 2017 at 12:24
  • Yes best answer, worked fine for me with qTranslate-X 3.4.6.8 Feb 14, 2017 at 12:31
  • 1
    According to the FAQs this is a recommended way of doing it. qtranslatexteam.wordpress.com/faq
    – Arsen K.
    Jan 18, 2021 at 9:54
4

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
  • 1
    qtrans_getLanguage is deprecated use qtranxf_getLanguage instead May 31, 2018 at 8:51
1

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 and acknowledge you have read our privacy policy.

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