Search code examples
phpwordpressmultilingualqtranslate

PHP multilingual site displaying array in correct order with Qtranslate


I'm running a wordpress blog with qtranslate. It's using 3 languages.

I created a custom Menu (price lists), which is saved to db using post_meta wp hooks.

I use simple logic to display list item, it worked perfectly when I was using only 1 language. But now that I added qtranslate I'm having problems.

Let me guide you through the resolution process so you better understand where I'm stuck!

So, I had this code:

        <?php
            if ( get_post_meta($post->ID, 'price_list_items_category1', true) ) :
                $pricelistline = explode("\n", get_post_meta($post->ID, 'price_list_items_category2', true));

                foreach($pricelistline as $value) {
                    $e = explode("|",$value);
                    echo "<li><p>" . $e[0] . " <span>" . $e[1] . "</span></p> <span class=\"listmenuprice\">" . $e[2] . "</span><div class=\"clearfloat\"></div><!-- Very Important --></li>\n";
                }
            endif;
        ?>

print_r for $pricelistline was:

Array ( [0] => [:en]Rhine Riesling1|0,75 l|9,50 € [1] => [:ge]ჩიხირთმა|100გ|10ლ [2] => [:ru]шоколад|100грамм|10 L )

Obviously my script was displaying all three languages at the same time... To fix this I read about qtranslate, and discovered that by using _e() qtranslate would take over the situation and display correct language and block unwanted ones.

So modified code:

        <?php
            if ( get_post_meta($post->ID, 'price_list_items_category1', true) ) :
                $pricelistline = explode("\n", get_post_meta($post->ID, 'price_list_items_category1', true));

                print_r($pricelistline);
                foreach($pricelistline as $value) {
                    $e = explode("|",$value);
                    _e( "<li><p>" . $e[0] . " <span>" . $e[1] . "</span></p> <span class=\"listmenuprice\">" . $e[2] . "</span><div class=\"clearfloat\"></div><!-- Very Important --></li>\n");
                }
            endif;
        ?>

Notice the difference here: _e( "<li><p>" . $e[0] . " <span>" . $e[1] . "</span></p> <span class=\"listmenuprice\">" . $e[2] . "</span><div class=\"clearfloat\"></div><!-- Very Important --></li>\n");

This seemed to have worked! as other language instances disappeared, so on English page I'd see only english version etc.

Problem is now I find some bogus html remaining when I view source.

HTML output looks like this now:

<ul class="listmenuitems" id="listingmenu_1">
<li><p>Rhine Riesling1 <span>0,75 l</span></p> <span class="listmenuprice">9,50 €
</span><div class="clearfloat"></div><!-- Very Important --></li>
<li><p><li><p>
</ul>

notice: <li><p><li><p> these are two opening elements of two remaining languages. My guess is something gets jammed in $e[1] on languages that are not displayed and this is why languages seem to work! but in reality there is some php error.

I did tail -f /var/log/apache2/error_log but I don't see any php errors.

So what is going on here? can you please help? I just need to get rid of extra html elements. For the rest my modification seems to have worked!

Came out as a long post, but I hope I gave all the details.

Thanks for reading :)

var_dump

array(3) { [0]=> string(37) "[:en]Rhine Riesling1|0,75 l|9,50 € " [1]=> string(43) "[:ge]ჩიხირთმა|100გ|10ლ " [2]=> string(38) "[:ru]шоколад|100грамм|10 L" }

Solution

  • I don't know how _e blocks out the other languages but it looks like it just drops when it finds a word in the wrong language, I suggest you do one of:

    Variant 1: don't use foreach, but instead use fixed indexes for your array depending on language

    Variant 2: check the first letters of the array yourself to see if it matches the correct language and if yes use the old echo instead.

    Variant 3: find a function that just tells you if the language is the one you're looking for and echo the output depending on that condition

    Update: looks like the function for Variant3 might be __ (that's two underscores I think)