Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a lookup table to define a few things for the qTranslate plugin

Table

$trans_home = array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil');
$trans_company = array('en' => 'Company','it' => 'L\'azienda','de' => 'Firma','zh' => '公司','es' => 'Empresa','fr' => 'Société');

Code calling up lookup table

echo $trans_home[qtrans_getLanguage()];
echo $trans_company[qtrans_getLanguage()];

I thought that if I put the lookup table in the header file I would be able to call on the table throughout the template, but it seems I need to define the lookup table in each part of the template I need it in, so for example I would have to define the table in both in the header and footer for it to work.

So the question is how do I define a lookup table that will work across all PHP elements of the template?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Loading it in the header should work, or on the init hook. You will need to declare it global as well. So...

// define
global $trans_home;
$trans_home = array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil');

// and use in your templates
global $trans_home;
echo $trans_home[qtrans_getLanguage()];
share|improve this answer
    
Thanks, knew it would be something simple, delared it global and works perfectly. –  Vince Pettit Nov 8 '12 at 14:54
add comment

I would use some sort of function or object to wrap things up, rather than a global variable. WordPress has plenty of those already, no need to add more.

Eg.

<?php
function wpse71942_home($l)
{
    // only set it once
    static $langs = null;

    // Using filters makes your theme more extensible later
    if(is_null($langs))
        $langs = apply_filters('wpse71942_home_trans', array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil'));

    return isset($langs[$l]) ? $langs[$l] : '';
}

Usage:

<?php
echo wpse71942_home(qtrans_getLanguage());

Note that the current language is passed in -- it's a bit more "loosely coupled" with qTranslate.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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